Skip to content

Commit ae811e0

Browse files
committed
review ap01..ch07
1 parent aa70d99 commit ae811e0

22 files changed

+78
-35
lines changed

ap01/Series.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Example method from Chapter 6.
3+
*/
4+
public class Series {
5+
6+
public static int fibonacci(int n) {
7+
if (n == 1 || n == 2) {
8+
return 1;
9+
}
10+
return fibonacci(n - 1) + fibonacci(n - 2);
11+
}
12+
13+
public static void main(String[] args) {
14+
if (fibonacci(1) != 1) {
15+
System.err.println("fibonacci(1) is incorrect");
16+
}
17+
if (fibonacci(2) != 1) {
18+
System.err.println("fibonacci(2) is incorrect");
19+
}
20+
if (fibonacci(3) != 2) {
21+
System.err.println("fibonacci(3) is incorrect");
22+
}
23+
}
24+
25+
}

ch06/SeriesTest.java renamed to ap01/SeriesTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public void testFibonacci() {
1010
assertEquals(1, Series.fibonacci(2));
1111
assertEquals(2, Series.fibonacci(3));
1212
}
13+
1314
}

ap02/Drawing.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
public class Drawing extends Canvas {
66

7-
// this is here to suppress a warning; you can read about it at
8-
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
9-
static final long serialVersionUID = 1;
10-
117
public static void main(String[] args) {
128
JFrame frame = new JFrame("My Drawing");
139
Canvas drawing = new Drawing();
@@ -20,4 +16,5 @@ public static void main(String[] args) {
2016
public void paint(Graphics g) {
2117
g.fillOval(100, 100, 200, 200);
2218
}
19+
2320
}

ap02/Mickey.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import java.awt.Canvas;
2+
import java.awt.Color;
23
import java.awt.Graphics;
34
import java.awt.Rectangle;
4-
55
import javax.swing.JFrame;
66

7-
87
public class Mickey extends Canvas {
98

10-
// this is here to suppress a warning; you can read about it at
11-
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
12-
static final long serialVersionUID = 1;
13-
149
public static void main(String[] args) {
15-
JFrame frame = new JFrame("My Drawing");
10+
JFrame frame = new JFrame("Mickey Mouse");
1611
Canvas canvas = new Mickey();
1712
canvas.setSize(400, 400);
13+
canvas.setBackground(Color.white);
1814
frame.add(canvas);
1915
frame.pack();
2016
frame.setVisible(true);
@@ -25,6 +21,10 @@ public void paint(Graphics g) {
2521
mickey(g, bb);
2622
}
2723

24+
public void boxOval(Graphics g, Rectangle bb) {
25+
g.fillOval(bb.x, bb.y, bb.width, bb.height);
26+
}
27+
2828
public void mickey(Graphics g, Rectangle bb) {
2929
boxOval(g, bb);
3030

@@ -39,7 +39,4 @@ public void mickey(Graphics g, Rectangle bb) {
3939
boxOval(g, half);
4040
}
4141

42-
public void boxOval(Graphics g, Rectangle bb) {
43-
g.fillOval(bb.x, bb.y, bb.width, bb.height);
44-
}
4542
}

ap02/Moire.java

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import java.awt.Canvas;
22
import java.awt.Color;
33
import java.awt.Graphics;
4-
54
import javax.swing.JFrame;
65

7-
86
public class Moire extends Canvas {
97

10-
// this is here to suppress a warning; you can read about it at
11-
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
12-
static final long serialVersionUID = 1;
8+
public static void main(String[] args) {
9+
JFrame frame = new JFrame("Moire Pattern");
10+
Canvas canvas = new Moire();
11+
canvas.setSize(400, 400);
12+
canvas.setBackground(Color.white);
13+
frame.add(canvas);
14+
frame.pack();
15+
frame.setVisible(true);
16+
}
1317

1418
public void paint(Graphics g) {
1519
int i = 90;
@@ -19,19 +23,4 @@ public void paint(Graphics g) {
1923
}
2024
}
2125

22-
public static void main(String[] args) {
23-
// make the frame
24-
JFrame frame = new JFrame();
25-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26-
27-
// add the canvas
28-
Canvas canvas = new Moire();
29-
canvas.setSize(400, 400);
30-
canvas.setBackground(Color.white);
31-
frame.getContentPane().add(canvas);
32-
33-
// show the frame
34-
frame.pack();
35-
frame.setVisible(true);
36-
}
3726
}

ch01/Goodbye.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public static void main(String[] args) {
1010
System.out.print("Goodbye, "); // note the space
1111
System.out.println("cruel world");
1212
}
13+
1314
}

ch01/Hello.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public static void main(String[] args) {
44
// generate some simple output
55
System.out.println("Hello, World!");
66
}
7+
78
}

ch02/Variables.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class Variables {
66
public static void main(String[] args) {
77

88
String message;
9-
109
int x;
1110

1211
String firstName;
@@ -80,4 +79,5 @@ public static void main(String[] args) {
8079
hour = minute + 1; // correct
8180
// minute + 1 = hour; // compiler error
8281
}
82+
8383
}

ch03/Convert.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ public static void main(String[] args) {
2323
System.out.printf("%.2f cm = %d ft, %d in\n",
2424
cm, feet, remainder);
2525
}
26+
2627
}

ch03/Echo.java

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public static void main(String[] args) {
1414
line = in.nextLine();
1515
System.out.println("You also said: " + line);
1616
}
17+
1718
}

ch03/GuessStarter.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public static void main(String[] args) {
1111
int number = random.nextInt(100) + 1;
1212
System.out.println(number);
1313
}
14+
1415
}

ch03/Input.java

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public static void main(String[] args) {
1212
double pi = 3.14159;
1313
double x = (int) pi * 20.0;
1414
}
15+
1516
}

ch03/ScannerBug.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ public static void main(String[] args) {
2222
name = in.nextLine();
2323
System.out.printf("Hello %s, age %d\n", name, age);
2424
}
25+
2526
}

ch04/Methods.java

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ public static void main(String[] args) {
1818
double x3 = Math.exp(Math.log(10.0));
1919
double x4 = Math.pow(2.0, 10.0);
2020
}
21+
2122
}

ch04/NewLine.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public static void main(String[] args) {
1515
threeLine();
1616
System.out.println("Second line.");
1717
}
18+
1819
}

ch04/PrintTime.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public static void main(String[] args) {
1111
int minute = 59;
1212
printTime(hour, minute);
1313
}
14+
1415
}

ch04/PrintTwice.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public static void printTwice(String s) {
88
public static void main(String[] args) {
99
printTwice("Don't make me say this twice!");
1010
}
11+
1112
}

ch06/Series.java

+1
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,5 @@ public static void main(String[] args) {
146146
System.out.println("fibonacci");
147147
System.out.println(fibonacci(3));
148148
}
149+
149150
}

ch07/Exercise.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Exercise {
2+
3+
public static void main(String[] args) {
4+
loop(10);
5+
}
6+
7+
public static void loop(int n) {
8+
int i = n;
9+
while (i > 1) {
10+
System.out.println(i);
11+
if (i % 2 == 0) {
12+
i = i / 2;
13+
} else {
14+
i = i + 1;
15+
}
16+
}
17+
}
18+
19+
}

ch07/Loops.java

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ public static void main(String[] args) {
2929
System.out.println("sequence");
3030
sequence(10);
3131
}
32+
3233
}

ch07/Tables.java

+1
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,5 @@ public static void main(String[] args) {
153153
System.out.println("printTable4");
154154
printTable4(6);
155155
}
156+
156157
}

ch07/Validate.java

+1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ public static double addNumbers() {
5050
}
5151
return sum;
5252
}
53+
5354
}

0 commit comments

Comments
 (0)