Google Answers Logo
View Question
 
Q: Java Programming ( Answered 4 out of 5 stars,   3 Comments )
Question  
Subject: Java Programming
Category: Computers > Programming
Asked by: strongbow69-ga
List Price: $14.00
Posted: 13 Nov 2002 13:41 PST
Expires: 13 Dec 2002 13:41 PST
Question ID: 107134
Produce a Java program which, given three sides of a triangle,
determines whether the triangle is:

Equilateral - all sides the same length, 
Isosceles - two sides the same length or 
Scalene - no sides the same length 

Assume that any individual side must be a positive number not greater
than 100.0.
Answer  
Subject: Re: Java Programming
Answered By: seizer-ga on 13 Nov 2002 14:49 PST
Rated:4 out of 5 stars
 
Hi there strongbow.

I've written the program for you. I've included comments, so it should
be fairly self-explanatory, but if anything is unclear, then feel free
to request clarification before rating this answer.

You can download it from: http://xult.org/Triangle.java

Or you can copy and paste from below.

Good luck

--seizer-ga

To compile this code, simply type at the command line: 

javac Triangle.java

To run it, type:

java Triangle

-------- CODE IS BELOW THIS LINE --------

// We need to import this for user input
import java.io.*;

public class Triangle {

	public static void main(String args[]) {
		// declare our variables
		int side1=0, side2=0, side3=0;
		// Read in the lengths of the sides
		try {
			side1 = Integer.parseInt(getInput("Please enter the length of the
first side: "));
			side2 = Integer.parseInt(getInput("Please enter the length of the
second side: "));
			side3 = Integer.parseInt(getInput("Please enter the length of the
third side: "));
		} catch (NumberFormatException e) {
			// We might get this error if the user types in a string, rather
than a valid integer
			System.out.println("Error: " + e.getMessage());
			System.exit(1);
		} catch (IOException e) {
			// We might get this error if there was a problem reading from the
keyboard
			System.out.println("Error: " + e.getMessage());
			System.exit(1);
		}
		
		// Here we do a sanity check. If the user does not type in three
valid sides, we warn them and exit
		if (side1 + side2 + side3 != 180) {
			System.out.println("Warning: sides should add up to 180 to be a
valid triangle");
			System.exit(1);
		}
		
		/* 
		 * Now we determine which triangle it is.
		 *
		 * If side1 is equal to side2, AND  side2 is equal to side3, we know
that
		 * side1 must be equal to side3 because of the rule of inference.
This
		 * means that the triangle is equilateral.
		 *
		 * If side1 is equal to side2, OR side1 is equal to side3, OR side2
is equal to
		 * side3, then the triangle fits the criteria for being an
isosceles.
		 *
		 * If neither of the above are true, then we know that the triangle
must be
		 * scalene.
		 *
		 */
		if (side1 == side2 && side2 == side3) {
			System.out.println("Equilateral");
			System.exit(0);
		} else if (side1 == side2 || side1 == side3 || side2 == side3) {
			System.out.println("Isosceles");
			System.exit(0);
		} else {
			System.out.println("Scalene");
			System.exit(0);
		}
        }
	
	// We use this method to obtain console input from the user.
	public static String getInput(String question) throws IOException {
		// Display the question to the user
		System.out.print(question);
		// obtain their input
		BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
		// return that input
		return in.readLine();
	}
}

-------- CODE IS ABOVE THIS LINE --------

Clarification of Answer by seizer-ga on 13 Nov 2002 16:32 PST
As the commenters below have pointed out, the check

if (side1 + side2 + side3 != 180)

is wrong. I was of course thinking of angles - please excuse my absent
mindedness!

larryjay7's suggestion is an excellent one, and I would recommend the
substitution of his conditional for my erroneous one.

--seizer-ga
strongbow69-ga rated this answer:4 out of 5 stars and gave an additional tip of: $1.00
Thanks good answer and quick. Had to change bit about sides adding up to 180 though.

Comments  
Subject: Re: Java Programming
From: rbnn-ga on 13 Nov 2002 15:53 PST
 
What is this line for?

if (side1 + side2 + side3 != 180) { 
   System.out.println("Warning: sides should add up to 180 to be a valid triangle");
Subject: Re: Java Programming
From: rpcxdr-ga on 13 Nov 2002 15:57 PST
 
Nice program - Is there a way we can get extra credit on this assignment? =)
Subject: Re: Java Programming
From: larryjay7-ga on 13 Nov 2002 16:19 PST
 
I think there is a confusion of sides with angles here....

if (side1 + side2 + side3 != 180) {  
   System.out.println("Warning: sides should add up to 180 to be a
valid triangle");

Instead, what you should check for is that no side is greater than the
sum of the other two sides)

if ( (side1 > (side2 + side3)) ||  (side2 > (side1 + side3)) || (side3
> (side2 + side1)) ) {
 System.out.println("Warning: sides do not form a valid triangle");

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy