Difference between revisions of "Pythagoras"

From CompSciWiki
Jump to: navigation, search
(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 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 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 [[JOptionPane Methods|JOptionPane]]
+
*Input using [[Input using Scanner|Scanner]]
 
*Squares and Square roots using [[Math Methods]]
 
*Squares and Square roots using [[Math Methods]]
  
<pre>
+
{{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
</pre>
+
}}
  
 
|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 an input dialog box.
+
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.
<pre>
+
{{CodeBlock
String input;
+
|Code=
 +
double sideA;
  
// Get input 1
+
// Creates a new scanner that reads from console input
input = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle");
+
Scanner scan = new Scanner(System.in);
</pre>
+
 
 +
// 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.
<pre>import javax.swing.*; //needed for JOptionPane</pre>
+
{{CodeBlock
 +
|Code=
 +
import javax.util.Scanner; //needed for Scanner
 +
}}
  
The input you receive is going to be a string. You will need to convert that input into a double before using it.
+
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.
<pre>// Convert string to double
+
sideA = Double.parseDouble(input);</pre>
+
 
+
Do this for both sets of input. 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.
<pre>import java.lang.Math;//needed for pow, sqrt</pre>
+
{{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
<pre>//calculate A^2
+
{{CodeBlock
double sideA2 = Math.pow(sideA, 2);</pre>
+
|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
<pre>//calculate C^2
+
{{CodeBlock
double sideC2 = sideA2 + sideB2;</pre>
+
|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.
<pre>//calculate C
+
{{CodeBlock
double sideC = Math.sqrt(sideC2);</pre>
+
|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
<pre>//output solution
+
{{CodeBlock
System.out.println("The length of the hypotenuse is: " + sideC);</pre>
+
|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.swing.*; //needed for JOptionPane
+
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
String input;
 
 
double sideA;
 
double sideA;
 
double sideB;
 
double sideB;
 +
 +
                // Creates a new scanner that reads from console input
 +
                Scanner scan = new Scanner(System.in);
 
 
// Get input 1
+
// Prompt for input 1
input = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle");
+
System.out.println("Please enter the first side of the triangle");
 +
// Get input 1 from System.in
 +
sideA = scan.nextDouble();
  
// Convert string to double
+
// Prompt for input 2
sideA = Double.parseDouble(input);
+
System.out.println("Please enter the second side of the triangle");
 
+
// Get input 2 from System.in
// Get input 2
+
sideB = scan.nextDouble();
input = JOptionPane.showInputDialog(null, "Please enter the second side of the triangle");
+
 
+
// Convert string to double
+
sideB = Double.parseDouble(input);
+
  
 
//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

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