Difference between revisions of "Pythagoras"

From CompSciWiki
Jump to: navigation, search
m (Math is the name of a class, and should therefore be capitalized)
m (description of variables in formula now has the letters in math tags, so that they are properly italicized)
Line 3: Line 3:
 
|Problem= Create a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, and the result should be given using System.out.println. The input should be taken as two positive decimal values. Assume valid inputs.
 
|Problem= Create a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, 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 a and b are the two sides, and c is the hypotenuse.
+
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:
 
To solve this problem, you will need to understand:

Revision as of 01:05, 8 April 2010

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 will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, 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 an input dialog box.

String input;

// Get input 1
input = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle");

Don't forget your import statement.

import javax.swing.*; //needed for JOptionPane

The input you receive is going to be a string. You are going to need to convert that input into a double before using it.

// Convert string to double
sideA = Double.parseDouble(input);

Do this for both sets of input. There is a reason to use doubles instead of ints. 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 inputted <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>. To do so, you should 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