2015-09-28 HW06

posted Sep 28, 2015, 10:51 AM by Samuel Konstantinovich   [ updated Sep 28, 2015, 9:26 PM ]
Edit:  [Exam Friday]
Edit2: Corrected the use of float. All float type variables should be double.
HW06: 
Upload it to ~/apcs/hw06/Coordinate.java  so you don't have to worry about bringing it to school.
Maybe also email it to yourself so you can get it whenever you need it.

Write a Coordinate class.
-must store x and y values.  Think what data type this should be before you write it.

0. Make a Driver class so you can test your coordinate as you write it. 

1. Constructors:
a) default: make the point (0,0)
b) non default: Take two values to fill the abscissa and ordinate. e.g. Coordinate(5.2,6) makes (5.2,6)

2. Accessors:
getX()
getY()
toString()  -> return a string in the format "(a,b)" where a  and b are the numbers stored in the coordinate.
distToOrigin() -> return the Euclidean distance from 0,0 to the coordinate.

3. Mutators:
setX(value)
setY(value)

Tricky: (see if you can do it!)
dist(AnotherCoordinate) -> return the distance from this coordinate to the parameter coordinate. 



Notes:

public class Student{
    private String firstName, lastName;
    private int age, osis;
    private double gpa;

    //useless default constructor
    public Student(){ 
    }
    public Student(String first, String last, int ID, int years){
         firstName = first;
         lastName = last;
         age = years;
         osis = ID;
         gpa = 100.0;//default gpa
    }

    public void print(){
         System.out.println(lastName+", "+firstName);
         System.out.println("ID: "+osis+"\tage: "+age);
         System.out.println("GPA: "+gpa);
    }
    
    public double getGPA(){
         return gpa;
    }

    public void lowerGPA(double n){
         gpa -= n;
    }
    public void raiseGPA(double n){
         gpa += n;
    }

    public String toString(){//did we talk about this in period 7? If not we will tomorrow.
  return lastName+", "+firstName+"\nID: "+osis+"\tage: "+age+"\nGPA: "+gpa ;
    }

}


public class Driver {
    public static void main(String[]args) {
        //Make an object
        Student s = new Student("Flintstone","Fred",100100100,42);

        System.out.println(s);
        System.out.println("Yabba dabba doo...");

        s.print();//This will work because of the toString() method.

        s.lowerGPA(-19);
        s.print();

        s.raiseGPA(1);
        s.print();


    }
}
Comments