I am working on a project that computes the perimeter and the area of a triangle given its three sides a,b,c and that side b is the base of the triangle, I have made a class for it as shown below but I am having issues setting up the math and I guess the tester file to see if it all works, if someone could help me I would be very grateful
edit: I made some corrections(un-capitalizing class seemed to help some) as for the constructor I wanted to have one that handled all three sides and wasn't quiet sure how to handle it and while compiling this I ran into some errors which I can't figure out (its very late over here) and not quiet sure how to post my errors
// Class declaration
public class Triangle
{
// 3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;
// constant value for bad input.
public static final int INVALID_DIMENSION = -1;
// one arg constructor. Divides input by 3 and calls
// setter methods.
public Triangle(double length)
{
int sideLength = length/3;
setSideA(sideLength);
setSideB(sideLength);
setSideC(sideLength);
}
// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
if (a > 0)
this.sideA = a;
else
this.sideA = INVALID_DIMENSION;
}
// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
if (b > 0)
this.sideB = b;
else
this.sideB = INVALID_DIMENSION;
}
//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
if (c > 0)
this.sideC = c;
else
this.sideC = INVALID_DIMENSION;
}
// private method that cheks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
return false;
else
return true;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getArea()
{
if (isValid())
{
// Logic here
return // return new value here.
}
return INVALID_DIMENSION;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getPermiter()
{
if (isValid())
{
// Logic here
return // return new value here.
}
return INVALID_DIMENSION;
}
}
edit hi everyone! sorry for the delay I took your guys advice and took it bit by bit and I sorted out the errors I did add my math now, but I ran into an error that said "unreachable statement" and I can't figure out why here is the new new code with the comment on where the error pops up
// Class declaration
public class Triangle
{
// 3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;
private double s;
private double area;
// constant value for bad input.
public static final int INVALID_DIMENSION = -1;
// one arg construcotr. Divides input by 3 and calls
// setter methods.
public Triangle(double length)
{
double sideLength = length/3;
setSideA(sideLength);
setSideB(sideLength);
setSideC(sideLength);
}
// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
if (a > 0)
this.sideA = a;
else
this.sideA = INVALID_DIMENSION;
}
// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
if (b > 0)
this.sideB = b;
else
this.sideB = INVALID_DIMENSION;
}
//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
if (c > 0)
this.sideC = c;
else
this.sideC = INVALID_DIMENSION;
}
// private method that checks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
return false;
else
return true;
}
// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getArea()
{
if (isValid())
{
// Logic here
s = 1/2 * (sideA + sideB + sideC);
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
return getArea(); // return new value here.
}
else
{
return INVALID_DIMENSION;
}
} // checks to see if all values are valid. If all values pass then permiter is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned.
public double getPermiter()
{
if (isValid())
{
// Logic here
double s = (sideA+sideB+sideC)/2;
return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
return getPermiter(); // return new value here. this is where it is telling me where the error is
}
return INVALID_DIMENSION;
}
}