Difference between revisions of "Pythagoras"
(removed the end of program, as other programs aren't doing it) |
(Changed example to use scanner instead of JOptionPane; Added codeblocks) |
||
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 output the length of the triangle's hypotenuse. The two sides should be entered using two | + | |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. | 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: | ||
− | *Input using [[ | + | *Input using [[Input using Scanner|Scanner]] |
*Squares and Square roots using [[Math Methods]] | *Squares and Square roots using [[Math Methods]] | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
Sample Input: 3,4 | Sample Input: 3,4 | ||
Sample Output: 5.0 | Sample Output: 5.0 | ||
Line 15: | Line 16: | ||
Sample Input: 2.5,2.5 | Sample Input: 2.5,2.5 | ||
Sample Output: 3.5355339059327378 | Sample Output: 3.5355339059327378 | ||
− | + | }} | |
|SideSectionTitle=Primitive Data Types | |SideSectionTitle=Primitive Data Types | ||
Line 26: | Line 27: | ||
*printing out 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 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. |
− | + | {{CodeBlock | |
− | + | |Code= | |
+ | 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. | Don't forget your import statement. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
+ | 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. | 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. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
+ | 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 | Once the Math class is imported, calculating <math>a^2</math> is as simple as calling Math.pow with an exponent of 2 | ||
− | + | {{CodeBlock | |
− | double sideA2 = Math.pow(sideA, 2); | + | |Code= |
+ | //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 | 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 | ||
− | + | {{CodeBlock | |
− | double sideC2 = sideA2 + sideB2; | + | |Code= |
+ | //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. | 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. | ||
− | + | {{CodeBlock | |
− | double sideC = Math.sqrt(sideC2); | + | |Code= |
+ | //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 | With the length of the hypotenuse calculated, all that is left is to output the result. This should be done using System.out | ||
− | + | {{CodeBlock | |
− | System.out.println("The length of the hypotenuse is: " + sideC); | + | |Code= |
+ | //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. | You should now have a program that calculates the hypotenuse of a right-angled triangle. For the entire code solution, see below. | ||
+ | |||
|SolutionCode= | |SolutionCode= | ||
/* Class Pythagoras | /* Class Pythagoras | ||
Line 69: | Line 91: | ||
*/ | */ | ||
− | import javax. | + | import javax.util.Scanner; //needed for Scanner |
import java.lang.Math;//needed for pow, sqrt | import java.lang.Math;//needed for pow, sqrt | ||
Line 77: | Line 99: | ||
{ | { | ||
// These variables are used for input | // These variables are used for input | ||
− | |||
double sideA; | double sideA; | ||
double sideB; | double sideB; | ||
+ | |||
+ | // Creates a new scanner that reads from console input | ||
+ | Scanner scan = new Scanner(System.in); | ||
− | // | + | // Prompt for input 1 |
− | + | System.out.println("Please enter the first side of the triangle"); | |
+ | // Get input 1 from System.in | ||
+ | sideA = scan.nextDouble(); | ||
− | // | + | // Prompt for input 2 |
− | + | System.out.println("Please enter the second side of the triangle"); | |
− | + | // Get input 2 from System.in | |
− | + | sideB = scan.nextDouble(); | |
− | + | ||
− | + | ||
− | // | + | |
− | sideB = | + | |
//calculate A^2 | //calculate A^2 | ||
Line 109: | Line 131: | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
}} | }} |
Revision as of 15:00, 4 December 2011
Back to the Program-A-Day homepage
ProblemCreate 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 | |
---|---|---|
SolutionThere are three distinct steps in this program:
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 |