2
I want to make an algorithm that calculates which of the 3 triangles has the largest perimeter, but my logic is not working.
import entities.Triangle;
import java.util.Locale;
import java.util.Scanner;
public class OrientaçãoObjetos {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner (System.in);
Triangle x,y,z;
x = new Triangle();
y = new Triangle();
z = new Triangle();
System.out.println("Enter the mensures of triangules X: ");
x.a = sc.nextDouble();
x.b = sc.nextDouble();
x.c = sc.nextDouble();
System.out.println("Enter the mensures of triangules Y: ");
y.a = sc.nextDouble();
y.b = sc.nextDouble();
y.c = sc.nextDouble();
System.out.println("Enter the mensures of triangules Z: ");
z.a = sc.nextDouble();
z.b = sc.nextDouble();
z.c = sc.nextDouble();
Double p = (x.a + x.b + x.c) / 2.0;
double areaX = Math.sqrt(p * (p -x.a) * (p- x.b)*(p - x.c));
p = (y.a + y.b + y.c) / 2.0;
double areaY = Math.sqrt(p * (p -x.a) * (p-x.b)*(p - x.c));
p = (z.a + z.b + z.c) / 2.0;
double areaZ = Math.sqrt(p * (p -z.a) * (p-z.b)*(p - z.c));
System.out.printf("Triangle X area: %.4f%n", areaX);
System.out.printf("Triangle Y area: %.4f%n", areaY);
System.out.printf("Triangle z area: %.4f%n", areaZ);
if (areaX> areaY && areaZ){
System.out.println("Larger area: X");
}else if(areaY>areaZ && areaX){
System.out.println("Larger area: Y");
}else{
System.out.println("Larger area: Z");
}
sc.close();
}
}