Difference between revisions of "Pythagoras"

From CompSciWiki
Jump to: navigation, search
(Added Question and Code)
 
Line 1: Line 1:
 
{{1010PrAD|Pythagoras=The name of the program
 
{{1010PrAD|Pythagoras=The name of the program
  
|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. If a value is 0 or smaller, the program should inform the user that the value is invalid. If a value is not a number, let the program crash.
+
|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 a and b are the two sides, and c is the hypotenuse.
Line 38: Line 38:
 
String input;
 
String input;
 
double sideA;
 
double sideA;
double SideB;
+
double sideB;
 
 
 
// Get input 1
 
// Get input 1
Line 52: Line 52:
 
sideB = Double.parseDouble(input);
 
sideB = Double.parseDouble(input);
  
//Validate input
+
//calculate A^2
if(sideA > 0 && sideB > 0)
+
double sideA2 = Math.pow(sideA, 2);
{
+
//calculate A^2
+
double sideA2 = Math.pow(sideA, 2);
+
  
//calculate B^2
+
//calculate B^2
double sideB2 = Math.pow(sideB, 2);
+
double sideB2 = Math.pow(sideB, 2);
  
//calculate C^2
+
//calculate C^2
double sideC2 = sideA2 + sideB2;
+
double sideC2 = sideA2 + sideB2;
  
//calculate C
+
//calculate C
double sideC = Math.sqrt(SideC2);
+
double sideC = Math.sqrt(sideC2);
  
//output solution
+
//output solution
System.out.println("The length of the hypotenuse is: " + sideC);
+
System.out.println("The length of the hypotenuse is: " + sideC);
}
+
else
+
{
+
System.out.println("Invalid input, the side lengths must be greater than 0");
+
}
+
 
}
 
}
 
}
 
}

Revision as of 12:08, 6 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 a and b are the two sides, and c is the hypotenuse.

To solve this problem, you will need to understand:

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution...

For the entire code solution, see below.

Code

Solution Code

Back to the Program-A-Day homepage