I'm writing a program to ask the user for 6 points for a triangle and then calculate the length of the 3 sides, the perimeter, and the area. Then the tester will have to display the properties of the triangle, length of sides, the area, the perimeter, and if the triangle is valid. Nothing too serious, just that if SideAB + SideBC > SideAC.. it's invalid.
Here's my class.
public class Triangle
{
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;
private double sideAB;
private double sideBC;
private double sideAC;
private double perimeter;
private double area;
private double s;
public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
public boolean isValid() {
if (sideAB()+sideBC() > sideAC())
return true;
else
return false;
}
public double sideAB()
{
double x = Math.pow(((x2)-(x1)),2);
double y = Math.pow(((y2)-(y1)),2);
sideAB = Math.sqrt((x+y));
return sideAB;
}
public double sideBC()
{
double x = Math.pow(((x3)-(x2)),2);
double y = Math.pow(((y3)-(y2)),2);
sideBC = Math.sqrt((x+y));
return sideBC;
}
public double sideAC()
{
double x = Math.pow(((x1)-(x3)),2);
double y = Math.pow(((y1)-(y3)),2);
sideAC = Math.sqrt((x+y));
return sideAC;
}
public double peterimeter(double perimeter)
{
perimeter = ((sideAB) + (sideBC) + (sideAC));
return perimeter;
}
public double area(double area)
{
s = perimeter/2;
area = Math.sqrt(s * (s - sideAB) * (s - sideBC) * (s - sideAC));
return area;
}
public String toString()
{
Double.toString(x1);
Double.toString(y1);
Double.toString(x2);
Double.toString(y2);
Double.toString(x3);
Double.toString(y3);
Double.toString(sideAB);
Double.toString(sideBC);
Double.toString(sideAC);
Double.toString(area);
Double.toString(perimeter);
String myTriangle = "A traingle with points " + "(" +x1+ "," +y1+ ")" + " " + "(" +x2+ "," +y2+ ")" + " " + "(" +x3+ "," +y3+ ")" + " a perimeter of: " +perimeter+ " and an area: " + area;
return myTriangle;
}
public double getX1()
{
return x1;
}
public double getY1()
{
return y1;
}
public double getX2()
{
return x2;
}
public double getY2()
{
return y2;
}
public double getX3()
{
return x3;
}
public double getY3()
{
return y3;
}
public String getPerimeter()
{
String p = Double.toString(perimeter);
return p;
}
public String getArea()
{
String a = Double.toString(area);
return a;
}
public double getSideAB()
{
return sideAB;
}
public double getSideBC()
{
return sideBC;
}
public double getSideAC()
{
return sideAC;
}
}
And Here's my tester
import java.util.*;
public class TriangleTester
{
public static void main(String[] args)
{
{
Scanner scanObject = new Scanner(System.in);
System.out.println("Enter x coordinate for first point: ");
double x1 = scanObject.nextInt();
System.out.println("Enter y coordinate for first point: ");
double y1 = scanObject.nextInt();
System.out.println("Enter x coordinate for second point: ");
double x2 = scanObject.nextInt();
System.out.println("Enter y coordinate for second point: ");
double y2 = scanObject.nextInt();
System.out.println("Enter x coordinate for third point: ");
double x3 = scanObject.nextInt();
System.out.println("Enter y coordinate for third point: ");
double y3 = scanObject.nextInt();
Triangle myTriangle = new Triangle(x1, y1, x2, y2, x3, y3);
if (myTriangle.isValid())
System.out.println(myTriangle);
else
System.out.println("This triangle is invalid");
}
}
}
I know that I never executed the methods, which is why the area and perimeter is always 0 no matter what values I put.
I tried putting the calculations inside like this :
public double getSideAB()
{
double x = Math.pow(((x2)-(x1)),2);
double y = Math.pow(((y2)-(y1)),2);
sideAB = Math.sqrt((x+y));
return sideAB;
}
But the output will always say the triangle is invalid. So now it just left me confused on where I should put the calculations.
Thanks