I had to write a java class that contains following: Private Instance Variable(s
ID: 3631172 • Letter: I
Question
I had to write a java class that contains following:Private Instance Variable(s):
an array or ArayyList of doubles for the three side of the triangle
Constructor with 3 doubles for the three sides of the triangle
Public Instance Methods
getPerimeter - computes the perimeter of triangle(the sum of three side and returns a double)
geArea - Computes the area of the triangle using the formula: Area = (s*(s-a)*(s-b)*(s-c))^0.5 where s=0.5*(the triangle's perimeter) where perimeter = sum of the three sides and the method returns double.
isARightTriangle - determines if the triangle satisfies c^2 = a^2+b^2(Where c is the longest side) and method should return a boolean(true/false).
I wrote the code that works perfectly for calculating the perimeter and for determining the right angle triangle. But i get an error when i call a method to calculate the area. PLEASE HELP ME TO FIND OUT WHAT DID I DO WRONG WITH getArea() method. the code is below:
--------------------------------------…
public class Triangle
{
private ArrayList<Double> sides = null;
public Triangle(double a, double b, double c)
{
sides = new ArrayList<Double>();
sides.add(a);
sides.add(b);
sides.add(c);
}
public double getPerimeter()
{
if (sides == null)
{
return 0.0;
}
return(sides.get(0) + sides.get(1) + sides.get(2));
}
public double getArea()
{
double s, calcSides, area;
s = 0.5*getPerimeter();
calcSides = (s*(s-sides.get(0)) * (s - sides.get(1)) * (s - sides.get(3)));
area = Math.pow(calcSides, 0.5);
if(sides == null)
{
return(0.0);
}
return area;
}
public boolean isARightTriangle()
{
double sidec = Math.pow(sides.get(2), 2);
double sidea = Math.pow(sides.get(0), 2);
double sideb = Math.pow(sides.get(2), 2);
double rHS = sidea + sideb;
double lHS = sidec;
if(sides==null)
{
return false;
}
if(rHS == lHS)
{
return true;
}
return false;
}
Explanation / Answer
public boolean isARightTriangle()
{
if(sides==null)
{
return false;
}
double sidec = Math.pow(sides.get(2), 2);
double sidea = Math.pow(sides.get(0), 2);
double sideb = Math.pow(sides.get(2), 2);
if(sidea == sideb+sidec)
{
return true;
}
if(sideb == sidea+sidec)
{
return true;
}
if(sidec == sideb+sidea)
{
return true;
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.