0
votes

My assignment consists of this: I have to design a calculator that allows user to enter the number of triangles. Goals are the following:

-User enters x number of triangles

-Computer automatically generates 3 numbers from my list of numbers between 3 and 13.

-Computer displays those numbers on screen.

-It decides if the numbers represent the sides of a valid triangle. (For example, 5, 5, and 13 do NOT create a triangle).

-If the numbers do not represent a valid triangle, displays an error message

-If the numbers are valid, the program determines, and displays

a) Side classification of the triangle – equilateral, isosceles, or scalene, and

b) The angle classification of the triangle – right, acute, or obtuse

[Here's what I did, but something seems to have gone wrong,as computer only displays "Acute", or "Not valid".]

public class triangle_creator extends javax.swing.JFrame {

private static int side1 = (int) (11 * Math.random() + 3);
private static int side2 = (int) (11 * Math.random() + 3);
private static int side3 = (int) (11 * Math.random() + 3);
private static int userNum; 
private static String userInput; 

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    //get number of triangles by user in amountInput field 

    userInput=amountInput.getText(); 
    side1=Integer.parseInt(userInput);
    side2=Integer.parseInt(userInput); 
    side3=Integer.parseInt(userInput); 

    //add all sides together

    int sumTriangle= side1+ side2+ side3; 

   // generate error message 

    if(side1+ side2< side3){
        outputArea.setText("Not valid");
    }  
    else{
        if (side1==side2 && side1==side3 && side2==side3 )
            outputArea.setText("Equilateral"); 
        if (sumTriangle==sumTriangle) 
            outputArea.setText("Isosceles"); 
        if (side1!=side2&& side1!=side3&& side2!=side3)
            outputArea.setText("Scalene");

    if (side1*side1+ side2*side2==side3*side3){
        outputArea.setText("Triangle is right");
    }        

    else if (side3*side3< side1*side1+ side2*side2)   
        outputArea.setText("Triangle is acute");

    else if (side3*side3> side1*side1+ side2*side2){
        outputArea.setText("Triangle is obtuse");

    }

    else 
        outputArea.setText("Error");

}

I would really appreciate if you guys could give me at least one tip on how to make this work. Thaaanks!!! :)

1
Are you sure this code ever wrote "Not Valid"? - Henry
@Henry Yes, only "Not Valid and "Triangle is acute". When I ran the program and entered the amount of triangles, and clicked on calculate, it only displayed one of those two words. - Grace Kenny
If you step through this with a debugger, you will see immediately why you're not getting the results you expect. - Dawood ibn Kareem

1 Answers

0
votes

Your problem is that side1, side2 and side3 are all set to the same value, namely the value returned by Integer.parseInt(userInput).

In most cases, what will happen next is that side1+ side2< side3 will be false, so the code enters the else block. Now a whole lot of conditions are evaluated, and eventually, side3*side3< side1*side1+ side2*side2 will be evaluated, and turns out to be true, so "Triangle is acute" is displayed.

But if you enter a large enough value in the field, you can get integer overflow when evaluating side1+ side2< side3 - that is, side1 + side2 might be too big to fit into an int field, so it will overflow to a negative value. In that case, this condition will be true, "Not valid" will be displayed, and the else block will never be entered.