-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab2.java
56 lines (42 loc) · 1.85 KB
/
Lab2.java
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
public class Lab2 {
public static void main(String[] args) {
System.out.println("As I was going to St. Ives");
System.out.println("I met a man with seven wives");
System.out.println("Each wife had seven sacks");
System.out.println("Each sack had seven cats");
System.out.println("Each cat had seven kits");
System.out.println("Kittens, cats, sacks, wives");
System.out.println("How many were going to St. Ives?");
// 1. Each of the following variable declarations has something
// wrong with it. Uncomment the line and fix it. If your code
// compiles, you are probably on the right track!
// Assume that the original man is also going to St. Ives, so
// the boolean variable manGoingToStIves should be true.
boolean manGoingToStIves = true;
int numWives = 7;
int numSacksPerWife = 7;
int numCatsPerSack = 7;
int numKitsPerCat = 7;
int total;
// If the original man is going to St. Ives, then all are going
// the narrator, original man, kitten, cats, sacks, wives.
// If the original man is going AWAY from St. Ives, then the only
// person going is the narrator
if (manGoingToStIves) {
// 2. Remember, if the original man is going to St. Ives, then
// all of the wives, sacks, cats, and kittens are as well, along
// with the narrator.
// Calculate the number going to St. Ives
total = 1+ numWives + (numWives*numSacksPerWife) + (numWives*numSacksPerWife*numCatsPerSack)
+ (numWives*numSacksPerWife*numCatsPerSack*numKitsPerCat);
} else {
// Otherwise, only the narrator is going!
total = 1;
}
// 3. Right before exiting, the system should print
// "Number going to St. Ives is " and then, on the same line,
// the number going to St. Ives (stored in the _total_ variable).
// Add a statement or statements that will do this.
System.out.println("Number going to St. Ives is " +total);
}
}