Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Intermediate Programming — Chapter 3 — MIS 233 Fall 2010."— Presentation transcript:

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

2 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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


Download ppt "1 Intermediate Programming — Chapter 3 — MIS 233 Fall 2010."

Similar presentations


Ads by Google