Pythagoras

From CompSciWiki
Revision as of 15:00, 4 December 2011 by JordanW (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Create a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and output the length of the triangle's hypotenuse. The two sides should be entered using two calls to Scanner.nextDouble(), and the result should be given using System.out.println. The input should be taken as two positive decimal values. Assume valid inputs.

The formula for calculating the hypotenuse is <math>a^2 + b^2 = c^2</math>, where <math>a</math> and <math>b</math> are the two sides, and <math>c</math> is the hypotenuse.

To solve this problem, you will need to understand:

 Sample Input: 3,4
Sample Output: 5.0

Sample Input: 2.5,2.5
Sample Output: 3.5355339059327378 
 

Primitive Data Types

Wiki chars03.jpg

Solution

There are three distinct steps in this program:

  • reading in input from the user
  • calculating the result
  • printing out the result.

The first step is to read in the input. For each of the two inputs, you are going to need to use the scanner from System.in.

 double sideA;

// Creates a new scanner that reads from console input
Scanner scan = new Scanner(System.in);

// Prompt for input
System.out.println("Please enter the first side of the triangle");
// Get input
sideA = scan.nextDouble(); 

Don't forget your import statement.

 import javax.util.Scanner; //needed for Scanner 

You can use the same scanner for both inputs. There is a reason to use doubles instead of integers. Using two integer inputs, the output can result in a decimal value. Your output shouldn't be more precise than your inputs, so you should allow decimal values for input as well.

The next step is calculating the length of the hypotenuse. Recall, the formula is <math>a^2 + b^2 = c^2</math>. The user has supplied <math>a</math> and <math>b</math>, so the first thing you need to do is calculate <math>a^2</math> and <math>b^2</math>. Use Java's built-in method for calculating powers. The method is part of the Math class, so make sure you import it.

 import java.lang.Math;//needed for pow, sqrt 

Once the Math class is imported, calculating <math>a^2</math> is as simple as calling Math.pow with an exponent of 2

 //calculate A^2
double sideA2 = Math.pow(sideA, 2); 

Once you have calculated <math>a^2</math> and <math>b^2</math>, <math>c^2</math> can be calculated by adding the two values together

 //calculate C^2
double sideC2 = sideA2 + sideB2; 

Now that you have <math>c^2</math>, you just need to take the square root of that value, then you will have your solution. Luckily, the Java Math class has a built-in square root function.

 //calculate C
double sideC = Math.sqrt(sideC2); 

With the length of the hypotenuse calculated, all that is left is to output the result. This should be done using System.out

 //output solution
System.out.println("The length of the hypotenuse is: " + sideC); 

You should now have a program that calculates the hypotenuse of a right-angled triangle. For the entire code solution, see below.

Code

Solution Code

Back to the Program-A-Day homepage