2016-09-26 HW06

posted Sep 26, 2016, 6:55 AM by Samuel Konstantinovich   [ updated Sep 26, 2016, 9:12 AM ]
Goal: Write one more class !

Student Class Example:
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 printStudent(){
     //since we have a toString...
         //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;
         if(gpa < 0){
            gpa = 0;
    public void raiseGPA(double n){
         gpa += n;
         if(gpa > 100){
            gpa = 100;

    public String toString(){
  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);//This will work because of the toString() method.
        System.out.println("Yabba dabba doo...");



        s.raiseGPA(-1);//this works, do we want it to?


LAB + HW, DUE THURS 8am. This gives you plenty of time to write, test, and get help. I will run batch tests on your solutions so be careful.

Add to the point class:
double distance( Point other) {  ...  }
This will help you write the triangle class.

1. First thing: Go through the entire Triangle class and see what you will be writing. 
-List all of the methods in your notes. I will be looking at this now.
-Check off the ones that you have a VERY good idea how to complete! This includes why it is needed, how it will be used, and how you will solve the problem.
-Label the ones you need to figure out, state if you aren't sure about the math or the java specific stuff. Basically explain what you aren't sure how to do.

2. Make a new directory Triangle, inside of which should be:
Paste your own Point, and my Triangle. 
Compile and test Point before you start working on Triangle.

3. Explain how you would write the ones that ask to be explained, write it on paper for now, but later 
copy what you wrote into  the comments of Triangle.java.

4. CODE!
4a. Write the class.
4b. Write a series of test cases to verify your code works. Discuss how you want to test this with your neighbors. If you didn't discuss this with enough people then you don't know if your tests are rigorous! My suggestion would be a discussion on the mailing list. If your tests are not sufficient, you will not receive credit.

public class Triangle{
    private Point v1, v2, v3;

    //Your coordinates would be null, and your methods will not
    //work properly (null pointer exceptions)
    public Triangle(){
    //make copies of the parameters, do not just say v1 = a, etc.
    public Triangle(Point a, Point b, Point c){
        //1. Explain why we want this, and how you will do it.

    public Triangle(double x1,double y1,double x2,double y2,double x3,double y3){

    //format: "Triangle @ (1,1) (3,2) (5,6)"
    public String toString(){

    public double getPerimeter(){

    public double getArea(){
        //2. Explain how to do this with the tools you have in Point/Triangle. 

    //do not use == to check if doubles are the same, because of rounding errors + irrational numbers.
    //Instead write a function to check if values are very close together.  
    private boolean isCloseEnough(double a,double b){
        //3. Explain how to calculate. 
        //Are 100,000,001 and 100,000,000 close together? they are 1 apart. 
        //Are 1 and 2 close together? they are also 1 apart

    //helper function for isRight, assume that ab is the longest side
    private boolean pycheck(Point a, Point b, Point c){
        //Optional but helpful.

    //It is suggested that you use pycheck 
    //Return true when the triangle is a right triangle, false otherwise.
    public boolean isRight(){