Skip to content

Commit

Permalink
review ap01..ch07
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Apr 29, 2016
1 parent aa70d99 commit ae811e0
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 35 deletions.
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");
}
}

}
1 change: 1 addition & 0 deletions ch06/SeriesTest.java → ap01/SeriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public void testFibonacci() {
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);
}
}
29 changes: 9 additions & 20 deletions ap02/Moire.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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;
Expand All @@ -19,19 +23,4 @@ public void paint(Graphics g) {
}
}

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!");
}

}
2 changes: 1 addition & 1 deletion ch02/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class Variables {
public static void main(String[] args) {

String message;

int x;

String firstName;
Expand Down Expand Up @@ -80,4 +79,5 @@ public static void main(String[] args) {
hour = minute + 1; // correct
// 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);
}

}
1 change: 1 addition & 0 deletions ch03/GuessStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

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

}
19 changes: 19 additions & 0 deletions ch07/Exercise.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Exercise {

public static void main(String[] args) {
loop(10);
}

public static void loop(int n) {
int i = n;
while (i > 1) {
System.out.println(i);
if (i % 2 == 0) {
i = i / 2;
} else {
i = i + 1;
}
}
}

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

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

}
1 change: 1 addition & 0 deletions ch07/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public static double addNumbers() {
}
return sum;
}

}

0 comments on commit ae811e0

Please sign in to comment.