1 Intermediate Programming — Chapter 3 — MIS 233 Fall 2010.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Lecture 2: Object Oriented Programming I
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Java Syntax Primitive data types Operators Control statements.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Objects and Classes Mostafa Abdallah
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Classes - Intermediate
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Learners Support Publications Constructors and Destructors.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Constructors and Destructors
The need for Programming Languages
Haidong Xue Summer 2011, at GSU
Intro To Classes Review
Contents Introduction to Constructor Characteristics of Constructor
Chapter 4 (part 2).
Classes & Objects: Examples
Constructors and Destructors
Chapter 6 Objects and Classes
Java Programming Language
Chapter 7 Objects and Classes
Presentation transcript:

1 Intermediate Programming — Chapter 3 — MIS 233 Fall 2010

2 Example: Two Dimensional Point Create a point class with only x and y coordinates Add constructors No argument One parameter: taking a real value Two parameters: taking two real values One point object Add methods Get and set methods toString method for displaying the point Distance between two points Mid point between two points Illustrate use of that class from a test clas by

3 Class Point public class Point { private double x,y;// instace variables // constructors public Point() { // no argument constuctor x=0.0; y=0.0; } public Point(double x) { // one parameter consturtor this.x = x; // the use of this is required this.y = x; // the use of this is optional } public Point(double x,double y) { // two parameter consturtor this.x = x; // the use of this is required this.y = y; // the use of this is required }

4 Class Point (cont.) public Point(Point p) { // taking a Point as parameter and generating a copy of that point as another object this.x = p.x; // private instace variables can be reached as Point class manipulates a Point object this.y = p.getY(); // the use of this is optional } // get and set methods public double getX() { return x; } public double getY() { return y; }

5 Class Point (cont.) public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } // takes coordinates and utilize other methods available in clas Point for software reusability public void setXY(double x, double y) { setX(x); this.setY(y); // methods can be called using this as well }

6 Class Point (cont.) // method for calculating distace between two points // invoked over one of these points, takes the other as parameter public double distance(Point p) { // declare and initilize xd double xd = this.x – p.getX(); double yd; // declare yd yd = this.y – p.getY(); // initilize yd double dist = Math.sqrt(xd*xd+yd+yd); return dist; }

7 Class Point (cont.) // method for calculating mid point between two points // invoked over one of these points, takes the other as parameter, return mid point as a distict object public Point mid(Point p) { double xm = 0.5*(this.x + p.getX()); double ym = 0.5*(this.y + p.getY()); Point pm = new Point(xm,ym); return pm; /* last two lines can be shorted as return new Point(xm,ym); without creating a local reference variable pm but creation of a new Point object is always necessary }

8 Class Point (cont.) // toString method to display the point public String toString() { String s = “x coordinate:” + this.x +”\n”; s += “y coordinate:” + y +”\n”; return s; } } // end class Point

9 Test Class public class TestPoint { public static void main(String args[]) { // create two point object with two different constructors Point p1 = new Point(3.0); Point p2 = new Point(2.0,4.0); // create p3 as a copy of p1 Point p3 = new Point(p1); // distance between p1 and p2 double distp1p2 = p1.distance(p2); // same as p2.distance(p1)

10 Test Class (cont.) // mid point between p1 and p2 Point p4 = p1.mid(p2); /* mid method invoked over one of these objects creates and returns a new point object (the mid point) This is met in Test class by the reference p4 */ // displaying using toString System.out.printf(“%s”,p1.toString()); // equivalent to System.out.printf(“%s”,p1); // a String is expected no object name is enough toStrring method is invoked implicitly // display the characteristic of mid point without creating a reference variable System.out.printf(“%s”,p1.mid(p4)); } // end of main } // end of TestPoint

11 Addition to Point class Add a display method to the Point class public void display() { // another use of this System.out.printf(“%s”,this); // invoke toString on the object the method is invoked }

12 Test Class // main method of the test class public static void main(String[] args) { Point p1 = new Point(1,2); Point p2 = new Point(3,4); Point pm =p1.mid(p2); pm.display(); } // end of main A method with four java statements 1,2: create two point referece and objects 3: find mid point between p1 and p2 4: display the mid point

13 Test Class (cont.) // eliminate satatement 2 main() { Point p1 = new Point(1,2); Point pm =p1.mid(new Point(3,4)); pm.display(); } // end of main When calling mid method over p1: a new Point object is created and directly passed to the method as an argument without a referece variable so p2 is eliminated

14 Test Class (cont.) // eliminate satatement 1 main() { Point pm =new Point(1,2).mid(new Point(3,4)); pm.display(); } // end of main a new Point object is created with coordinates 1 and 2. Just after creating the object its mid method is invoked passing another oject as argumet without a referece variable to both p1 and p2 what the mid method returns is met by pm

15 Test Class (cont.) // eliminate satatement 3 main() { new Point(1,2).mid(new Point(3,4)).display(); } // end of main a new Point object is created with coordinates 1 and 2. Just after creating the object its mid method is invoked without a referece variable to both p1 and p2 what the mid method returns is another Point object without refering it with a reference variable its display method is invoked

16 Quiz #1 Summer 2010: A life simulation program Extension of the Quiz #1 Summer or Homework #1 Fall 2010 simple life simulation program Illustrate the concept of private constructors How to use this when passing the same object to a method

17 Quiz #1 Summer 2010: A life simulation program Write the java program for the following problem: Create a Person class having name,gender, mother and father as instance variables. Gender is an integer variable: 0 for males and 1 for females Constructor: Write a constructor having two parameters: gender and name and creates a person object. Used in the test class for creating first humans Mother and father are not known A private constructor: Write another constructor having four parameters: gender and name and creates a person object. Used in ihe born method for the objectrs whose mothers and fathers are known

18 Quiz #1 Summer 2010: A life simulation program Born method: Write a born method running over females (as the mother of the child), taking three parameters: a male object as the father of the child, name and gender of the child. The born method returns the a new person object if the genders of mother and father are corect otherwise returns null (return null;). Do not write any other method for the Person class. In the test class create two persons: a father and a mother and illustrate the use of the born method The child returned from the born method should be assigned to another person object in the test class as well.

19 Person class public class Person { private String name; private int gender; private Person mother, father; public Person(String n, int g ) { name = n; gender = g; // mother and father are null } private Person(String n, int g, Person m, Person f ) { name = n; gender = g; mother = m; father = f; }

20 Person class (cont.) public Person born(String n, int g, Person f) { /* creaate a Person objcect using the private constructor. The four parameter constructor can only be used within the class methods to create Persons objects (childeren whose mothers and fathers are known) Pass the mother as the third parameter using this */ Person child = null; if(gender == 0 and f.gender == 1) child = new Person(n,g,this,f); return child; }

21 Test class public class Test { main() { // use public constuctor when creating initial Persons Person pf = new Person(“Adem”,1); Person pm = new Person(“Havva”,0); // in the born method the private constructor will bu used Person pchild = pm.born(“Habil”,1,pf); } // end method main } // end class Test