# Triangle Java class problems...what to do to make it work??



## Anatolyz (Feb 6, 2008)

I had to write a triangle code enabeling it make some functions and including constructors inside..based on using the class Point(i atached the description file of it here and the Point.class itself)..I can't write a main line in my code cause it screams errors to me and i try to write a tester class checking the triangle class but it does nothing despite the triangle class passed the comilation..
here is the Triangle class i wrote so far:
import java.util.Scanner;
import java.lang.Math;


class Triangle { //Class which represents 3 points of triangle
private Point p1;
private Point p2;
private Point p3;

public Triangle() {
this(1,0, -1,0,0,1);
}
public Triangle (int x1,int y1,int x2,int y2,int x3,int y3) {
p1=new Point(x1,y1);
p2=new Point (x2,y2);
p3=new Point(x3,y3);}
public Triangle (Point a,Point b,Point c) {p1=new Point(a);p2=new Point(b);p3=new Point(c); }
public Point getPoint1() {return p1;}
public Point getPoint2() {return p2;}
public Point getPoint3() {return p3;}
public boolean isValid() { //checking if its a a triangle by the its points,if there are 3 X's or 3 Y's which are same its a straight line..and if all X's and Y's 
if (p1.getX()==p2.getX() && p2.getX()==p3.getX()) return false; //thats also makes it be not a triangle
else if (p1.getY()==p2.getY()&& p2.getY()==p3.getY()) return false;
else if (p1.getX()!=p2.getX()&&p2.getX()!=p3.getX()&&p1.getX()!=p3.getX() &&p1.getY()!=p2.getY()&&p2.getY()!=p3.getY() &&p1.getY()!=p3.getY()) return false;
else return true;
} 
public String toString() {
return ""+ p1.getX()+" ,"+p1.getY()+" "+p2.getX()+","+p2.getY()+" "+p3.getX()+","+p3.getY();} //supposed to print all of the points as strings
public void moveTriangle(int deltaX , int deltaY) { //supposed to move the whole triangle using the function "move" (see the attached file)
p1.move(deltaX,deltaY);
p2.move(deltaX,deltaY);
p3.move(deltaX,deltaY);}
public double getLengthOfSide(Point pp1, Point pp2) { //function which returns  us the length of a Side...by formula between 2 points 
double power1,power2,squareResult;
if (pp1!=p1&&pp1!=p2&&pp1!=p3) return 0;
else if ( pp2!=p1 &&pp2!=p2&&pp2!=p3) return 0;
else {power1=Math.pow(pp2.getY()-pp1.getY(),2);
power2=Math.pow(pp2.getX()-pp1.getX(),2);
squareResult=Math.sqrt(power1+power2);
return squareResult;
}}
public double getPerimeter() { //by adding all the Sides together it returns us the Perimeter
return (getLengthOfSide(p1,p2)+getLengthOfSide(p2,p3)+getLengthOfSide(p3,p1));}
public double getArea() { //by the Haron formula it finds us the area of the triangle
double perimeter=getPerimeter();
return Math.sqrt(perimeter*(perimeter-getLengthOfSide(p1,p2)))*(perimeter-getLengthOfSide(p2,p3))*(perimeter-getLengthOfSide(p3,p1));}

boolean isIsosceles() { //checking if the triangle is isosceles....
if ( getLengthOfSide(p1,p2)==getLengthOfSide(p2,p3)) return true;
else if (getLengthOfSide(p2,p3)==getLengthOfSide(p3,p1)) return true;
else if (getLengthOfSide(p3,p1)==getLengthOfSide(p1,p2)) return true;
else return false;}
}


thanks..


----------

