Skip to content

Commit c483e99

Browse files
committed
import version 6.1.2
1 parent ec2b72c commit c483e99

143 files changed

Lines changed: 2747 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ap01/Series.java

Lines changed: 25 additions & 0 deletions
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+
}

ap01/SeriesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import junit.framework.TestCase;
2+
3+
/**
4+
* Example JUnit test from Appendix A.
5+
*/
6+
public class SeriesTest extends TestCase {
7+
8+
public void testFibonacci() {
9+
assertEquals(1, Series.fibonacci(1));
10+
assertEquals(1, Series.fibonacci(2));
11+
assertEquals(2, Series.fibonacci(3));
12+
}
13+
14+
}

ap02/Drawing.java

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

ap02/Mickey.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.awt.Canvas;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.awt.Rectangle;
5+
import javax.swing.JFrame;
6+
7+
public class Mickey extends Canvas {
8+
9+
public static void main(String[] args) {
10+
JFrame frame = new JFrame("Mickey Mouse");
11+
Canvas canvas = new Mickey();
12+
canvas.setSize(400, 400);
13+
canvas.setBackground(Color.white);
14+
frame.add(canvas);
15+
frame.pack();
16+
frame.setVisible(true);
17+
}
18+
19+
public void paint(Graphics g) {
20+
Rectangle bb = new Rectangle(100, 100, 200, 200);
21+
mickey(g, bb);
22+
}
23+
24+
public void boxOval(Graphics g, Rectangle bb) {
25+
g.fillOval(bb.x, bb.y, bb.width, bb.height);
26+
}
27+
28+
public void mickey(Graphics g, Rectangle bb) {
29+
boxOval(g, bb);
30+
31+
int dx = bb.width / 2;
32+
int dy = bb.height / 2;
33+
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
34+
35+
half.translate(-dx / 2, -dy / 2);
36+
boxOval(g, half);
37+
38+
half.translate(dx * 2, 0);
39+
boxOval(g, half);
40+
}
41+
42+
}

ap02/Moire.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.awt.Canvas;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import javax.swing.JFrame;
5+
6+
public class Moire extends Canvas {
7+
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+
}
17+
18+
public void paint(Graphics g) {
19+
int i = 90;
20+
while (i < getWidth()) {
21+
g.drawOval(0, 0, i, i);
22+
i = i + 3;
23+
}
24+
}
25+
26+
}

ch01/Goodbye.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Example program that demonstrates print vs println.
3+
*/
4+
public class Goodbye {
5+
6+
/**
7+
* Prints a greeting.
8+
*/
9+
public static void main(String[] args) {
10+
System.out.print("Goodbye, "); // note the space
11+
System.out.println("cruel world");
12+
}
13+
14+
}

ch01/Hello.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Hello {
2+
3+
public static void main(String[] args) {
4+
// generate some simple output
5+
System.out.println("Hello, World!");
6+
}
7+
8+
}

ch02/Variables.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Examples from Chapter 2.
3+
*/
4+
public class Variables {
5+
6+
public static void main(String[] args) {
7+
8+
String message;
9+
int x;
10+
11+
String firstName;
12+
String lastName;
13+
int hour, minute;
14+
15+
message = "Hello!"; // give message the value "Hello!"
16+
hour = 11; // assign the value 11 to hour
17+
minute = 59; // set minute to 59
18+
19+
message = "123"; // legal
20+
// message = 123; // not legal
21+
22+
String message2 = "Hello!";
23+
int hour2 = 11;
24+
int minute2 = 59;
25+
26+
int a = 5;
27+
int b = a; // a and b are now equal
28+
a = 3; // a and b are no longer equal
29+
30+
String firstLine = "Hello, again!";
31+
System.out.println(firstLine);
32+
33+
System.out.print("The value of firstLine is ");
34+
System.out.println(firstLine);
35+
36+
System.out.print("The current time is ");
37+
System.out.print(hour);
38+
System.out.print(":");
39+
System.out.print(minute);
40+
System.out.println(".");
41+
42+
System.out.print("Number of minutes since midnight: ");
43+
System.out.println(hour * 60 + minute);
44+
45+
System.out.print("Fraction of the hour that has passed: ");
46+
System.out.println(minute / 60);
47+
48+
System.out.print("Percent of the hour that has passed: ");
49+
System.out.println(minute * 100 / 60);
50+
51+
double pi;
52+
pi = 3.14159;
53+
54+
double minute3 = 59.0;
55+
System.out.print("Fraction of the hour that has passed: ");
56+
System.out.println(minute3 / 60.0);
57+
58+
double y = 1.0 / 3.0; // correct
59+
60+
System.out.println(0.1 * 10);
61+
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
62+
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
63+
64+
double balance = 123.45; // potential rounding error
65+
int balance2 = 12345; // total number of cents
66+
67+
System.out.println(1 + 2 + "Hello");
68+
// the output is 3Hello
69+
70+
System.out.println("Hello" + 1 + 2);
71+
// the output is Hello12
72+
73+
System.out.println(17 * 3);
74+
System.out.println(hour * 60 + minute);
75+
76+
int percentage;
77+
percentage = (minute * 100) / 60;
78+
79+
hour = minute + 1; // correct
80+
// minute + 1 = hour; // compiler error
81+
}
82+
83+
}

ch03/Convert.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Converts centimeters to feet and inches.
5+
*/
6+
public class Convert {
7+
8+
public static void main(String[] args) {
9+
double cm;
10+
int feet, inches, remainder;
11+
final double CM_PER_INCH = 2.54;
12+
final int IN_PER_FOOT = 12;
13+
Scanner in = new Scanner(System.in);
14+
15+
// prompt the user and get the value
16+
System.out.print("Exactly how many cm? ");
17+
cm = in.nextDouble();
18+
19+
// convert and output the result
20+
inches = (int) (cm / CM_PER_INCH);
21+
feet = inches / IN_PER_FOOT;
22+
remainder = inches % IN_PER_FOOT;
23+
System.out.printf("%.2f cm = %d ft, %d in\n",
24+
cm, feet, remainder);
25+
}
26+
27+
}

ch03/Echo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.Scanner;
2+
3+
public class Echo {
4+
5+
public static void main(String[] args) {
6+
String line;
7+
Scanner in = new Scanner(System.in);
8+
9+
System.out.print("Type something: ");
10+
line = in.nextLine();
11+
System.out.println("You said: " + line);
12+
13+
System.out.print("Type something else: ");
14+
line = in.nextLine();
15+
System.out.println("You also said: " + line);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)