-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTheSpeedOfSound.java
More file actions
63 lines (50 loc) · 2.27 KB
/
Copy pathTheSpeedOfSound.java
File metadata and controls
63 lines (50 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import javax.swing.JOptionPane;
import java.text.*;
/**@author Lemuel M. Uhuru
* @Date June 15, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 190 Problem 12: The Speed of Sound
*
* @Description Write a program that asks the user to enter
* "air", "water", or "steel", and the distance that a sound
* wave will travel in the medium. The program should then display
* the amount of time it will take.
*/
public class TheSpeedOfSound {
public static void main(String[] args) {
//Declare constants that approximate the
//speed of sound in Air, Steel, and Water
int AIR = 1100;
int WATER = 4900;
int STEEL = 16400;
//Declare variable to hold medium type and distance value.
String medium,
distance;
//Prompt user for medium
medium = JOptionPane.showInputDialog("Enter the medium (water/steel/air) ").toLowerCase();
//Prompt user for distance
distance = JOptionPane.showInputDialog("Enter distance in feet");
//Convert distance to data-type double
double D = Double.parseDouble(distance);
// Calculate time for sound to travel through medium
double timeA = D/AIR;
double timeW = D/WATER;
double timeS = D/STEEL;
// Decimal formating for Time variables
NumberFormat df = DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
//Design if-else control structure to display the amount
//of time it will take sound to travel through the medium given
//the users input.
// *Don't understand why the '==' or equal to operator cannot be used
// as a substitute for the equals method in the following conditions.
if(medium.equals("water"))
JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeW) + " seconds");
else if(medium.equals("air"))
JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeA) + " seconds");
else if(medium.equals("steel"))
JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeS) + " seconds");
else
JOptionPane.showMessageDialog(null, "Invalid input");
}
}