-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMassAndWeight.java
More file actions
46 lines (37 loc) · 1.34 KB
/
Copy pathMassAndWeight.java
File metadata and controls
46 lines (37 loc) · 1.34 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
import javax.swing.JOptionPane;
/**@author Lemuel M. Uhuru
* @Date June 8, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 189 Problem 5: Mass and Weight
*
* @Description Write a program that asks the user to enter
* an objects mass, and then calculate it's weight using the
* following formula: Weight = Mass X 9.8.
*/
public class MassAndWeight {
public static void main(String[] args) {
//Declare variable to hold user input
String input;
//Declare variable to hold the weight value in Newtons.
double weight;
//Prompt user for the objects mass
input = JOptionPane.showInputDialog(null, "Enter the objects mass");
//Convert user input to data-type double because
// JOptionPane returns data-type String.
double mass = Double.parseDouble(input);
//Calculate weight
weight = mass * 9.8;
//Design decision structure to determine if
//weight is 'too heavy' or 'too light'.
if(weight > 1000){
JOptionPane.showMessageDialog(null, "The object weighs " + weight + " newtons. It is too heavy.");
}
else if (weight < 10) {
JOptionPane.showMessageDialog(null, "The object weighs " + weight + " newtons. It is too light.");
}
// Optional branch
else{
JOptionPane.showMessageDialog(null, "The object weighs " + weight + " newtons.");
}
}
}