announcements‎ > ‎

2015-09-29 HW07

posted Sep 29, 2015, 10:43 AM by Samuel Konstantinovich   [ updated Sep 29, 2015, 5:11 PM ]
HW07: Complete the triangle class (sample Coordinate class provided)
Fill in the methods so that the Triangle class works as intended.
Write a Driver program to test your Triangle as you develop it. Do not write the whole thing then test.
Edit: Store your homework as we have done in the past few assignments:
~/apcs/hw07/Triangle.java
Triangle.java
public class Triangle{
    /*                                                                                                            
     *Instance Variables                                                                                          
     */
    private Coordinate v1,v2,v3;

    public Triangle(){}

    //1 
    public Triangle(Coordinate a,Coordinate b, Coordinate c){}

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

    //3
    public String toString(){
        return "Triangle!";
    }

    //4
    public double getPerimeter(){
        return 0.0;
    }

    //5  (use Heron's formula)
    public double getArea(){
        return 0.0;
    }

    //6 use your smarts, remember for booleans || is or, && is and.
    //e.g.    x > 0 || x < 0
    public boolean isRight(){
        return false;
    }

}


Coordinate.java

public class Coordinate{
    private double x, y;

    public Coordinate(){
x = 0;
        y = 0;
    }

    /*constructors                                                                                                   
    public <className> ( <params> ) { }                                                                              
     */
    public Coordinate(double x, double y){
        setX(x);
setY(y);
    }

    /*methods                                                                                                        
    <privacy> <returnType> <functionName> ( <params> ) { }                                                           
     */
    public double getX(){
return x;
    }

    public double getY(){
return y;
    }

    public String toString(){
return "("+x+","+y+")";
    }


    public void setX(double x){
        this.x = x;
    }

    public void setY(double y){
this.y = y;
    }

    public double distToOrigin(){
return Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
    }

    public double dist(double X, double Y){
return Math.sqrt(Math.pow((x-X),2)+Math.pow((y-Y),2));
    }

    public double dist(Coordinate other){
return Math.sqrt(
                  Math.pow(x-other.x,2)
                  +
                  Math.pow(y-other.y,2));
    }
}
Comments