Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Allen Downey
Copyright (c) 2016 Allen Downey and Chris Mayfield

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ If you don't already have a GitHub account, you'll need to create one.
After forking, you'll have your own repository on GitHub that you can use to keep track of code you write.
Then you can ``clone'' the repository, which downloads a copy of the files to your computer.

* 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.
* Alternatively, 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.

* 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).

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:
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:

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

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.

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

25 changes: 25 additions & 0 deletions ap01/Series.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Example method from Chapter 6.
*/
public class Series {

public static int fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

public static void main(String[] args) {
if (fibonacci(1) != 1) {
System.err.println("fibonacci(1) is incorrect");
}
if (fibonacci(2) != 1) {
System.err.println("fibonacci(2) is incorrect");
}
if (fibonacci(3) != 2) {
System.err.println("fibonacci(3) is incorrect");
}
}

}
4 changes: 4 additions & 0 deletions ch06/SeriesTest.java → ap01/SeriesTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import junit.framework.TestCase;

/**
* Example JUnit test from Appendix A.
*/
public class SeriesTest extends TestCase {

public void testFibonacci() {
assertEquals(1, Series.fibonacci(1));
assertEquals(1, Series.fibonacci(2));
assertEquals(2, Series.fibonacci(3));
}

}
5 changes: 1 addition & 4 deletions ap02/Drawing.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

public class Drawing extends Canvas {

// this is here to suppress a warning; you can read about it at
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
static final long serialVersionUID = 1;

public static void main(String[] args) {
JFrame frame = new JFrame("My Drawing");
Canvas drawing = new Drawing();
Expand All @@ -20,4 +16,5 @@ public static void main(String[] args) {
public void paint(Graphics g) {
g.fillOval(100, 100, 200, 200);
}

}
17 changes: 7 additions & 10 deletions ap02/Mickey.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JFrame;


public class Mickey extends Canvas {

// this is here to suppress a warning; you can read about it at
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
static final long serialVersionUID = 1;

public static void main(String[] args) {
JFrame frame = new JFrame("My Drawing");
JFrame frame = new JFrame("Mickey Mouse");
Canvas canvas = new Mickey();
canvas.setSize(400, 400);
canvas.setBackground(Color.white);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
Expand All @@ -25,6 +21,10 @@ public void paint(Graphics g) {
mickey(g, bb);
}

public void boxOval(Graphics g, Rectangle bb) {
g.fillOval(bb.x, bb.y, bb.width, bb.height);
}

public void mickey(Graphics g, Rectangle bb) {
boxOval(g, bb);

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

public void boxOval(Graphics g, Rectangle bb) {
g.fillOval(bb.x, bb.y, bb.width, bb.height);
}
}
31 changes: 10 additions & 21 deletions ap02/Moire.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;


public class Moire extends Canvas {

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

public void paint(Graphics g) {
int i = 90;
while (i < getWidth()) {
g.drawOval (0, 0, i, i);
g.drawOval(0, 0, i, i);
i = i + 3;
}
}

public static void main(String[] args) {
// make the frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add the canvas
Canvas canvas = new Moire();
canvas.setSize(400, 400);
canvas.setBackground(Color.white);
frame.getContentPane().add(canvas);

// show the frame
frame.pack();
frame.setVisible(true);
}
}
1 change: 1 addition & 0 deletions ch01/Goodbye.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public static void main(String[] args) {
System.out.print("Goodbye, "); // note the space
System.out.println("cruel world");
}

}
1 change: 1 addition & 0 deletions ch01/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public static void main(String[] args) {
// generate some simple output
System.out.println("Hello, World!");
}

}
17 changes: 8 additions & 9 deletions ch02/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ public class Variables {
public static void main(String[] args) {

String message;

int x;

String firstName;
String lastName;
int hour, minute;

message = "Hello!"; // give message the value "Hello!"
hour = 10; // assign the value 10 to hour
hour = 11; // assign the value 11 to hour
minute = 59; // set minute to 59

message = "123"; // legal
// message = 123; not legal
message = "123"; // legal
// message = 123; // not legal

String message2 = "Hello!";
int hour2 = 10;
int hour2 = 11;
int minute2 = 59;

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


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

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

System.out.println(0.1 * 10);
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);

double balance = 123.45; // potential rounding error
int balance2 = 12345; // total number of cents
int balance2 = 12345; // total number of cents

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

hour = minute + 1; // correct
// minute + 1 = hour; syntax error
// minute + 1 = hour; // compiler error
}

}
1 change: 1 addition & 0 deletions ch03/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public static void main(String[] args) {
System.out.printf("%.2f cm = %d ft, %d in\n",
cm, feet, remainder);
}

}
1 change: 1 addition & 0 deletions ch03/Echo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public static void main(String[] args) {
line = in.nextLine();
System.out.println("You also said: " + line);
}

}
3 changes: 2 additions & 1 deletion ch03/GuessStarter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.Random;

/**
* Starter code for the "guess my number" exercise.
* Starter code for the "Guess My Number" exercise.
*/
public class GuessStarter {

Expand All @@ -11,4 +11,5 @@ public static void main(String[] args) {
int number = random.nextInt(100) + 1;
System.out.println(number);
}

}
1 change: 1 addition & 0 deletions ch03/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public static void main(String[] args) {
double pi = 3.14159;
double x = (int) pi * 20.0;
}

}
1 change: 1 addition & 0 deletions ch03/ScannerBug.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public static void main(String[] args) {
name = in.nextLine();
System.out.printf("Hello %s, age %d\n", name, age);
}

}
1 change: 1 addition & 0 deletions ch04/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public static void main(String[] args) {
double x3 = Math.exp(Math.log(10.0));
double x4 = Math.pow(2.0, 10.0);
}

}
1 change: 1 addition & 0 deletions ch04/NewLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public static void main(String[] args) {
threeLine();
System.out.println("Second line.");
}

}
1 change: 1 addition & 0 deletions ch04/PrintTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public static void main(String[] args) {
int minute = 59;
printTime(hour, minute);
}

}
1 change: 1 addition & 0 deletions ch04/PrintTwice.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public static void printTwice(String s) {
public static void main(String[] args) {
printTwice("Don't make me say this twice!");
}

}
2 changes: 0 additions & 2 deletions ch05/Conditional.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.util.Scanner;

/**
* Examples from Chapter 5.
*/
Expand Down
2 changes: 1 addition & 1 deletion ch05/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {

public static void clink(int fork) {
System.out.print("It's ");
zoop("breakfast ", fork) ;
zoop("breakfast ", fork);
}

public static void ping(String strangStrung) {
Expand Down
12 changes: 6 additions & 6 deletions ch05/Logarithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public static void main(String[] args) {
System.out.println("printLogarithm");
printLogarithm(3.0);

Scanner in = new Scanner(System.in);

System.out.println("scandouble");
scanDouble();
scanDouble(in);

System.out.println("scandouble2");
scanDouble2();
scanDouble2(in);
}

public static void printLogarithm(double x) {
Expand All @@ -22,15 +24,13 @@ public static void printLogarithm(double x) {
System.out.println("The log of x is " + result);
}

public static void scanDouble() {
Scanner in = new Scanner(System.in);
public static void scanDouble(Scanner in) {
System.out.print("Enter a number: ");
double x = in.nextDouble();
printLogarithm(x);
}

public static void scanDouble2() {
Scanner in = new Scanner(System.in);
public static void scanDouble2(Scanner in) {
System.out.print("Enter a number: ");
if (!in.hasNextDouble()) {
String word = in.next();
Expand Down
2 changes: 1 addition & 1 deletion ch06/Recursive.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static int prod(int m, int n) {
if (m == n) {
return n;
} else {
int recurse = prod(m, n-1);
int recurse = prod(m, n - 1);
int result = n * recurse;
return result;
}
Expand Down
Loading