09-27-2013 - HW*

posted Sep 27, 2013, 8:28 AM by Samuel Konstantinovich   [ updated Oct 3, 2013, 7:42 AM ]
Lab: Work on a Triangle class and test program. This will be submitted on Dropbox by Sunday 11:59pm. Place it in a folder called HW3.

Things to mention:
- It is preferred to use the Mutators in the constructor
- the keyword this, so set methods are more clear.

Write the Triangle class:
Instance Variables
-Coordinates:v1,v2,v3   //If your Coordinate does not work, I posted code.
Methods
-Triangle(x1,y1,x2,y2,x3,y3) //accept 6 doubles
-Triangle(V1,V2,V3) //accept 3 Coordinates
-getPerimeter() -> returns a double
-getType() -> returns "equilateral" "isosceles" "scalene"
-toString()  //format: Triangle @ (x1, y1),(x2, y2),(x3, y3) 
e.g.
"Triangle @ (2.0,3.0),(0.0,0.0),(9.0,3.5)"

Write a program:  TestMyTriangle that will to test all of the methods when you run it.


Some hints:
   
NEVER DO THIS: 
public class BigObject{
    private SmallObject2 a = new SmallObject();
The instance variable _CAN_ be initialized outside of a constructor, then Java would put the initialization code inside the constructor. There are many problems with this, not the least of which is you don't know exactly where it would go. Other problems include the inability to include error checking (or exception handling.. but you don't know about this yet) and the inability to call the initialization code again if you want to reuse it. 

INSTEAD DO EITHER:
public class BigObject1{
    private SmallObject a;
    //THIS:
    public BigObject(){
        a = new SmallObject();
     }
    //OR THIS:
    public BigObject( SmallObject x ){
        a = x;
     }

SAMPLE SOLUTIONS:

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

    public Triangle(Coordinate v1,Coordinate v2,Coordinate v3){
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
    }

    public Triangle(double x1,double y1,
   double x2,double y2,
   double x3,double y3){
v1 = new Coordinate(x1,y1);
v2 = new Coordinate(x2,y2);
v3 = new Coordinate(x3,y3);
    }

    public double perimeter(){
double s1 = v1.distance(v2);
double s2 = v2.distance(v3);
double s3 = v1.distance(v3);
return s1 + s2 + s3;
    } 

    public String toString(){
//When you try to do: String + object 
//the object uses its toString method before the + operator can be used.
return "Triangle @ "+v1+v2+v3;
    }
   
    private boolean isEqual(double a,double b)
    {
return Math.abs( (a - b) / b) < 0.000001;
    }
    
    public String getType(){
double s1 = v1.distance(v2);
double s2 = v2.distance(v3);
double s3 = v1.distance(v3);
if( isEqual(s1,s2) && isEqual(s2,s3)){
   return "equilateral";
}else if( isEqual(s1,s2) || isEqual(s2,s3) || isEqual(s3,s1)){
   return "isosceles";
}else{
   return "scalene";
}
    }
}

STOP COPYING!!! THIS IS THE SECOND FILE

public class TriangleDemo{

    public static void main(String[]args){
        //3 different types of triangles:
Triangle ROB = new Triangle(0,0,1,Math.sqrt(2),2,0);
Triangle DAV = new Triangle(2,0,0,0,1,Math.sqrt(3));
Triangle JAM = new Triangle(0,0,3,0,3,4);
System.out.println("Perimeter of a 3/4/5 triangle: "+JAM.perimeter());
System.out.println(ROB);
System.out.println(ROB.getType());

System.out.println(DAV);
System.out.println(DAV.getType());

System.out.println(JAM);
System.out.println(JAM.getType());
    }
}


Comments