Last active
November 10, 2019 04:53
-
-
Save lbattaglioli2000/6b6144af26def13c8f20406d5b011e4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
import java.util.Random; | |
/** | |
* @author lbattaglioli | |
* @version 1.0.0 | |
* | |
* This program asks if you need a ladder, and then | |
* tells you about some random ladder leaning against | |
* some random house. | |
*/ | |
public class Ladder { | |
public static void main(String[] args){ | |
int option = JOptionPane.showConfirmDialog( | |
null, | |
"Do you need a ladder?", | |
"Ladder Tool v2.0", | |
JOptionPane.YES_NO_OPTION, | |
JOptionPane.QUESTION_MESSAGE | |
); | |
// Checks if the user selected the "YES" option in the ConfirmDialog. | |
if (option == JOptionPane.YES_OPTION){ | |
// Generate a random ladder between 15-25 in length. | |
int ladderLength = getNumberInRange(15, 25); | |
// Generate a random ladder angle between 60-75 degrees. | |
int angle = getNumberInRange(60, 75); | |
// Compute the random angle to radians | |
double radians = Math.toRadians(angle); | |
// Determine the height of the ladder leaned up against the house | |
// by multiplying the ladder's length by the Sine of the angle in radians. | |
double height = ladderLength * Math.sin(radians); | |
// Figure out the distance the ladder is away from the house by using Pythagorean theorem given that | |
// we know the hypotenuse and the length of one of the sides. | |
double distance = Math.sqrt(Math.pow(ladderLength, 2) - Math.pow(height, 2)); | |
// Out put the information we computed above. | |
System.out.printf("\nThe length of the ladder length is %d.", ladderLength); | |
System.out.printf("\nThe angle is %d degrees.", angle); | |
System.out.printf("\nThe angle in radians is %.1f", radians); | |
System.out.printf("\nThe height of ladder up the house is %.1f ", height); | |
System.out.printf("\nThe ladder's distance from house is %.1f ", distance); | |
} else { | |
// Display a MessageDialog if the user selected "NO" on the ConfirmDialog, | |
// letting them know that "No ladder is needed" | |
JOptionPane.showMessageDialog( | |
null, | |
"No ladder needed" | |
); | |
} | |
} | |
/** | |
* | |
* @param minimum: the smallest value within the range of numbers | |
* @param maximum: the largest value within the range of numbers | |
* @return a random number between the minimum and maximum | |
*/ | |
public static int getNumberInRange(int minimum, int maximum) { | |
// Make Java have an anxiety attack if the minimum value is greater than the maximum value. | |
if (minimum >= maximum) { | |
throw new IllegalArgumentException("The maximum value must be greater than minimum."); | |
} | |
Random r = new Random(); | |
return r.nextInt((maximum - minimum) + 1) + minimum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the breakdown.
Figuring out the radians is easy as pie. Simply pass the randomly generated angle into the
Math.toRadians()
method. It will return the radians as a double (so be sure you save it to a variable of typedouble
.)The height of the ladder against the side of the house can be determined by multiplying the length of the ladder (which is the hypotenuse) by the Sin of our angle, in radians.
The distance of the ladder away from the house is a little more challenging to determine. You need to use the Pythagorean theorem to solve it.
Where:
Seeing that we know:
we can now solve for
a
in the formula. The value ofa
is equal to the hypotenuse squared, minus the height of the ladder against the house, also squared.