How to compare 3 variables?

Asked

Viewed 188 times

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();
    }
    
}
 

2 answers

2

First, you are calculating the areas - not the perimeters, as was said in the question - but anyway, the idea below would be the same.

The problem is in this condition:

if (areaX > areaY && areaZ)

That nay is comparing whether areaX is at the same time larger than areaY and areaZ. Actually you’re trying to test two conditions:

  • if areaX is greater than areaY
  • if areaZ

The second "condition" is not really a condition because areaZ is a double, but the conditions of a if must be values boolean (there are languages that allow other values in contexts boolean - example - but this is not the case for Java). So much so that this if nor compiles.


Therefore, to test whether a value is greater than all others, you must explicitly put these conditions in the if:

if (areaX > areaY && areaX > areaZ) {
    System.out.println("Larger area: X");
} else if (areaY > areaX && areaY > areaZ) {
    System.out.println("Larger area: Y");
} else {
    System.out.println("Larger area: Z");
}

Note that you are using the values of x to calculate the area of y:

double areaY = Math.sqrt(p * (p -x.a) * (p-x.b)*(p - x.c));

I mean, it should be:

// usar y em vez de x
double areaY = Math.sqrt(p * (p - y.a) * (p - y.b) * (p - y.c));

Of course there are other improvements. For example, the class itself Triangle could be able to calculate your own area:

public class Triangle {
    private double a, b, c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double area() {
        double p = (a + b + c) / 2.0;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }
}

I also changed the constructor to receive the values from the sides, because I understand that it does not make sense to create a triangle without having the sides (that is, it does not make much sense to make new Triangulo() and only set the sides later, create a triangle only if you already have the 3 sides) - see more on the subject at What good is a builder? - then it would look like this:

System.out.println("Enter the mensures of triangules X: ");
Triangle x = new Triangle(sc.nextDouble(), sc.nextDouble(), sc.nextDouble());

double areaX = x.area();

Anyway, there are several other things to improve, but I believe that it is already too far from the scope of the question. The main (check the highest value between 3 different values) is already solved above.

1

Well, my knowledge in java is little or nothing, but it seems to me that you have to compare all the variables, and in your code it seems to me that they are not all being compared. Taking your simple code of logic:

    if (areaX > areaY && areaX > areaZ){
        System.out.println("Larger area: X");
    }else if(areaY > areaX && areaY > areaZ){
        System.out.println("Larger area: Y");
    }else if(areaZ > areaX && areaZ > areaY){
        System.out.println("Larger area: Z");
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.