Help,

I'm trying to get which object has the greater area of the 3 triangles. Also want to print out the info for each triangle object by calling toString(). If any one would check my work I have done if for any change I need it.
Thank you.

// testerTriangle
public class TestTriangle {

    public static void main (String [ ] args) {
    Triangle Triangle01 = new Triangle( 2, 4.5, 6 );
    Triangle Triangle02 = new Triangle( 1, 3.5, 3 );
    Triangle Triangle03 = new Triangle( 3, 2.5, 4 );

    System.out.println("The Triangle01 Sides are "  + Triangle01.toString());   
    System.out.println("The Triangle’s Area is "  + Triangle01.getArea());
    System.out.println("The Triangle’s Perimeter is " +  Triangle01.getPerimeter());

    System.out.println("\nThe Triangle02 Sides are "  + Triangle02.toString()); 
    System.out.println("The Triangle’s Area is "  + Triangle02.getArea());
    System.out.println("The Triangle’s Perimeter is " +  Triangle02.getPerimeter());

    System.out.println("\nThe Triangle03 Sides are "  + Triangle03.toString()); 
    System.out.println("The Triangle’s Area is "  + Triangle03.getArea());
    System.out.println("The Triangle’s Perimeter is " +  Triangle03.getPerimeter());

    if ( Triangle01.getArea() > Triangle02.getArea() && Triangle01.getArea() > Triangle03.getArea() )
        System.out.println( "\n TRIANGLE 01 is greater. ");
    else if ( Triangle02.getArea() > Triangle01.getArea() && Triangle02.getArea() > Triangle03.getArea() )
        System.out.println( "\n TRIANGLE 02 is greater. ");
    else if ( Triangle03.getArea() > Triangle01.getArea() && Triangle03.getArea() > Triangle02.getArea() )
        System.out.println( "\n TRIANGLE 03 is greater. ");
    else
        System.out.println( "\n TRIANGLE IS NOT EQUAL. ");

    }
}

I don't really see what you are asking... Is there something wrong with your code? Does it not run correctly?

I want to know if my code needs some change in the logic.

if this code doesn't work right that's the time you ask question..

It makes sense to me. This is probably the simpliest way I can think of doing this. The only problem I see is a logical statement (not computer logic, human logic). The else considition says TRIANGLE IS NOT EQUAL. Wouldn't getting that output mean atleast two of them are equal? Other than that, nothing is wrong. Nicely done.

well, we don't know what getArea returns, nor how or when it is set (we can only guess), so how are we supposed to see whether the > operator will return the wanted result.
as far as I know, we can't even be sure this code'll compile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.