Skip to content

Commit aa70d99

Browse files
committed
review ch1-7
1 parent 5bc037b commit aa70d99

File tree

8 files changed

+86
-77
lines changed

8 files changed

+86
-77
lines changed

ch02/Variables.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public static void main(String[] args) {
1414
int hour, minute;
1515

1616
message = "Hello!"; // give message the value "Hello!"
17-
hour = 10; // assign the value 10 to hour
17+
hour = 11; // assign the value 11 to hour
1818
minute = 59; // set minute to 59
1919

20-
message = "123"; // legal
21-
// message = 123; not legal
20+
message = "123"; // legal
21+
// message = 123; // not legal
2222

2323
String message2 = "Hello!";
24-
int hour2 = 10;
24+
int hour2 = 11;
2525
int minute2 = 59;
2626

2727
int a = 5;
@@ -43,7 +43,6 @@ public static void main(String[] args) {
4343
System.out.print("Number of minutes since midnight: ");
4444
System.out.println(hour * 60 + minute);
4545

46-
4746
System.out.print("Fraction of the hour that has passed: ");
4847
System.out.println(minute / 60);
4948

@@ -61,10 +60,10 @@ public static void main(String[] args) {
6160

6261
System.out.println(0.1 * 10);
6362
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
64-
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
63+
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
6564

6665
double balance = 123.45; // potential rounding error
67-
int balance2 = 12345; // total number of cents
66+
int balance2 = 12345; // total number of cents
6867

6968
System.out.println(1 + 2 + "Hello");
7069
// the output is 3Hello
@@ -79,6 +78,6 @@ public static void main(String[] args) {
7978
percentage = (minute * 100) / 60;
8079

8180
hour = minute + 1; // correct
82-
// minute + 1 = hour; syntax error
81+
// minute + 1 = hour; // compiler error
8382
}
8483
}

ch03/GuessStarter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.util.Random;
22

33
/**
4-
* Starter code for the "guess my number" exercise.
4+
* Starter code for the "Guess My Number" exercise.
55
*/
66
public class GuessStarter {
77

ch06/Recursive.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static int prod(int m, int n) {
88
if (m == n) {
99
return n;
1010
} else {
11-
int recurse = prod(m, n-1);
11+
int recurse = prod(m, n - 1);
1212
int result = n * recurse;
1313
return result;
1414
}

ch06/Series.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public static void countup(int n) {
1212
}
1313
}
1414

15-
public static double area(double radius) {
16-
double area = Math.PI * radius * radius;
17-
return area;
15+
public static double calculateArea(double radius) {
16+
double result = Math.PI * radius * radius;
17+
return result;
1818
}
1919

20-
public static double area2(double radius) {
20+
public static double calculateArea2(double radius) {
2121
return Math.PI * radius * radius;
2222
}
2323

@@ -59,13 +59,13 @@ public static double absoluteValue(double x) {
5959
public static double circleArea
6060
(double xc, double yc, double xp, double yp) {
6161
double radius = distance(xc, yc, xp, yp);
62-
double area = area(radius);
62+
double area = calculateArea(radius);
6363
return area;
6464
}
6565

66-
public static double area
66+
public static double calculateArea
6767
(double xc, double yc, double xp, double yp) {
68-
return area(distance(xc, yc, xp, yp));
68+
return calculateArea(distance(xc, yc, xp, yp));
6969
}
7070

7171
/**
@@ -106,17 +106,17 @@ public static void main(String[] args) {
106106
countup(3);
107107
System.out.println("Have a nice day.");
108108

109-
System.out.println("area");
110-
System.out.println(area(3.0));
109+
System.out.println("calculateArea");
110+
System.out.println(calculateArea(3.0));
111111

112-
System.out.println("area2");
113-
System.out.println(area2(3.0));
112+
System.out.println("calculateArea2");
113+
System.out.println(calculateArea2(3.0));
114114

115115
System.out.println("circleArea");
116116
System.out.println(circleArea(1.0, 2.0, 4.0, 6.0));
117117

118-
System.out.println("area with 4 doubles");
119-
System.out.println(area(1.0, 2.0, 4.0, 6.0));
118+
System.out.println("calculateArea with 4 doubles");
119+
System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0));
120120

121121
System.out.println("absolute value");
122122
System.out.println(absoluteValue(-2));
@@ -133,11 +133,11 @@ public static void main(String[] args) {
133133
System.out.println(isSingleDigit(2));
134134
boolean bigFlag = !isSingleDigit2(17);
135135

136-
int i = 9;
137-
if (isSingleDigit(i)) {
138-
System.out.println("i is small");
136+
int z = 9;
137+
if (isSingleDigit(z)) {
138+
System.out.println("z is small");
139139
} else {
140-
System.out.println("i is big");
140+
System.out.println("z is big");
141141
}
142142

143143
System.out.println("factorial");

ch06/SeriesTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import junit.framework.TestCase;
22

3+
/**
4+
* Example JUnit test from Appendix A.
5+
*/
36
public class SeriesTest extends TestCase {
47

58
public void testFibonacci() {

ch07/Loops.java

+3-49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import java.util.Scanner;
2-
1+
/**
2+
* Examples from Chapter 7.
3+
*/
34
public class Loops {
45

56
public static void countdown(int n) {
@@ -21,58 +22,11 @@ public static void sequence(int n) {
2122
}
2223
}
2324

24-
public static double scanDouble() {
25-
Scanner in = new Scanner(System.in);
26-
boolean okay;
27-
do {
28-
System.out.print("Enter a number: ");
29-
if (in.hasNextDouble()) {
30-
okay = true;
31-
} else {
32-
okay = false;
33-
String word = in.next();
34-
System.err.println(word + " is not a number");
35-
}
36-
} while (!okay);
37-
double x = in.nextDouble();
38-
return x;
39-
}
40-
41-
public static double scanDouble2() {
42-
Scanner in = new Scanner(System.in);
43-
while (true) {
44-
System.out.print("Enter a number: ");
45-
if (in.hasNextDouble()) {
46-
break;
47-
}
48-
String word = in.next();
49-
System.err.println(word + " is not a number");
50-
}
51-
double x = in.nextDouble();
52-
return x;
53-
}
54-
55-
public static double addNumbers() {
56-
Scanner in = new Scanner(System.in);
57-
int x = -1;
58-
int sum = 0;
59-
while (x != 0) {
60-
x = in.nextInt();
61-
if (x <= 0) {
62-
continue;
63-
}
64-
System.out.println("Adding " + x);
65-
sum += x;
66-
}
67-
return sum;
68-
}
69-
7025
public static void main(String[] args) {
7126
System.out.println("countdown");
7227
countdown(3);
7328

7429
System.out.println("sequence");
7530
sequence(10);
7631
}
77-
7832
}

ch07/Tables.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Examples from Chapter 7.
2+
* Generating tables; encapsulation and generalization.
33
*/
44
public class Tables {
55

ch07/Validate.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Do-while, break, and continue.
5+
*/
6+
public class Validate {
7+
8+
public static double scanDouble() {
9+
Scanner in = new Scanner(System.in);
10+
boolean okay;
11+
do {
12+
System.out.print("Enter a number: ");
13+
if (in.hasNextDouble()) {
14+
okay = true;
15+
} else {
16+
okay = false;
17+
String word = in.next();
18+
System.err.println(word + " is not a number");
19+
}
20+
} while (!okay);
21+
double x = in.nextDouble();
22+
return x;
23+
}
24+
25+
public static double scanDouble2() {
26+
Scanner in = new Scanner(System.in);
27+
while (true) {
28+
System.out.print("Enter a number: ");
29+
if (in.hasNextDouble()) {
30+
break;
31+
}
32+
String word = in.next();
33+
System.err.println(word + " is not a number");
34+
}
35+
double x = in.nextDouble();
36+
return x;
37+
}
38+
39+
public static double addNumbers() {
40+
Scanner in = new Scanner(System.in);
41+
int x = -1;
42+
int sum = 0;
43+
while (x != 0) {
44+
x = in.nextInt();
45+
if (x <= 0) {
46+
continue;
47+
}
48+
System.out.println("Adding " + x);
49+
sum += x;
50+
}
51+
return sum;
52+
}
53+
}

0 commit comments

Comments
 (0)