Overclock.net banner

1 - 8 of 8 Posts

·
Registered
Joined
·
1,290 Posts
Discussion Starter #1
I have to write a program that will take 3 numbers and tell me if I have a right triangle, a triangle but not a right triangle, or no triangle at all. I have a program (see code below) that works, but it seems incredibly bloated for what I want to do. Is there a simpler way to write a program that will do what I want?

Code:

Code:
import java.util.Scanner;

/**
 * Compares 3 numbers and determines what kind,
 *if any, triangle can be formed.
 * @
 * 09/19/2010
 */
public class TestTriangle {
public static void main (String [] args) 
{
Scanner in = new Scanner (System.in);
System.out.println("Enter 3 numbers");
int num1 = in.nextInt();
int num2 = in.nextInt();
int num3 = in.nextInt();

if ((num1^2 + num2^2) == (num3^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else if ((num2^2 +num3^2)== (num1^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else if ((num3^2 + num1^2) == (num2^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else
if ((num1 + num2 <= num3))
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else if ((num2 + num3 <= num1))
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else if (num3 + num1 <= num2)
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else
if ((num1 + num2 > num3))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
else if ((num1 + num3 > num2))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
else if  ((num2 + num3 > num1))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
}
}
 

·
Registered
Joined
·
857 Posts
Im not too sure how you could trim it down, however isnt everything from

Code:

Code:
if ((num1 + num2 <= num3))
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
and below supposed to have the numbers squared?
 

·
Registered
Joined
·
2,577 Posts
Are we talking about lengths?
Any three lengths will form a triangle.

Second of all, what are all of the "%d" for?

In java, you do not use the % sign to print anything.

Also, you will need to import Math for math.pow(int1,int2);
 

·
Registered
Joined
·
335 Posts
Quote:

Originally Posted by cdolphin View Post
Are we talking about lengths?
Any three lengths will form a triangle.

Second of all, what are all of the "%d" for?

In java, you do not use the % sign to print anything.
%d is used to represent a variable defined after the string has been close ie

System.out.printf (%d, x) will print the variable x.
 

·
Registered
Joined
·
783 Posts
If the program works. You do not need to do anything to it. You have a small amount of code. Worry about bloating when you take up more than 20,000 lines.

Does the code work? I haven't done any java programming since may
.

I think the carrot (^) works but if it doesn't you will have to do MATH.pow(num1,2).
 

·
Registered
Joined
·
2,577 Posts
Wow! You learn something new every day.

You can just use:
System.out.println(Object O);

With regards to the bloated nature of your code...It would be easier to find a hypoteneuse first.

Can you first explain the inputs; if they are lengths, then any three lengths will form a triangle, unless my sickly mind is missing something...

Code:

Code:
int hyp = -1;

if(num1 > num2 && num1 > num3)//num1 is greatest
hyp = num1;
else if (num2 > num1 && num2 > num3) //num 2 is greatest
hyp = num1;
else
hyp = num3;
 

·
Registered
Joined
·
1,248 Posts
Well, you could combine some elseif statements into a single if statement by using || (ie, num1+num2>num3 || num1+num3>num2 ||...etc), but that would really just be taking some vertical space and turn it into horizontal space I guess.

Also, I don't know them off the top of my head, but I'm sure you could put the lengths into an array or some kind of list, and then use some prebuilt method to put them in order from least to greatest, which would eliminate the need to test all the different configurations.

Quote:

Originally Posted by cdolphin View Post
Can you first explain the inputs; if they are lengths, then any three lengths will form a triangle, unless my sickly mind is missing something...
No, you can't have a side that is larger than the sum of the other two sides in a triangle.
 

·
Registered
Joined
·
1,290 Posts
Discussion Starter #8
Quote:

Originally Posted by cdolphin View Post
Wow! You learn something new every day.

You can just use:
System.out.println(Object O);

With regards to the bloated nature of your code...It would be easier to find a hypoteneuse first.

Can you first explain the inputs; if they are lengths, then any three lengths will form a triangle, unless my sickly mind is missing something...

Code:

Code:
int hyp = -1;

if(num1 > num2 && num1 > num3)//num1 is greatest
hyp = num1;
else if (num2 > num1 && num2 > num3) //num 2 is greatest
hyp = num1;
else
hyp = num3;
not any 3 lengths will form a triangle. Try forming a triangle with 3 lines, of length 1, 1, and 4.
 
1 - 8 of 8 Posts
Top