Skip to content

Commit 7c16d7e

Browse files
authored
Merge pull request #1 from AllenDowney/master
getting updates
2 parents 353123f + 7302e62 commit 7c16d7e

Some content is hidden

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

53 files changed

+683
-475
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Allen Downey
3+
Copyright (c) 2016 Allen Downey and Chris Mayfield
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ If you don't already have a GitHub account, you'll need to create one.
1313
After forking, you'll have your own repository on GitHub that you can use to keep track of code you write.
1414
Then you can ``clone'' the repository, which downloads a copy of the files to your computer.
1515

16-
* Or you could clone the repository without forking. If you choose this option, you don't need a GitHub account, but you won't be able to save your changes back in GitHub.
16+
* Alternatively, you could clone the repository without forking.
17+
If you choose this option, you don't need a GitHub account, but you won't be able to save your changes back in GitHub.
1718

1819
* If you don't want to use Git at all, you can download the code in a zip archive using the "Download ZIP" button on this page, or [this link](http://tinyurl.com/ThinkJavaCodeZip).
1920

20-
To clone a repository, you need a Git client installed on your computer. The URL of this repository is `https://github.com/AllenDowney/ThinkJavaCode.git`. If you use Git from the command line, you can clone it like this:
21+
To clone a repository, you need a Git client installed on your computer.
22+
The URL of this repository is `https://github.com/AllenDowney/ThinkJavaCode.git`.
23+
If you use Git from the command line, you can clone it like this:
2124

2225
git clone https://github.com/AllenDowney/ThinkJavaCode.git
2326

2427
After you clone the repository or unzip the zip file, you should have a directory called `ThinkJavaCode` with a subdirectory for each chapter in the book.
2528

26-
All the examples in this book were developed and tested using Java SE Development Kit 7.
29+
All examples in this book were developed and tested using Java SE Development Kit 8.
2730
If you are using a more recent version, the examples in this book should still work.
2831
If you are using an older version, some of them may not.
29-

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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
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() {
69
assertEquals(1, Series.fibonacci(1));
710
assertEquals(1, Series.fibonacci(2));
811
assertEquals(2, Series.fibonacci(3));
912
}
13+
1014
}

ap02/Drawing.java

Lines changed: 1 addition & 4 deletions
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

Lines changed: 7 additions & 10 deletions
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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
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;
1620
while (i < getWidth()) {
17-
g.drawOval (0, 0, i, i);
21+
g.drawOval(0, 0, i, i);
1822
i = i + 3;
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ public class Variables {
66
public static void main(String[] args) {
77

88
String message;
9-
109
int x;
1110

1211
String firstName;
1312
String lastName;
1413
int hour, minute;
1514

1615
message = "Hello!"; // give message the value "Hello!"
17-
hour = 10; // assign the value 10 to hour
16+
hour = 11; // assign the value 11 to hour
1817
minute = 59; // set minute to 59
1918

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

2322
String message2 = "Hello!";
24-
int hour2 = 10;
23+
int hour2 = 11;
2524
int minute2 = 59;
2625

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

46-
4745
System.out.print("Fraction of the hour that has passed: ");
4846
System.out.println(minute / 60);
4947

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

6260
System.out.println(0.1 * 10);
6361
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
64-
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
62+
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
6563

6664
double balance = 123.45; // potential rounding error
67-
int balance2 = 12345; // total number of cents
65+
int balance2 = 12345; // total number of cents
6866

6967
System.out.println(1 + 2 + "Hello");
7068
// the output is 3Hello
@@ -79,6 +77,7 @@ public static void main(String[] args) {
7977
percentage = (minute * 100) / 60;
8078

8179
hour = minute + 1; // correct
82-
// minute + 1 = hour; syntax error
80+
// minute + 1 = hour; // compiler error
8381
}
82+
8483
}

0 commit comments

Comments
 (0)