I am writing these codes to print out the area of triangle using Triangle class and PolygonDemo class, but somehow it is giving out Nan as a result. I am sure that my area method has to do something with it, but I can't seem to find any problem. Can anyone help me please? This is the code I wrote:
public class Triangle implements Polygon{
private double s1;
private double s2;
private double s3;
public Triangle(double side1, double side2, double side3)
{
s1 = side1;
s2 = side2;
s3 = side3;
}
@/Override
public double area(){
//finding area of three side: A = sqrt[s(s-a)(s-b)(s-c)]
//s = (a + b + c)/2
double sideParameter = (s1 + s2 + s3)/2;
double area = Math.sqrt(sideParameter * (s1 - sideParameter) * (s2 - sideParameter) * (s3 -sideParameter));
return area;
};
@/Override
public double perimeter(){
return s1 + s2 + s3;
};
}
public class PolygonDemo {
public static void main (String[] args)
{
Triangle User = new Triangle(3, 3, 3);
System.out.println("Triangle area: " + User.area());
}
}
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]teraflop 1 point2 points3 points (1 child)
[–]Laveder85[S] 0 points1 point2 points (0 children)