09-25-2013 - HW

posted Sep 25, 2013, 8:17 AM by Samuel Konstantinovich   [ updated Oct 7, 2013, 7:35 AM ]
-Miscelanery: Books, Accounts,  Mailing list management, getting help on the Mailing list 
-If statements.
-File redirection
-Expanding an object to do more:
    Having completed a few classes (Coordinate) we should look at ways of adding methods to improve their functionality.
-ORDER OF OPERATIONS:


We had a constructor already:
    public Coordinate()
x=0.0;
y=0.0;
    }

But we can make a more flexible one:
    public Coordinate(double a,double b)
x=a;
y=b;
    }
This enables us to make a new instance with values we already decided.
Example:

Coordinate A = new Coordinate();  //A is located at 0,0
Coordinate B = new Coordinate(9.0,5.3);  //B is located at 9.0,5.3


Homework:
    Expand your Coordinate class with 2 methods:

1. distance()  : a method that returns the distance from the current Coordinate to 0,0.
2. distance(Coordinate A) : a method that returns the distance from the current Coordinate to Coordinate A
3. If 2 is too hard, try    distance(double x2, double y2) : a method that returns the distance from the current Coordinate to (x2,y2)

Sample usage in a driver program:

//say you had:
Coordinate a,b,c;
a= new Coordinate(0.0,1.0);
b= new Coordinate(1.0,2.0);
c= new Coordinate(4.0,6.0);

//if you printed:    a.distance()   you would get 1.0 as a result. 
//if you printed:    a.distance(b)  you would get 1.414....  [an approximate square root of 2]
//if you printed.    b.distance(c)  you would get 5.0  [it is a 3,4,5 triple]
    
Comments