Skip to content

Commit 02c8171

Browse files
committed
chap 12
Chapter code and exercises through chapter 12
1 parent 3aed00b commit 02c8171

25 files changed

Lines changed: 695 additions & 100 deletions

‎.vscode/launch.json‎

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,45 @@
22
"configurations": [
33
{
44
"type": "java",
5-
"name": "CodeLens (Launch) - Tables",
5+
"name": "CodeLens (Launch) - encap",
66
"request": "launch",
7-
"mainClass": "Tables",
7+
"mainClass": "encap",
88
"projectName": "ThinkJavaCode_bf076222"
99
},
1010
{
1111
"type": "java",
12-
"name": "CodeLens (Launch) - Loops",
12+
"name": "CodeLens (Launch) - Format",
1313
"request": "launch",
14-
"mainClass": "Loops",
14+
"mainClass": "Format",
1515
"projectName": "ThinkJavaCode_bf076222"
1616
},
1717
{
1818
"type": "java",
19-
"name": "CodeLens (Launch) - Multadd",
19+
"name": "CodeLens (Launch) - ArrayExamples",
2020
"request": "launch",
21-
"mainClass": "Multadd",
21+
"mainClass": "ArrayExamples",
2222
"projectName": "ThinkJavaCode_bf076222"
2323
},
2424
{
2525
"type": "java",
26-
"name": "CodeLens (Launch) - testing",
26+
"name": "CodeLens (Launch) - math",
2727
"request": "launch",
28-
"mainClass": "testing",
28+
"mainClass": "math",
2929
"projectName": "ThinkJavaCode_bf076222"
3030
},
3131
{
3232
"type": "java",
3333
"name": "CodeLens (Launch) - Exercise",
3434
"request": "launch",
35-
"mainClass": "Exercise"
36-
},
37-
{
38-
"type": "java",
39-
"name": "CodeLens (Launch) - Series",
40-
"request": "launch",
41-
"mainClass": "Series"
42-
},
43-
{
44-
"type": "java",
45-
"name": "CodeLens (Launch) - Recursive",
46-
"request": "launch",
47-
"mainClass": "Recursive"
48-
},
49-
{
50-
"type": "java",
51-
"name": "CodeLens (Launch) - Buzz",
52-
"request": "launch",
53-
"mainClass": "Buzz"
54-
},
55-
{
56-
"type": "java",
57-
"name": "CodeLens (Launch) - CheckFermat",
58-
"request": "launch",
59-
"mainClass": "CheckFermat"
60-
},
61-
{
62-
"type": "java",
63-
"name": "CodeLens (Launch) - GuessStarter",
64-
"request": "launch",
65-
"mainClass": "GuessStarter",
66-
"console": "integratedTerminal"
67-
},
68-
{
69-
"type": "java",
70-
"name": "CodeLens (Launch) - Temp",
71-
"request": "launch",
72-
"mainClass": "Temp",
73-
"console": "integratedTerminal"
74-
},
75-
{
76-
"type": "java",
77-
"name": "CodeLens (Launch) - Echo",
78-
"request": "launch",
79-
"mainClass": "Echo"
80-
},
81-
{
82-
"type": "java",
83-
"name": "CodeLens (Launch) - Convert",
84-
"request": "launch",
85-
"mainClass": "Convert",
86-
"console": "integratedTerminal"
87-
},
88-
{
89-
"type": "java",
90-
"name": "CodeLens (Launch) - Time",
91-
"request": "launch",
92-
"mainClass": "Time"
93-
},
94-
{
95-
"type": "java",
96-
"name": "CodeLens (Launch) - Date",
97-
"request": "launch",
98-
"mainClass": "Date"
35+
"mainClass": "Exercise",
36+
"projectName": "ThinkJavaCode_bf076222"
9937
},
10038
{
10139
"type": "java",
102-
"name": "CodeLens (Launch) - Hello",
40+
"name": "CodeLens (Launch) - Tables",
10341
"request": "launch",
104-
"mainClass": "Hello"
42+
"mainClass": "Tables",
43+
"projectName": "ThinkJavaCode_bf076222"
10544
}
10645
]
10746
}

‎ap02/.vscode/launch.json‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - Moire",
6+
"request": "launch",
7+
"mainClass": "Moire"
8+
},
9+
{
10+
"type": "java",
11+
"name": "CodeLens (Launch) - Mickey",
12+
"request": "launch",
13+
"mainClass": "Mickey"
14+
},
15+
{
16+
"type": "java",
17+
"name": "CodeLens (Launch) - japan",
18+
"request": "launch",
19+
"mainClass": "japan"
20+
},
21+
{
22+
"type": "java",
23+
"name": "CodeLens (Launch) - Drawing",
24+
"request": "launch",
25+
"mainClass": "Drawing"
26+
}
27+
]
28+
}

‎ap02/Mickey.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ public void mickey(Graphics g, Rectangle bb) {
3030

3131
int dx = bb.width / 2;
3232
int dy = bb.height / 2;
33-
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
33+
if (dx > 3) {
34+
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
3435

35-
half.translate(-dx / 2, -dy / 2);
36-
boxOval(g, half);
36+
half.translate(-dx / 2, -dy / 2);
37+
mickey(g, half);
3738

38-
half.translate(dx * 2, 0);
39-
boxOval(g, half);
39+
half.translate(dx * 2, 0);
40+
mickey(g, half);
41+
42+
dx /= 2;
43+
dy /= 2;
44+
}
4045
}
4146

4247
}

‎ap02/Moire.java‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ public static void main(String[] args) {
1616
}
1717

1818
public void paint(Graphics g) {
19-
int i = 90;
20-
while (i < getWidth()) {
21-
g.drawOval(0, 0, i, i);
22-
i = i + 3;
19+
// int i = 90;
20+
// while (i < getWidth()) {
21+
// g.drawOval(getWidth() / 2 - i / 2, getHeight() / 2 - i / 2, i, i);
22+
// i = i + 3;
23+
// }
24+
radial(g);
25+
}
26+
27+
public void radial(Graphics g) {
28+
int i = 0;
29+
int j = 0;
30+
while (i <= getWidth() && j <= getHeight()) {
31+
g.drawLine(i, 0, getWidth() - i, getHeight());
32+
g.drawLine(0, j, getWidth(), getHeight() - j);
33+
i = i + 5;
34+
j = j + 5;
2335
}
36+
g.drawLine(0, getHeight(), getWidth(), 0);
37+
2438
}
2539

2640
}

‎ap02/japan.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.awt.Canvas;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import javax.swing.JFrame;
5+
6+
7+
public class japan extends Canvas {
8+
9+
public static void main(String[] args) {
10+
JFrame frame = new JFrame("Flag of Japan");
11+
Canvas japan = new japan();
12+
japan.setSize(600, 400);
13+
frame.add(japan);
14+
frame.pack();
15+
frame.setVisible(true);
16+
}
17+
18+
public void paint(Graphics g) {
19+
g.setColor(Color.red);
20+
g.fillOval(175, 75, 250, 250);
21+
}
22+
}

‎ch07/math.java‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public static class math {
2+
3+
public static void main(String args[]) {
4+
System.out.println(SquareRoot(16));
5+
System.out.println(power(3,0));
6+
System.out.println(factorial(0));
7+
check(1.0);
8+
}
9+
10+
public static double SquareRoot(double a) {
11+
double x = a/2;
12+
double y = a;
13+
while (Math.abs(x - y) > 0.0001) {
14+
y = x;
15+
x = (x + a/x)/2;
16+
}
17+
return x;
18+
}
19+
20+
public static double power(double x, int n) {
21+
double x0 = 1;
22+
for (int i=0; i<n; i++) {
23+
x0*=x;
24+
}
25+
return x0;
26+
}
27+
public static int factorial(int n) {
28+
int m = 1;
29+
for (int i = n; i > 0; i--) {
30+
m = m * i;
31+
}
32+
return m;
33+
}
34+
35+
public static double myexp(double x, int n) {
36+
double e = 1.0;
37+
double j = 1.0;
38+
for (int i=1; i<n; i++) {
39+
//e = e + (power(x,i)/factorial(i));
40+
j *= (x/i);
41+
e += j;
42+
}
43+
return e;
44+
}
45+
46+
public static double gauss(double x, int n) {
47+
double j = 1;
48+
double y = 1.0;
49+
for (int i = 1; i < n; i++) {
50+
j *= (-x / i) * x;
51+
y += j;
52+
}
53+
return y;
54+
}
55+
56+
public static void check(double x) {
57+
System.out.print(x + "\t");
58+
System.out.print(myexp(x, 18)+"\t");
59+
System.out.println(Math.exp(x));
60+
}
61+
}

‎ch08/.vscode/launch.json‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - MakeDubMus",
6+
"request": "launch",
7+
"mainClass": "MakeDubMus"
8+
},
9+
{
10+
"type": "java",
11+
"name": "CodeLens (Launch) - Histogram",
12+
"request": "launch",
13+
"mainClass": "Histogram"
14+
},
15+
{
16+
"type": "java",
17+
"name": "CodeLens (Launch) - encap",
18+
"request": "launch",
19+
"mainClass": "encap"
20+
},
21+
{
22+
"type": "java",
23+
"name": "CodeLens (Launch) - ArrayExamples",
24+
"request": "launch",
25+
"mainClass": "ArrayExamples"
26+
}
27+
]
28+
}

‎ch08/Fruit.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44
public class Fruit {
55

6-
public static int banana(int[] a) {
7-
int kiwi = 1;
6+
public static int banana(int[] a) { //Takes product of all elements
7+
int kiwi = 1; // Final product of all list elements
88
int i = 0;
99
while (i < a.length) {
1010
kiwi = kiwi * a[i];
@@ -13,7 +13,7 @@ public static int banana(int[] a) {
1313
return kiwi;
1414
}
1515

16-
public static int grapefruit(int[] a, int grape) {
16+
public static int grapefruit(int[] a, int grape) { // Searches for grape in a
1717
for (int i = 0; i < a.length; i++) {
1818
if (a[i] == grape) {
1919
return i;
@@ -22,8 +22,8 @@ public static int grapefruit(int[] a, int grape) {
2222
return -1;
2323
}
2424

25-
public static int pineapple(int[] a, int apple) {
26-
int pear = 0;
25+
public static int pineapple(int[] a, int apple) { // Counts the number of times apple appears in a
26+
int pear = 0; // Total number of appearances of apple in a
2727
for (int pine: a) {
2828
if (pine == apple) {
2929
pear++;

0 commit comments

Comments
 (0)