Expand|Select|Wrap|Line Numbers
- import javax.swing.*; // gets option pane
- import java.text.DecimalFormat; //formats decimals
- public class Lab8 {
- public static final int G = 32; //a constant
- public static double waveSpeed; //a class double variable
- public static String userInput;
- // method that generates a random wave
- public static double randomWaveSpeed () {
- int depth = randomNumber(1, 20);
- int length = randomNumber(1, 15);
- waveSpeed = depth / length;
- // determining which wave is selected
- if (depth >= .5){
- return deepWaveSpeed(length);
- }
- else
- return shallowWaveSpeed(depth);
- }
- // method that generates a random number
- public static int randomNumber(int low , int hi ){
- return (low + (int)(Math.random() * hi));
- }
- // method that determines the speed of a wave in shallow water
- public static double shallowWaveSpeed(int depth ){
- return Math.sqrt((G*depth));
- }
- // method that determines the speed of a wave in deep water
- public static double deepWaveSpeed(int length ){
- return Math.sqrt((G*length)/(2*Math.PI));
- }
- // method that displays whether or not the user caught the wave from
- // the paddle speed entered
- public static void displayRideOutcome(double paddleSpeed ){
- String output;
- if(paddleSpeed > waveSpeed){
- DecimalFormat twoDigits = new DecimalFormat("0.00");
- // generating output
- output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
- output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";
- output += "You were able to catch the wave.";
- }
- else{
- DecimalFormat twoDigits = new DecimalFormat("0.00");
- // generating output
- output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
- output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";
- output += "You were not able to catch the wave.";
- }
- //outputing with JOptionPane
- JOptionPane.showMessageDialog(null, output, "Lab8 (Edward Powers)", JOptionPane.PLAIN_MESSAGE);
- }
- // main method
- public static void main (String[] args) {
- String userInput;
- double paddleSpeed;
- int option;
- // a do while loop til the user clicks no
- do {
- do{
- userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
- "Valid Range is (1.0 to 8.0)");
- // if statement to cancel prog with error message if the user click cancel
- if (userInput == null)
- {
- JOptionPane.showMessageDialog(null, "User clicked cancel. The program will exit.",
- "Lab8 (Edward Powers)", JOptionPane.ERROR_MESSAGE );
- System.exit(0);
- }
- paddleSpeed = Double.parseDouble(userInput);
- if (paddleSpeed < 1 | paddleSpeed > 8){
- userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
- "Valid Range is (1.0 to 8.0)");}
- else{
- displayRideOutcome ( paddleSpeed);
- }
- option = JOptionPane.showConfirmDialog(null, "Would you like to continue?", "Lab7 (Edward Powers)", JOptionPane.YES_NO_OPTION );
- }
- while (option == JOptionPane.YES_OPTION);
- }
- while ( paddleSpeed >= 1 | paddleSpeed <=8);
- }
- }