From cd34cf8bdd70786ce62d4bc63d6a9eb6dacc3ecd Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 8 Mar 2016 15:04:24 -0500 Subject: [PATCH 01/24] ch03 sync w/book --- ch03/Convert.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ch03/Convert.java b/ch03/Convert.java index 5c93d3d..92fc06b 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -8,7 +8,8 @@ public class Convert { public static void main(String[] args) { double cm; int feet, inches, remainder; - final double centPerInch = 2.54; + final double CM_PER_INCH = 2.54; + final int IN_PER_FOOT = 12; Scanner in = new Scanner(System.in); // prompt the user and get the value @@ -16,9 +17,9 @@ public static void main(String[] args) { cm = in.nextDouble(); // convert and output the result - inches = (int) (cm / centPerInch); - feet = inches / 12; - remainder = inches % 12; + inches = (int) (cm / CM_PER_INCH); + feet = inches / IN_PER_FOOT; + remainder = inches % IN_PER_FOOT; System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, remainder); } From c560ff5757c1067e2aab9f72461e7346dfdc0f43 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 8 Mar 2016 19:23:43 -0500 Subject: [PATCH 02/24] read through ch04 --- ch04/Exercise.java | 25 +++++++++++++++++++++++++ ch04/Methods.java | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ch04/Exercise.java diff --git a/ch04/Exercise.java b/ch04/Exercise.java new file mode 100644 index 0000000..3db4c56 --- /dev/null +++ b/ch04/Exercise.java @@ -0,0 +1,25 @@ +public class Exercise { + + public static void zoop() { + baffle(); + System.out.print("You wugga "); + baffle(); + } + + public static void main(String[] args) { + System.out.print("No, I "); + zoop(); + System.out.print("I "); + baffle(); + } + + public static void baffle() { + System.out.print("wug"); + ping(); + } + + public static void ping() { + System.out.println("."); + } + +} diff --git a/ch04/Methods.java b/ch04/Methods.java index c6fa25e..c03c5ac 100644 --- a/ch04/Methods.java +++ b/ch04/Methods.java @@ -13,7 +13,8 @@ public static void main(String[] args) { double radians = Math.toRadians(180.0); double degrees2 = Math.toDegrees(Math.PI); long x = Math.round(Math.PI * 20.0); - double x2 = Math.cos(angle + Math.PI / 2); + + double x2 = Math.cos(angle + Math.PI / 2.0); double x3 = Math.exp(Math.log(10.0)); double x4 = Math.pow(2.0, 10.0); } From 6b00a8bad859f490734fe3ab05779bdd69eca2e2 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 8 Mar 2016 20:52:52 -0500 Subject: [PATCH 03/24] read through ch05 --- ch05/Conditional.java | 121 ++++-------------------------------------- ch05/Exercise.java | 28 ++++++++++ ch05/Logarithm.java | 44 +++++++++++++++ ch05/Recursion.java | 71 +++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 111 deletions(-) create mode 100644 ch05/Exercise.java create mode 100644 ch05/Logarithm.java create mode 100644 ch05/Recursion.java diff --git a/ch05/Conditional.java b/ch05/Conditional.java index 61b3485..eb2224d 100644 --- a/ch05/Conditional.java +++ b/ch05/Conditional.java @@ -4,25 +4,25 @@ * Examples from Chapter 5. */ public class Conditional { - + public static void main(String[] args) { String fruit1 = "Apple"; String fruit2 = "Orange"; System.out.println(fruit1.equals(fruit2)); - + int x = 17; int n = 18; - + if (x > 0) { System.out.println("x is positive"); } - + if (x % 2 == 0) { System.out.println("x is even"); } else { System.out.println("x is odd"); } - + if (x > 0) { System.out.println("x is positive"); } else if (x < 0) { @@ -30,7 +30,7 @@ public static void main(String[] args) { } else { System.out.println("x is zero"); } - + if (x == 0) { System.out.println("x is zero"); } else { @@ -40,118 +40,17 @@ public static void main(String[] args) { System.out.println("x is negative"); } } + boolean evenFlag = (n % 2 == 0); // true if n is even boolean positiveFlag = (x > 0); // true if x is positive - + if (evenFlag) { System.out.println("n was even when I checked it"); } - + if (!evenFlag) { System.out.println("n was odd when I checked it"); } - - System.out.println("printLogarithm"); - printLogarithm(3.0); - - System.out.println("countdown"); - countdown(3); - - System.out.println("countup"); - countup(3); - - System.out.println("newLine"); - newLine(); - - System.out.println("nLines"); - nLines(3); - - System.out.println("threeLine"); - threeLine(); - - System.out.println("displayBinary"); - displayBinary(23); - System.out.println(); - - System.out.println("scandouble"); - scanDouble(); - - System.out.println("scandouble2"); - scanDouble2(); - } - - public static void printLogarithm(double x) { - if (x <= 0.0) { - System.err.println("Error: x must be positive."); - return; - } - double result = Math.log(x); - System.out.println("The log of x is " + result); - } - - public static void scanDouble() { - Scanner in = new Scanner(System.in); - System.out.print("Enter a number: "); - double x = in.nextDouble(); - printLogarithm(x); - } - - public static void scanDouble2() { - Scanner in = new Scanner(System.in); - System.out.print("Enter a number: "); - if (!in.hasNextDouble()) { - String word = in.next(); - System.err.println(word + " is not a number"); - return; - } - double x = in.nextDouble(); - printLogarithm(x); - } - - public static void countdown(int n) { - if (n == 0) { - System.out.println("Blastoff!"); - } else { - System.out.println(n); - countdown(n - 1); - } - } - - public static void newLine() { - System.out.println(); - } - - public static void threeLine() { - newLine(); - newLine(); - newLine(); - } - - public static void nLines(int n) { - if (n > 0) { - System.out.println(); - nLines(n - 1); - } - } - - public static void forever(String s) { - System.out.println(s); - forever(s); - } - - public static void countup(int n) { - if (n == 0) { - System.out.println("Blastoff!"); - } else { - countup(n - 1); - System.out.println(n); - } - } - - public static void displayBinary(int value) { - if (value > 0) { - displayBinary(value / 2); - System.out.print(value % 2); - } } + } diff --git a/ch05/Exercise.java b/ch05/Exercise.java new file mode 100644 index 0000000..410de7a --- /dev/null +++ b/ch05/Exercise.java @@ -0,0 +1,28 @@ +public class Exercise { + + public static void zoop(String fred, int bob) { + System.out.println(fred); + if (bob == 5) { + ping("not "); + } else { + System.out.println("!"); + } + } + + public static void main(String[] args) { + int bizz = 5; + int buzz = 2; + zoop("just for", bizz); + clink(2 * buzz); + } + + public static void clink(int fork) { + System.out.print("It's "); + zoop("breakfast ", fork) ; + } + + public static void ping(String strangStrung) { + System.out.println("any " + strangStrung + "more "); + } + +} diff --git a/ch05/Logarithm.java b/ch05/Logarithm.java new file mode 100644 index 0000000..1784eea --- /dev/null +++ b/ch05/Logarithm.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +public class Logarithm { + + public static void main(String[] args) { + System.out.println("printLogarithm"); + printLogarithm(3.0); + + System.out.println("scandouble"); + scanDouble(); + + System.out.println("scandouble2"); + scanDouble2(); + } + + public static void printLogarithm(double x) { + if (x <= 0.0) { + System.err.println("Error: x must be positive."); + return; + } + double result = Math.log(x); + System.out.println("The log of x is " + result); + } + + public static void scanDouble() { + Scanner in = new Scanner(System.in); + System.out.print("Enter a number: "); + double x = in.nextDouble(); + printLogarithm(x); + } + + public static void scanDouble2() { + Scanner in = new Scanner(System.in); + System.out.print("Enter a number: "); + if (!in.hasNextDouble()) { + String word = in.next(); + System.err.println(word + " is not a number"); + return; + } + double x = in.nextDouble(); + printLogarithm(x); + } + +} diff --git a/ch05/Recursion.java b/ch05/Recursion.java new file mode 100644 index 0000000..ef1383f --- /dev/null +++ b/ch05/Recursion.java @@ -0,0 +1,71 @@ +public class Recursion { + + public static void main(String[] args) { + System.out.println("countdown"); + countdown(3); + + System.out.println("countup"); + countup(3); + + System.out.println("newLine"); + newLine(); + + System.out.println("nLines"); + nLines(3); + + System.out.println("threeLine"); + threeLine(); + + System.out.println("displayBinary"); + displayBinary(23); + System.out.println(); + } + + public static void countdown(int n) { + if (n == 0) { + System.out.println("Blastoff!"); + } else { + System.out.println(n); + countdown(n - 1); + } + } + + public static void newLine() { + System.out.println(); + } + + public static void threeLine() { + newLine(); + newLine(); + newLine(); + } + + public static void nLines(int n) { + if (n > 0) { + System.out.println(); + nLines(n - 1); + } + } + + public static void forever(String s) { + System.out.println(s); + forever(s); + } + + public static void countup(int n) { + if (n == 0) { + System.out.println("Blastoff!"); + } else { + countup(n - 1); + System.out.println(n); + } + } + + public static void displayBinary(int value) { + if (value > 0) { + displayBinary(value / 2); + System.out.print(value % 2); + } + } + +} From 7fc0d151a0d6db7f2406d91e125ac0f089187270 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 8 Mar 2016 21:37:21 -0500 Subject: [PATCH 04/24] remove duplicate file --- ch06/ValueMethods.java | 113 ----------------------------------------- 1 file changed, 113 deletions(-) delete mode 100644 ch06/ValueMethods.java diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java deleted file mode 100644 index 3a54d1b..0000000 --- a/ch06/ValueMethods.java +++ /dev/null @@ -1,113 +0,0 @@ -public class ValueMethods { - - public static void countup(int n) { - if (n == 0) { - System.out.println("Blastoff!"); - } else { - countup(n - 1); - System.out.println(n); - } - } - - public static double area(double radius) { - double area = Math.PI * radius * radius; - return area; - } - - public static double area2(double radius) { - return Math.PI * radius * radius; - } - - public static double absoluteValue(double x) { - if (x < 0) { - return -x; - } else { - return x; - } - } - - public static double distance - (double x1, double y1, double x2, double y2) { - double dx = x2 - x1; - double dy = y2 - y1; - System.out.println("dx is " + dx); - System.out.println("dy is " + dy); - return 0.0; - } - - public static double distance2 - (double x1, double y1, double x2, double y2) { - double dx = x2 - x1; - double dy = y2 - y1; - double dsquared = dx * dx + dy * dy; - System.out.println("dsquared is " + dsquared); - return 0.0; - } - - public static double distance3 - (double x1, double y1, double x2, double y2) { - double dx = x2 - x1; - double dy = y2 - y1; - double dsquared = dx * dx + dy * dy; - double result = Math.sqrt(dsquared); - return result; - } - - public static double circleArea - (double xc, double yc, double xp, double yp) { - double radius = distance(xc, yc, xp, yp); - double area = area(radius); - return area; - } - - public static double area - (double xc, double yc, double xp, double yp) { - return area(distance(xc, yc, xp, yp)); - } - - public static boolean isSingleDigit(int x) { - if (x >= 0 && x < 10) { - return true; - } else { - return false; - } - } - - public static boolean isSingleDigit2(int x) { - return x >= 0 && x < 10; - } - - public static int factorial(int n) { - if (n == 0) { - return 1; - } - int recurse = factorial(n - 1); - int result = n * recurse; - return result; - } - - 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) { - countup(3); - System.out.println("Have a nice day."); - - double x = area(3.0); - double y = area(1.0, 2.0, 4.0, 6.0); - - System.out.println(isSingleDigit(2)); - boolean bigFlag = !isSingleDigit(17); - - int z = 17; - if (isSingleDigit(z)) { - System.out.println("z is small"); - } else { - System.out.println("z is big"); - } - } -} From b006db326a4470d45f9a5cdcffe727558f8b8972 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 8 Mar 2016 22:01:02 -0500 Subject: [PATCH 05/24] read through ch06 --- ch06/Exercise.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ch06/Exercise.java diff --git a/ch06/Exercise.java b/ch06/Exercise.java new file mode 100644 index 0000000..9bd8c08 --- /dev/null +++ b/ch06/Exercise.java @@ -0,0 +1,36 @@ +public class Exercise { + + public static void main(String[] args) { + boolean flag1 = isHoopy(202); + boolean flag2 = isFrabjuous(202); + System.out.println(flag1); + System.out.println(flag2); + if (flag1 && flag2) { + System.out.println("ping!"); + } + if (flag1 || flag2) { + System.out.println("pong!"); + } + } + + public static boolean isHoopy(int x) { + boolean hoopyFlag; + if (x % 2 == 0) { + hoopyFlag = true; + } else { + hoopyFlag = false; + } + return hoopyFlag; + } + + public static boolean isFrabjuous(int x) { + boolean frabjuousFlag; + if (x > 0) { + frabjuousFlag = true; + } else { + frabjuousFlag = false; + } + return frabjuousFlag; + } + +} From 2b9b3d60bdd8f916608e712090a213fd7f84fabe Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 11:05:34 -0500 Subject: [PATCH 06/24] added missing exercises --- ch05/Buzz.java | 22 ++++++++++++++++++++++ ch06/Recursive.java | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 ch05/Buzz.java create mode 100644 ch06/Recursive.java diff --git a/ch05/Buzz.java b/ch05/Buzz.java new file mode 100644 index 0000000..95e1c2a --- /dev/null +++ b/ch05/Buzz.java @@ -0,0 +1,22 @@ +public class Buzz { + + public static void baffle(String blimp) { + System.out.println(blimp); + zippo("ping", -5); + } + + public static void zippo(String quince, int flag) { + if (flag < 0) { + System.out.println(quince + " zoop"); + } else { + System.out.println("ik"); + baffle(quince); + System.out.println("boo-wa-ha-ha"); + } + } + + public static void main(String[] args) { + zippo("rattle", 13); + } + +} diff --git a/ch06/Recursive.java b/ch06/Recursive.java new file mode 100644 index 0000000..0d29924 --- /dev/null +++ b/ch06/Recursive.java @@ -0,0 +1,17 @@ +public class Recursive { + + public static void main(String[] args) { + System.out.println(prod(1, 4)); + } + + public static int prod(int m, int n) { + if (m == n) { + return n; + } else { + int recurse = prod(m, n-1); + int result = n * recurse; + return result; + } + } + +} From d48e1210bfdc76a0515f20bc31a2b125acfadc42 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 11:06:01 -0500 Subject: [PATCH 07/24] consistent whitespace --- ch04/Exercise.java | 10 +++++----- ch04/Methods.java | 2 +- ch05/Conditional.java | 20 ++++++++++---------- ch05/Exercise.java | 10 +++++----- ch05/Logarithm.java | 14 +++++++------- ch05/Recursion.java | 28 ++++++++++++++-------------- ch06/Exercise.java | 8 ++++---- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/ch04/Exercise.java b/ch04/Exercise.java index 3db4c56..ba8c2a9 100644 --- a/ch04/Exercise.java +++ b/ch04/Exercise.java @@ -1,25 +1,25 @@ public class Exercise { - + public static void zoop() { baffle(); System.out.print("You wugga "); baffle(); } - + public static void main(String[] args) { System.out.print("No, I "); zoop(); System.out.print("I "); baffle(); } - + public static void baffle() { System.out.print("wug"); ping(); } - + public static void ping() { System.out.println("."); } - + } diff --git a/ch04/Methods.java b/ch04/Methods.java index c03c5ac..035a392 100644 --- a/ch04/Methods.java +++ b/ch04/Methods.java @@ -13,7 +13,7 @@ public static void main(String[] args) { double radians = Math.toRadians(180.0); double degrees2 = Math.toDegrees(Math.PI); long x = Math.round(Math.PI * 20.0); - + double x2 = Math.cos(angle + Math.PI / 2.0); double x3 = Math.exp(Math.log(10.0)); double x4 = Math.pow(2.0, 10.0); diff --git a/ch05/Conditional.java b/ch05/Conditional.java index eb2224d..1fd36ce 100644 --- a/ch05/Conditional.java +++ b/ch05/Conditional.java @@ -4,25 +4,25 @@ * Examples from Chapter 5. */ public class Conditional { - + public static void main(String[] args) { String fruit1 = "Apple"; String fruit2 = "Orange"; System.out.println(fruit1.equals(fruit2)); - + int x = 17; int n = 18; - + if (x > 0) { System.out.println("x is positive"); } - + if (x % 2 == 0) { System.out.println("x is even"); } else { System.out.println("x is odd"); } - + if (x > 0) { System.out.println("x is positive"); } else if (x < 0) { @@ -30,7 +30,7 @@ public static void main(String[] args) { } else { System.out.println("x is zero"); } - + if (x == 0) { System.out.println("x is zero"); } else { @@ -40,17 +40,17 @@ public static void main(String[] args) { System.out.println("x is negative"); } } - + boolean evenFlag = (n % 2 == 0); // true if n is even boolean positiveFlag = (x > 0); // true if x is positive - + if (evenFlag) { System.out.println("n was even when I checked it"); } - + if (!evenFlag) { System.out.println("n was odd when I checked it"); } } - + } diff --git a/ch05/Exercise.java b/ch05/Exercise.java index 410de7a..46774e0 100644 --- a/ch05/Exercise.java +++ b/ch05/Exercise.java @@ -1,5 +1,5 @@ public class Exercise { - + public static void zoop(String fred, int bob) { System.out.println(fred); if (bob == 5) { @@ -8,21 +8,21 @@ public static void zoop(String fred, int bob) { System.out.println("!"); } } - + public static void main(String[] args) { int bizz = 5; int buzz = 2; zoop("just for", bizz); clink(2 * buzz); } - + public static void clink(int fork) { System.out.print("It's "); zoop("breakfast ", fork) ; } - + public static void ping(String strangStrung) { System.out.println("any " + strangStrung + "more "); } - + } diff --git a/ch05/Logarithm.java b/ch05/Logarithm.java index 1784eea..bc21157 100644 --- a/ch05/Logarithm.java +++ b/ch05/Logarithm.java @@ -1,18 +1,18 @@ import java.util.Scanner; public class Logarithm { - + public static void main(String[] args) { System.out.println("printLogarithm"); printLogarithm(3.0); - + System.out.println("scandouble"); scanDouble(); - + System.out.println("scandouble2"); scanDouble2(); } - + public static void printLogarithm(double x) { if (x <= 0.0) { System.err.println("Error: x must be positive."); @@ -21,14 +21,14 @@ public static void printLogarithm(double x) { double result = Math.log(x); System.out.println("The log of x is " + result); } - + public static void scanDouble() { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); double x = in.nextDouble(); printLogarithm(x); } - + public static void scanDouble2() { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); @@ -40,5 +40,5 @@ public static void scanDouble2() { double x = in.nextDouble(); printLogarithm(x); } - + } diff --git a/ch05/Recursion.java b/ch05/Recursion.java index ef1383f..0e772c8 100644 --- a/ch05/Recursion.java +++ b/ch05/Recursion.java @@ -1,26 +1,26 @@ public class Recursion { - + public static void main(String[] args) { System.out.println("countdown"); countdown(3); - + System.out.println("countup"); countup(3); - + System.out.println("newLine"); newLine(); - + System.out.println("nLines"); nLines(3); - + System.out.println("threeLine"); threeLine(); - + System.out.println("displayBinary"); displayBinary(23); System.out.println(); } - + public static void countdown(int n) { if (n == 0) { System.out.println("Blastoff!"); @@ -29,29 +29,29 @@ public static void countdown(int n) { countdown(n - 1); } } - + public static void newLine() { System.out.println(); } - + public static void threeLine() { newLine(); newLine(); newLine(); } - + public static void nLines(int n) { if (n > 0) { System.out.println(); nLines(n - 1); } } - + public static void forever(String s) { System.out.println(s); forever(s); } - + public static void countup(int n) { if (n == 0) { System.out.println("Blastoff!"); @@ -60,12 +60,12 @@ public static void countup(int n) { System.out.println(n); } } - + public static void displayBinary(int value) { if (value > 0) { displayBinary(value / 2); System.out.print(value % 2); } } - + } diff --git a/ch06/Exercise.java b/ch06/Exercise.java index 9bd8c08..b826b44 100644 --- a/ch06/Exercise.java +++ b/ch06/Exercise.java @@ -1,5 +1,5 @@ public class Exercise { - + public static void main(String[] args) { boolean flag1 = isHoopy(202); boolean flag2 = isFrabjuous(202); @@ -12,7 +12,7 @@ public static void main(String[] args) { System.out.println("pong!"); } } - + public static boolean isHoopy(int x) { boolean hoopyFlag; if (x % 2 == 0) { @@ -22,7 +22,7 @@ public static boolean isHoopy(int x) { } return hoopyFlag; } - + public static boolean isFrabjuous(int x) { boolean frabjuousFlag; if (x > 0) { @@ -32,5 +32,5 @@ public static boolean isFrabjuous(int x) { } return frabjuousFlag; } - + } From fe4a34958400d84d800357c597aade8bacbb3b3d Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 13:29:42 -0500 Subject: [PATCH 08/24] added Javadoc comment --- ch06/Series.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ch06/Series.java b/ch06/Series.java index c256828..64ad34e 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -68,8 +68,14 @@ public static double absoluteValue(double x) { return area(distance(xc, yc, xp, yp)); } + /** + * Tests whether x is a single digit integer. + * + * @param x the integer to test + * @return true if x has one digit, false otherwise + */ public static boolean isSingleDigit(int x) { - if (x >= 0 && x < 10) { + if (x > -10 && x < 10) { return true; } else { return false; @@ -77,7 +83,7 @@ public static boolean isSingleDigit(int x) { } public static boolean isSingleDigit2(int x) { - return x >= 0 && x < 10; + return x > -10 && x < 10; } public static int factorial(int n) { From 98d82bc78675099b04c740c35e717e7efbe618f7 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 14:23:18 -0500 Subject: [PATCH 09/24] read through ch07 --- ch07/Tables.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch07/Tables.java b/ch07/Tables.java index 692ca39..57efe1d 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -41,11 +41,11 @@ public static void example2() { } public static void example3() { - final double log2 = Math.log(2); + final double LOG2 = Math.log(2); int i = 1; while (i < 100) { double x = (double) i; - System.out.println(x + " " + Math.log(x) / log2); + System.out.println(x + " " + Math.log(x) / LOG2); i = i * 2; } } From 2dd19cc7afc1d82a254d0ccef91344b46e3c51b8 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 17:30:27 -0500 Subject: [PATCH 10/24] added ch07 TODO sections --- ch07/Loops.java | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ ch07/Tables.java | 77 ++++++++++++++++------------------------------- 2 files changed, 104 insertions(+), 51 deletions(-) create mode 100644 ch07/Loops.java diff --git a/ch07/Loops.java b/ch07/Loops.java new file mode 100644 index 0000000..be02722 --- /dev/null +++ b/ch07/Loops.java @@ -0,0 +1,78 @@ +import java.util.Scanner; + +public class Loops { + + public static void countdown(int n) { + while (n > 0) { + System.out.println(n); + n = n - 1; + } + System.out.println("Blastoff!"); + } + + public static void sequence(int n) { + while (n != 1) { + System.out.println(n); + if (n % 2 == 0) { // n is even + n = n / 2; + } else { // n is odd + n = n * 3 + 1; + } + } + } + + public static double scanDouble() { + Scanner in = new Scanner(System.in); + boolean okay; + do { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + okay = true; + } else { + okay = false; + String word = in.next(); + System.err.println(word + " is not a number"); + } + } while (!okay); + double x = in.nextDouble(); + return x; + } + + public static double scanDouble2() { + Scanner in = new Scanner(System.in); + while (true) { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + break; + } + String word = in.next(); + System.err.println(word + " is not a number"); + } + double x = in.nextDouble(); + return x; + } + + public static double addNumbers() { + Scanner in = new Scanner(System.in); + int x = -1; + int sum = 0; + while (x != 0) { + x = in.nextInt(); + if (x <= 0) { + continue; + } + System.out.println("Adding " + x); + sum += x; + } + return sum; + } + + public static void main(String[] args) { + System.out.println("countdown"); + countdown(3); + + System.out.println("sequence"); + sequence(10); + } + +} diff --git a/ch07/Tables.java b/ch07/Tables.java index 57efe1d..4d5cd39 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -2,26 +2,7 @@ * Examples from Chapter 7. */ public class Tables { - - public static void countdown(int n) { - while (n > 0) { - System.out.println(n); - n = n - 1; - } - System.out.println("Blastoff!"); - } - - public static void sequence(int n) { - while (n != 1) { - System.out.println(n); - if (n % 2 == 0) { // n is even - n = n / 2; - } else { // n is odd - n = n * 3 + 1; - } - } - } - + public static void example() { int i = 1; while (i < 10) { @@ -30,7 +11,7 @@ public static void example() { i = i + 1; } } - + public static void example2() { int i = 1; while (i < 10) { @@ -39,7 +20,7 @@ public static void example2() { i = i + 1; } } - + public static void example3() { final double LOG2 = Math.log(2); int i = 1; @@ -49,7 +30,7 @@ public static void example3() { i = i * 2; } } - + public static void example4() { int i = 1; while (i <= 6) { @@ -58,7 +39,7 @@ public static void example4() { } System.out.println(); } - + public static void printRow() { int i = 1; while (i <= 6) { @@ -67,7 +48,7 @@ public static void printRow() { } System.out.println(); } - + public static void printRow2(int n) { int i = 1; while (i <= 6) { @@ -76,7 +57,7 @@ public static void printRow2(int n) { } System.out.println(); } - + public static void example5() { int i = 1; while (i <= 6) { @@ -84,7 +65,7 @@ public static void example5() { i = i + 1; } } - + public static void printTable() { int i = 1; while (i <= 6) { @@ -92,7 +73,7 @@ public static void printTable() { i = i + 1; } } - + public static void printTable2(int rows) { int i = 1; while (i <= rows) { @@ -100,7 +81,7 @@ public static void printTable2(int rows) { i = i + 1; } } - + public static void printRow3(int n, int cols) { int i = 1; while (i <= cols) { @@ -109,7 +90,7 @@ public static void printRow3(int n, int cols) { } System.out.println(); } - + public static void printTable3(int rows) { int i = 1; while (i <= rows) { @@ -117,13 +98,13 @@ public static void printTable3(int rows) { i = i + 1; } } - + public static void printTable4(int rows) { for (int i = 1; i <= rows; i = i + 1) { printRow3(i, rows); } } - + public static void printRow4(int n, int cols) { int i; for (i = 1; i <= cols; i = i + 1) { @@ -131,50 +112,44 @@ public static void printRow4(int n, int cols) { } System.out.println(i); } - + public static void main(String[] args) { - System.out.println("countdown"); - countdown(3); - - System.out.println("sequence"); - sequence(10); - System.out.println("example"); example(); - + System.out.println("example2"); example2(); - + System.out.println("example3"); example3(); - + System.out.println("example4"); example4(); - + System.out.println("example5"); example5(); - + System.out.println("printRow"); printRow(); - + System.out.println("printRow2"); printRow2(6); - + System.out.println("printTable"); printTable(); - + System.out.println("printTable2"); printTable2(6); - + System.out.println("printRow3"); printRow3(6, 6); - + System.out.println("printTable3"); printTable3(6); - + System.out.println("printRow4"); printRow4(6, 6); - + System.out.println("printTable4"); printTable4(6); } From 1340ba020541bf3296b1be16729a28a8f122ac52 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 20:08:17 -0500 Subject: [PATCH 11/24] consistent whitespace --- ch06/Series.java | 2 +- ch07/Loops.java | 16 +++++++-------- ch07/Tables.java | 52 ++++++++++++++++++++++++------------------------ 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/ch06/Series.java b/ch06/Series.java index 64ad34e..8ce7c30 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -70,7 +70,7 @@ public static double absoluteValue(double x) { /** * Tests whether x is a single digit integer. - * + * * @param x the integer to test * @return true if x has one digit, false otherwise */ diff --git a/ch07/Loops.java b/ch07/Loops.java index be02722..58c7bd6 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class Loops { - + public static void countdown(int n) { while (n > 0) { System.out.println(n); @@ -9,7 +9,7 @@ public static void countdown(int n) { } System.out.println("Blastoff!"); } - + public static void sequence(int n) { while (n != 1) { System.out.println(n); @@ -20,7 +20,7 @@ public static void sequence(int n) { } } } - + public static double scanDouble() { Scanner in = new Scanner(System.in); boolean okay; @@ -37,7 +37,7 @@ public static double scanDouble() { double x = in.nextDouble(); return x; } - + public static double scanDouble2() { Scanner in = new Scanner(System.in); while (true) { @@ -51,7 +51,7 @@ public static double scanDouble2() { double x = in.nextDouble(); return x; } - + public static double addNumbers() { Scanner in = new Scanner(System.in); int x = -1; @@ -66,13 +66,13 @@ public static double addNumbers() { } return sum; } - + public static void main(String[] args) { System.out.println("countdown"); countdown(3); - + System.out.println("sequence"); sequence(10); } - + } diff --git a/ch07/Tables.java b/ch07/Tables.java index 4d5cd39..e586699 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -2,7 +2,7 @@ * Examples from Chapter 7. */ public class Tables { - + public static void example() { int i = 1; while (i < 10) { @@ -11,7 +11,7 @@ public static void example() { i = i + 1; } } - + public static void example2() { int i = 1; while (i < 10) { @@ -20,7 +20,7 @@ public static void example2() { i = i + 1; } } - + public static void example3() { final double LOG2 = Math.log(2); int i = 1; @@ -30,7 +30,7 @@ public static void example3() { i = i * 2; } } - + public static void example4() { int i = 1; while (i <= 6) { @@ -39,7 +39,7 @@ public static void example4() { } System.out.println(); } - + public static void printRow() { int i = 1; while (i <= 6) { @@ -48,7 +48,7 @@ public static void printRow() { } System.out.println(); } - + public static void printRow2(int n) { int i = 1; while (i <= 6) { @@ -57,7 +57,7 @@ public static void printRow2(int n) { } System.out.println(); } - + public static void example5() { int i = 1; while (i <= 6) { @@ -65,7 +65,7 @@ public static void example5() { i = i + 1; } } - + public static void printTable() { int i = 1; while (i <= 6) { @@ -73,7 +73,7 @@ public static void printTable() { i = i + 1; } } - + public static void printTable2(int rows) { int i = 1; while (i <= rows) { @@ -81,7 +81,7 @@ public static void printTable2(int rows) { i = i + 1; } } - + public static void printRow3(int n, int cols) { int i = 1; while (i <= cols) { @@ -90,7 +90,7 @@ public static void printRow3(int n, int cols) { } System.out.println(); } - + public static void printTable3(int rows) { int i = 1; while (i <= rows) { @@ -98,13 +98,13 @@ public static void printTable3(int rows) { i = i + 1; } } - + public static void printTable4(int rows) { for (int i = 1; i <= rows; i = i + 1) { printRow3(i, rows); } } - + public static void printRow4(int n, int cols) { int i; for (i = 1; i <= cols; i = i + 1) { @@ -112,44 +112,44 @@ public static void printRow4(int n, int cols) { } System.out.println(i); } - + public static void main(String[] args) { System.out.println("example"); example(); - + System.out.println("example2"); example2(); - + System.out.println("example3"); example3(); - + System.out.println("example4"); example4(); - + System.out.println("example5"); example5(); - + System.out.println("printRow"); printRow(); - + System.out.println("printRow2"); printRow2(6); - + System.out.println("printTable"); printTable(); - + System.out.println("printTable2"); printTable2(6); - + System.out.println("printRow3"); printRow3(6, 6); - + System.out.println("printTable3"); printTable3(6); - + System.out.println("printRow4"); printRow4(6, 6); - + System.out.println("printTable4"); printTable4(6); } From c54190dc7f48a562dcdd253f2d9919fabb930a51 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 21:49:16 -0500 Subject: [PATCH 12/24] moved makeHistogram --- ch08/ArrayExamples.java | 58 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/ch08/ArrayExamples.java b/ch08/ArrayExamples.java index dfc8d1e..a9a4e9a 100644 --- a/ch08/ArrayExamples.java +++ b/ch08/ArrayExamples.java @@ -66,35 +66,6 @@ public static void main(String[] args) { makeHistogram(); } - /** - * Example code related to histograms. - */ - public static void makeHistogram() { - int numValues = 8; - int[] array = randomArray(numValues); - printArray(array); - - int[] scores = randomArray(30); - int a = inRange(scores, 90, 100); - int b = inRange(scores, 80, 90); - int c = inRange(scores, 70, 80); - int d = inRange(scores, 60, 70); - int f = inRange(scores, 0, 60); - - // making a histogram - int[] counts = new int[100]; - for (int i = 0; i < scores.length; i++) { - int index = scores[i]; - counts[index]++; - } - - // histogram with enhanced for loop - counts = new int[100]; - for (int score : scores) { - counts[score]++; - } - } - /** * Prints the elements of an array. */ @@ -153,4 +124,33 @@ public static int inRange(int[] a, int low, int high) { } return count; } + + /** + * Example code related to histograms. + */ + public static void makeHistogram() { + int numValues = 8; + int[] array = randomArray(numValues); + printArray(array); + + int[] scores = randomArray(30); + int a = inRange(scores, 90, 100); + int b = inRange(scores, 80, 90); + int c = inRange(scores, 70, 80); + int d = inRange(scores, 60, 70); + int f = inRange(scores, 0, 60); + + // making a histogram + int[] counts = new int[100]; + for (int i = 0; i < scores.length; i++) { + int index = scores[i]; + counts[index]++; + } + + // histogram with enhanced for loop + counts = new int[100]; + for (int score : scores) { + counts[score]++; + } + } } From 0639135fcdbf3ee65322a99b911e78419ceecc96 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 9 Mar 2016 23:26:42 -0500 Subject: [PATCH 13/24] read through ch09 --- ch09/Max.java | 19 +++++++++++++++++++ ch09/StringsThings.java | 20 ++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 ch09/Max.java diff --git a/ch09/Max.java b/ch09/Max.java new file mode 100644 index 0000000..01417f0 --- /dev/null +++ b/ch09/Max.java @@ -0,0 +1,19 @@ +import java.util.Arrays; + +/** + * Demonstrates command-line arguments. + */ +public class Max { + + public static void main(String[] args) { + System.out.println(Arrays.toString(args)); + + int max = Integer.MIN_VALUE; + for (String arg : args) { + int value = Integer.parseInt(arg); + if (value > max) { + max = value; + } + } + } +} diff --git a/ch09/StringsThings.java b/ch09/StringsThings.java index bdec09c..20357c1 100644 --- a/ch09/StringsThings.java +++ b/ch09/StringsThings.java @@ -28,13 +28,6 @@ public static void main(String[] args) { } System.out.println(); - String fruit = "banana"; - - for (int i = 0; i < fruit.length(); i++) { - char letter = fruit.charAt(i); - System.out.println(letter); - } - String name1 = "Alan Turing"; String name2 = "Ada Lovelace"; if (name1.equals(name2)) { @@ -50,6 +43,17 @@ public static void main(String[] args) { System.out.println("name2 comes before name1."); } - System.out.println(reverse(name1)); + String fruit = "banana"; + + for (int i = 0; i < fruit.length(); i++) { + char letter = fruit.charAt(i); + System.out.println(letter); + } + + for (char letter : fruit.toCharArray()) { + System.out.println(letter); + } + + System.out.println(reverse(fruit)); } } From 19511bbfedfdda74b94d2eb64af49607d20cd07c Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Thu, 10 Mar 2016 13:27:51 -0500 Subject: [PATCH 14/24] added String.format section --- ch09/Format.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ch09/Format.java diff --git a/ch09/Format.java b/ch09/Format.java new file mode 100644 index 0000000..7e3ff8a --- /dev/null +++ b/ch09/Format.java @@ -0,0 +1,33 @@ +/** + * Example using the String.format method. + */ +public class Format { + + /** + * Returns a time string in 12-hour format. + * + * @param hour between 0 and 23 + * @param minute between 0 and 59 + */ + public static String timeString(int hour, int minute) { + String ampm; + if (hour < 12) { + ampm = "AM"; + if (hour == 0) { + hour = 12; // midnight + } + } else { + ampm = "PM"; + hour = hour - 12; + } + return String.format("%02d:%02d %s", hour, minute, ampm); + } + + public static void main(String[] args) { + System.out.println(timeString(0, 0)); + System.out.println(timeString(7, 30)); + System.out.println(timeString(12, 5)); + System.out.println(timeString(23, 59)); + } + +} From 353123ff82492c716b96c4ca84715017cb7d5f3f Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Thu, 10 Mar 2016 16:15:06 -0500 Subject: [PATCH 15/24] read through ch12 --- ch12/Search.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ch12/Search.java b/ch12/Search.java index 520a838..3cfe2d1 100644 --- a/ch12/Search.java +++ b/ch12/Search.java @@ -24,14 +24,14 @@ public static int binarySearch(Card[] cards, Card target) { while (low <= high) { System.out.println(low + ", " + high); - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 + } else if (comp < 0) { // step 3 low = mid + 1; - } else { // step 4 + } else { // step 4 high = mid - 1; } } @@ -48,14 +48,14 @@ public static int binarySearchRec(Card[] cards, Card target, if (high < low) { return -1; } - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 + } else if (comp < 0) { // step 3 return binarySearchRec(cards, target, mid + 1, high); - } else { // step 4 + } else { // step 4 return binarySearchRec(cards, target, low, mid - 1); } } From 5bc037ba87865db163348261e0fedec9f369da6d Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 20 Apr 2016 16:55:30 -0400 Subject: [PATCH 16/24] stubs for ch13 --- ch13/Deck.java | 126 +++---------------------------------------------- 1 file changed, 6 insertions(+), 120 deletions(-) diff --git a/ch13/Deck.java b/ch13/Deck.java index d567844..864d52e 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -6,10 +6,6 @@ */ public class Deck { - // This is a class variable so we don't have to create - // a new Random object every time we call randomInt. - private static Random random = new Random(); - private Card[] cards; /** @@ -41,65 +37,24 @@ public Card[] getCards() { } /** - * Returns a string representation of the deck. - */ - public String toString() { - return Arrays.toString(this.cards); - } - - /** - * Swaps the cards at indexes i and j. + * Displays each of the cards in the deck. */ - public void swapCards(int i, int j) { - Card temp = this.cards[i]; - this.cards[i] = this.cards[j]; - this.cards[j] = temp; - } - - /** - * Chooses a random number between low and high, including both. - */ - public int randomInt(int low, int high) { - int range = high - low + 1; - return low + random.nextInt(range); + public void print() { + for (int i = 0; i < this.cards.length; i++) { + System.out.println(this.cards[i]); + } } /** * Randomly permutes the array of cards. */ public void shuffle() { - for (int i = 0; i < this.cards.length - 1; i++) { - int j = this.randomInt(i, this.cards.length - 1); - this.swapCards(i, j); - } - } - - /** - * Finds the index of the lowest card - * between low and high inclusive. - */ - public int indexLowest(int low, int high) { - int index = low; - Card minCard = this.cards[low]; - for (int i = low + 1; i <= high; i++) { - Card card = this.cards[i]; - if (card.compareTo(minCard) < 0) { - index = i; - minCard = card; - } - } - return index; } /** * Sorts the cards (in place) using selection sort. */ public void selectionSort() { - int high = this.cards.length - 1; - for (int i = 0; i < this.cards.length; i++) { - int j = this.indexLowest(i, high); - this.swapCards(i, j); - } } /** @@ -113,85 +68,16 @@ public Deck subdeck(int low, int high) { return sub; } - /** - * Combines two previously sorted subdecks. - */ - public static Deck merge(Deck d1, Deck d2) { - Card[] c1 = d1.cards; - Card[] c2 = d2.cards; - Deck result = new Deck(c1.length + c2.length); - Card[] c3 = result.cards; - int i = 0; // index in c1 - int j = 0; // index in c2 - - // for each index in the result - for (int k = 0; k < c3.length; k++) { - int choice; - - // determine which card to merge next - if (i >= c1.length) { - choice = 2; // c1 is empty - } else if (j >= c2.length) { - choice = 1; // c2 is empty - } else if (c1[i].compareTo(c2[j]) < 0) { - choice = 1; // c1 is lower - } else { - choice = 2; // c2 is lower - } - - // store the chosen card in the result - if (choice == 1) { - c3[k] = c1[i]; - i++; - } else { - c3[k] = c2[j]; - j++; - } - } - return result; - } - /** * Returns a sorted copy of the deck using merge sort. */ public Deck mergeSort() { - - // 0 or 1 cards, already sorted - int len = this.cards.length; - if (len < 2) { - return this; - } - - // cut the deck about in half - int mid = len / 2; - Deck d1 = this.subdeck(0, mid - 1); - Deck d2 = this.subdeck(mid, len - 1); - - // sort each half and merge - d1 = d1.mergeSort(); - d2 = d2.mergeSort(); - return merge(d1, d2); + return this; } /** * Reorders the cards (in place) using insertion sort. */ public void insertionSort() { - for (int i = 1; i < this.cards.length; i++) { - Card card = this.cards[i]; - this.insert(card, i); - } - } - - /** - * Helper method for insertion sort. - */ - private void insert(Card card, int i) { - int j = i; - while (j > 0 && card.compareTo(this.cards[j - 1]) < 0) { - this.cards[j] = this.cards[j - 1]; - j--; - } - this.cards[j] = card; } } From aa70d99c54de2dcbac42787094337e56356f61a7 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Thu, 28 Apr 2016 16:47:22 -0400 Subject: [PATCH 17/24] review ch1-7 --- ch02/Variables.java | 15 ++++++------ ch03/GuessStarter.java | 2 +- ch06/Recursive.java | 2 +- ch06/Series.java | 34 +++++++++++++-------------- ch06/SeriesTest.java | 3 +++ ch07/Loops.java | 52 +++-------------------------------------- ch07/Tables.java | 2 +- ch07/Validate.java | 53 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 ch07/Validate.java diff --git a/ch02/Variables.java b/ch02/Variables.java index b4a99d6..0312023 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -14,14 +14,14 @@ public static void main(String[] args) { 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; @@ -43,7 +43,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); @@ -61,10 +60,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 @@ -79,6 +78,6 @@ public static void main(String[] args) { percentage = (minute * 100) / 60; hour = minute + 1; // correct - // minute + 1 = hour; syntax error + // minute + 1 = hour; // compiler error } } diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 92d7f50..f3240c0 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -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 { diff --git a/ch06/Recursive.java b/ch06/Recursive.java index 0d29924..f045844 100644 --- a/ch06/Recursive.java +++ b/ch06/Recursive.java @@ -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; } diff --git a/ch06/Series.java b/ch06/Series.java index 8ce7c30..6db5643 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -12,12 +12,12 @@ public static void countup(int n) { } } - public static double area(double radius) { - double area = Math.PI * radius * radius; - return area; + public static double calculateArea(double radius) { + double result = Math.PI * radius * radius; + return result; } - public static double area2(double radius) { + public static double calculateArea2(double radius) { return Math.PI * radius * radius; } @@ -59,13 +59,13 @@ public static double absoluteValue(double x) { public static double circleArea (double xc, double yc, double xp, double yp) { double radius = distance(xc, yc, xp, yp); - double area = area(radius); + double area = calculateArea(radius); return area; } - public static double area + public static double calculateArea (double xc, double yc, double xp, double yp) { - return area(distance(xc, yc, xp, yp)); + return calculateArea(distance(xc, yc, xp, yp)); } /** @@ -106,17 +106,17 @@ public static void main(String[] args) { countup(3); System.out.println("Have a nice day."); - System.out.println("area"); - System.out.println(area(3.0)); + System.out.println("calculateArea"); + System.out.println(calculateArea(3.0)); - System.out.println("area2"); - System.out.println(area2(3.0)); + System.out.println("calculateArea2"); + System.out.println(calculateArea2(3.0)); System.out.println("circleArea"); System.out.println(circleArea(1.0, 2.0, 4.0, 6.0)); - System.out.println("area with 4 doubles"); - System.out.println(area(1.0, 2.0, 4.0, 6.0)); + System.out.println("calculateArea with 4 doubles"); + System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0)); System.out.println("absolute value"); System.out.println(absoluteValue(-2)); @@ -133,11 +133,11 @@ public static void main(String[] args) { System.out.println(isSingleDigit(2)); boolean bigFlag = !isSingleDigit2(17); - int i = 9; - if (isSingleDigit(i)) { - System.out.println("i is small"); + int z = 9; + if (isSingleDigit(z)) { + System.out.println("z is small"); } else { - System.out.println("i is big"); + System.out.println("z is big"); } System.out.println("factorial"); diff --git a/ch06/SeriesTest.java b/ch06/SeriesTest.java index 7517c85..93b5420 100644 --- a/ch06/SeriesTest.java +++ b/ch06/SeriesTest.java @@ -1,5 +1,8 @@ import junit.framework.TestCase; +/** + * Example JUnit test from Appendix A. + */ public class SeriesTest extends TestCase { public void testFibonacci() { diff --git a/ch07/Loops.java b/ch07/Loops.java index 58c7bd6..d5c915f 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -1,5 +1,6 @@ -import java.util.Scanner; - +/** + * Examples from Chapter 7. + */ public class Loops { public static void countdown(int n) { @@ -21,52 +22,6 @@ public static void sequence(int n) { } } - public static double scanDouble() { - Scanner in = new Scanner(System.in); - boolean okay; - do { - System.out.print("Enter a number: "); - if (in.hasNextDouble()) { - okay = true; - } else { - okay = false; - String word = in.next(); - System.err.println(word + " is not a number"); - } - } while (!okay); - double x = in.nextDouble(); - return x; - } - - public static double scanDouble2() { - Scanner in = new Scanner(System.in); - while (true) { - System.out.print("Enter a number: "); - if (in.hasNextDouble()) { - break; - } - String word = in.next(); - System.err.println(word + " is not a number"); - } - double x = in.nextDouble(); - return x; - } - - public static double addNumbers() { - Scanner in = new Scanner(System.in); - int x = -1; - int sum = 0; - while (x != 0) { - x = in.nextInt(); - if (x <= 0) { - continue; - } - System.out.println("Adding " + x); - sum += x; - } - return sum; - } - public static void main(String[] args) { System.out.println("countdown"); countdown(3); @@ -74,5 +29,4 @@ public static void main(String[] args) { System.out.println("sequence"); sequence(10); } - } diff --git a/ch07/Tables.java b/ch07/Tables.java index e586699..0fbfbbe 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -1,5 +1,5 @@ /** - * Examples from Chapter 7. + * Generating tables; encapsulation and generalization. */ public class Tables { diff --git a/ch07/Validate.java b/ch07/Validate.java new file mode 100644 index 0000000..eeb81da --- /dev/null +++ b/ch07/Validate.java @@ -0,0 +1,53 @@ +import java.util.Scanner; + +/** + * Do-while, break, and continue. + */ +public class Validate { + + public static double scanDouble() { + Scanner in = new Scanner(System.in); + boolean okay; + do { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + okay = true; + } else { + okay = false; + String word = in.next(); + System.err.println(word + " is not a number"); + } + } while (!okay); + double x = in.nextDouble(); + return x; + } + + public static double scanDouble2() { + Scanner in = new Scanner(System.in); + while (true) { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + break; + } + String word = in.next(); + System.err.println(word + " is not a number"); + } + double x = in.nextDouble(); + return x; + } + + public static double addNumbers() { + Scanner in = new Scanner(System.in); + int x = -1; + int sum = 0; + while (x != 0) { + x = in.nextInt(); + if (x <= 0) { + continue; + } + System.out.println("Adding " + x); + sum += x; + } + return sum; + } +} From ae811e02f8259d7d5cef3559914605fb3bdce8c0 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 15:56:57 -0400 Subject: [PATCH 18/24] review ap01..ch07 --- ap01/Series.java | 25 +++++++++++++++++++++++++ {ch06 => ap01}/SeriesTest.java | 1 + ap02/Drawing.java | 5 +---- ap02/Mickey.java | 17 +++++++---------- ap02/Moire.java | 29 +++++++++-------------------- ch01/Goodbye.java | 1 + ch01/Hello.java | 1 + ch02/Variables.java | 2 +- ch03/Convert.java | 1 + ch03/Echo.java | 1 + ch03/GuessStarter.java | 1 + ch03/Input.java | 1 + ch03/ScannerBug.java | 1 + ch04/Methods.java | 1 + ch04/NewLine.java | 1 + ch04/PrintTime.java | 1 + ch04/PrintTwice.java | 1 + ch06/Series.java | 1 + ch07/Exercise.java | 19 +++++++++++++++++++ ch07/Loops.java | 1 + ch07/Tables.java | 1 + ch07/Validate.java | 1 + 22 files changed, 78 insertions(+), 35 deletions(-) create mode 100644 ap01/Series.java rename {ch06 => ap01}/SeriesTest.java (99%) create mode 100644 ch07/Exercise.java diff --git a/ap01/Series.java b/ap01/Series.java new file mode 100644 index 0000000..2d2d66c --- /dev/null +++ b/ap01/Series.java @@ -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"); + } + } + +} diff --git a/ch06/SeriesTest.java b/ap01/SeriesTest.java similarity index 99% rename from ch06/SeriesTest.java rename to ap01/SeriesTest.java index 93b5420..6309616 100644 --- a/ch06/SeriesTest.java +++ b/ap01/SeriesTest.java @@ -10,4 +10,5 @@ public void testFibonacci() { assertEquals(1, Series.fibonacci(2)); assertEquals(2, Series.fibonacci(3)); } + } diff --git a/ap02/Drawing.java b/ap02/Drawing.java index 1453913..e82bfca 100644 --- a/ap02/Drawing.java +++ b/ap02/Drawing.java @@ -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(); @@ -20,4 +16,5 @@ public static void main(String[] args) { public void paint(Graphics g) { g.fillOval(100, 100, 200, 200); } + } diff --git a/ap02/Mickey.java b/ap02/Mickey.java index 14e93b9..6dfa3bc 100644 --- a/ap02/Mickey.java +++ b/ap02/Mickey.java @@ -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); @@ -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); @@ -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); - } } diff --git a/ap02/Moire.java b/ap02/Moire.java index 78a1c22..750bad3 100644 --- a/ap02/Moire.java +++ b/ap02/Moire.java @@ -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; @@ -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); - } } diff --git a/ch01/Goodbye.java b/ch01/Goodbye.java index 0b165c3..2963e2e 100644 --- a/ch01/Goodbye.java +++ b/ch01/Goodbye.java @@ -10,4 +10,5 @@ public static void main(String[] args) { System.out.print("Goodbye, "); // note the space System.out.println("cruel world"); } + } diff --git a/ch01/Hello.java b/ch01/Hello.java index 13dacf5..593557b 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -4,4 +4,5 @@ public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); } + } diff --git a/ch02/Variables.java b/ch02/Variables.java index 0312023..a295abf 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -6,7 +6,6 @@ public class Variables { public static void main(String[] args) { String message; - int x; String firstName; @@ -80,4 +79,5 @@ public static void main(String[] args) { hour = minute + 1; // correct // minute + 1 = hour; // compiler error } + } diff --git a/ch03/Convert.java b/ch03/Convert.java index 92fc06b..d31fe77 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -23,4 +23,5 @@ public static void main(String[] args) { System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, remainder); } + } diff --git a/ch03/Echo.java b/ch03/Echo.java index b82b2d4..500fd9f 100644 --- a/ch03/Echo.java +++ b/ch03/Echo.java @@ -14,4 +14,5 @@ public static void main(String[] args) { line = in.nextLine(); System.out.println("You also said: " + line); } + } diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index f3240c0..64984df 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -11,4 +11,5 @@ public static void main(String[] args) { int number = random.nextInt(100) + 1; System.out.println(number); } + } diff --git a/ch03/Input.java b/ch03/Input.java index 574290d..de97dc0 100644 --- a/ch03/Input.java +++ b/ch03/Input.java @@ -12,4 +12,5 @@ public static void main(String[] args) { double pi = 3.14159; double x = (int) pi * 20.0; } + } diff --git a/ch03/ScannerBug.java b/ch03/ScannerBug.java index 7a2d079..353affb 100644 --- a/ch03/ScannerBug.java +++ b/ch03/ScannerBug.java @@ -22,4 +22,5 @@ public static void main(String[] args) { name = in.nextLine(); System.out.printf("Hello %s, age %d\n", name, age); } + } diff --git a/ch04/Methods.java b/ch04/Methods.java index 035a392..90c1b7a 100644 --- a/ch04/Methods.java +++ b/ch04/Methods.java @@ -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); } + } diff --git a/ch04/NewLine.java b/ch04/NewLine.java index b13ddfc..a554e40 100644 --- a/ch04/NewLine.java +++ b/ch04/NewLine.java @@ -15,4 +15,5 @@ public static void main(String[] args) { threeLine(); System.out.println("Second line."); } + } diff --git a/ch04/PrintTime.java b/ch04/PrintTime.java index fe3549c..019d579 100644 --- a/ch04/PrintTime.java +++ b/ch04/PrintTime.java @@ -11,4 +11,5 @@ public static void main(String[] args) { int minute = 59; printTime(hour, minute); } + } diff --git a/ch04/PrintTwice.java b/ch04/PrintTwice.java index c34ed1c..7131085 100644 --- a/ch04/PrintTwice.java +++ b/ch04/PrintTwice.java @@ -8,4 +8,5 @@ public static void printTwice(String s) { public static void main(String[] args) { printTwice("Don't make me say this twice!"); } + } diff --git a/ch06/Series.java b/ch06/Series.java index 6db5643..10d2b41 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -146,4 +146,5 @@ public static void main(String[] args) { System.out.println("fibonacci"); System.out.println(fibonacci(3)); } + } diff --git a/ch07/Exercise.java b/ch07/Exercise.java new file mode 100644 index 0000000..6c7b3df --- /dev/null +++ b/ch07/Exercise.java @@ -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; + } + } + } + +} diff --git a/ch07/Loops.java b/ch07/Loops.java index d5c915f..dfc03ce 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -29,4 +29,5 @@ public static void main(String[] args) { System.out.println("sequence"); sequence(10); } + } diff --git a/ch07/Tables.java b/ch07/Tables.java index 0fbfbbe..44e9e62 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -153,4 +153,5 @@ public static void main(String[] args) { System.out.println("printTable4"); printTable4(6); } + } diff --git a/ch07/Validate.java b/ch07/Validate.java index eeb81da..e825705 100644 --- a/ch07/Validate.java +++ b/ch07/Validate.java @@ -50,4 +50,5 @@ public static double addNumbers() { } return sum; } + } From a60b1d128cd92c17881aa4fc5f60488e60dcc882 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 16:03:49 -0400 Subject: [PATCH 19/24] review ch08..ch11 --- ch08/ArrayExamples.java | 56 --------------------------- ch08/Fruit.java | 35 +++++++++++++++++ ch08/Histogram.java | 59 ++++++++++++++++++++++++++++ ch08/MakeDubMus.java | 34 ++++++++++++++++ ch09/Exercise.java | 22 +++++++++++ ch09/Max.java | 5 +++ ch09/Recurse.java | 34 ++++++++++++++++ ch09/StringsThings.java | 86 +++++++++++++++++++++++++++++++---------- ch10/Max.java | 23 ----------- ch10/PointRect.java | 59 +++++++++++++++++++++------- ch10/Pow.java | 24 ++++++++++++ ch10/Riddle.java | 23 +++++++++++ ch11/Time.java | 16 -------- ch11/TimeClient.java | 29 +++++++++++--- 14 files changed, 370 insertions(+), 135 deletions(-) create mode 100644 ch08/Fruit.java create mode 100644 ch08/Histogram.java create mode 100644 ch08/MakeDubMus.java create mode 100644 ch09/Exercise.java create mode 100644 ch09/Recurse.java delete mode 100644 ch10/Max.java create mode 100644 ch10/Pow.java create mode 100644 ch10/Riddle.java diff --git a/ch08/ArrayExamples.java b/ch08/ArrayExamples.java index a9a4e9a..a4cb6c3 100644 --- a/ch08/ArrayExamples.java +++ b/ch08/ArrayExamples.java @@ -1,5 +1,4 @@ import java.util.Arrays; -import java.util.Random; /** * Demonstrates uses of arrays. @@ -62,8 +61,6 @@ public static void main(String[] args) { // reduce double total = sum(a); System.out.println("total = " + total); - - makeHistogram(); } /** @@ -100,57 +97,4 @@ public static double sum(double[] a) { return total; } - /** - * Returns an array of random integers. - */ - public static int[] randomArray(int size) { - Random random = new Random(); - int[] a = new int[size]; - for (int i = 0; i < a.length; i++) { - a[i] = random.nextInt(100); - } - return a; - } - - /** - * Computes the number of array elements in [low, high). - */ - public static int inRange(int[] a, int low, int high) { - int count = 0; - for (int i = 0; i < a.length; i++) { - if (a[i] >= low && a[i] < high) { - count++; - } - } - return count; - } - - /** - * Example code related to histograms. - */ - public static void makeHistogram() { - int numValues = 8; - int[] array = randomArray(numValues); - printArray(array); - - int[] scores = randomArray(30); - int a = inRange(scores, 90, 100); - int b = inRange(scores, 80, 90); - int c = inRange(scores, 70, 80); - int d = inRange(scores, 60, 70); - int f = inRange(scores, 0, 60); - - // making a histogram - int[] counts = new int[100]; - for (int i = 0; i < scores.length; i++) { - int index = scores[i]; - counts[index]++; - } - - // histogram with enhanced for loop - counts = new int[100]; - for (int score : scores) { - counts[score]++; - } - } } diff --git a/ch08/Fruit.java b/ch08/Fruit.java new file mode 100644 index 0000000..50428c5 --- /dev/null +++ b/ch08/Fruit.java @@ -0,0 +1,35 @@ +/** + * Fruit exercise. + */ +public class Fruit { + + public static int banana(int[] a) { + int kiwi = 1; + int i = 0; + while (i < a.length) { + kiwi = kiwi * a[i]; + i++; + } + return kiwi; + } + + public static int grapefruit(int[] a, int grape) { + for (int i = 0; i < a.length; i++) { + if (a[i] == grape) { + return i; + } + } + return -1; + } + + public static int pineapple(int[] a, int apple) { + int pear = 0; + for (int pine: a) { + if (pine == apple) { + pear++; + } + } + return pear; + } + +} diff --git a/ch08/Histogram.java b/ch08/Histogram.java new file mode 100644 index 0000000..32b029c --- /dev/null +++ b/ch08/Histogram.java @@ -0,0 +1,59 @@ +import java.util.Random; + +/** + * Example code related to histograms. + */ +public class Histogram { + + /** + * Returns an array of random integers. + */ + public static int[] randomArray(int size) { + Random random = new Random(); + int[] a = new int[size]; + for (int i = 0; i < a.length; i++) { + a[i] = random.nextInt(100); + } + return a; + } + + /** + * Computes the number of array elements in [low, high). + */ + public static int inRange(int[] a, int low, int high) { + int count = 0; + for (int i = 0; i < a.length; i++) { + if (a[i] >= low && a[i] < high) { + count++; + } + } + return count; + } + + public static void main(String[] args) { + int numValues = 8; + int[] array = randomArray(numValues); + ArrayExamples.printArray(array); + + int[] scores = randomArray(30); + int a = inRange(scores, 90, 100); + int b = inRange(scores, 80, 90); + int c = inRange(scores, 70, 80); + int d = inRange(scores, 60, 70); + int f = inRange(scores, 0, 60); + + // making a histogram + int[] counts = new int[100]; + for (int i = 0; i < scores.length; i++) { + int index = scores[i]; + counts[index]++; + } + + // histogram with enhanced for loop + counts = new int[100]; + for (int score : scores) { + counts[score]++; + } + } + +} diff --git a/ch08/MakeDubMus.java b/ch08/MakeDubMus.java new file mode 100644 index 0000000..9c7e193 --- /dev/null +++ b/ch08/MakeDubMus.java @@ -0,0 +1,34 @@ +/** + * Stack diagram exercise. + */ +public class MakeDubMus { + + public static int[] make(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = i + 1; + } + return a; + } + + public static void dub(int[] jub) { + for (int i = 0; i < jub.length; i++) { + jub[i] *= 2; + } + } + + public static int mus(int[] zoo) { + int fus = 0; + for (int i = 0; i < zoo.length; i++) { + fus += zoo[i]; + } + return fus; + } + + public static void main(String[] args) { + int[] bob = make(5); + dub(bob); + System.out.println(mus(bob)); + } + +} diff --git a/ch09/Exercise.java b/ch09/Exercise.java new file mode 100644 index 0000000..d8a605d --- /dev/null +++ b/ch09/Exercise.java @@ -0,0 +1,22 @@ +/** + * Exercise on encapsulation and generalization. + */ +public class Exercise { + + public static void main(String[] args) { + String s = "((3 + 7) * 2)"; + int count = 0; + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(') { + count++; + } else if (c == ')') { + count--; + } + } + + System.out.println(count); + } + +} diff --git a/ch09/Max.java b/ch09/Max.java index 01417f0..2bf923e 100644 --- a/ch09/Max.java +++ b/ch09/Max.java @@ -5,6 +5,9 @@ */ public class Max { + /** + * Converts args to integers and prints the max. + */ public static void main(String[] args) { System.out.println(Arrays.toString(args)); @@ -15,5 +18,7 @@ public static void main(String[] args) { max = value; } } + System.out.println("The max is " + max); } + } diff --git a/ch09/Recurse.java b/ch09/Recurse.java new file mode 100644 index 0000000..c4ba2fb --- /dev/null +++ b/ch09/Recurse.java @@ -0,0 +1,34 @@ +/** + * Recursion exercise. + */ +public class Recurse { + + /** + * Returns the first character of the given String. + */ + public static char first(String s) { + return s.charAt(0); + } + + /** + * Returns all but the first letter of the given String. + */ + public static String rest(String s) { + return s.substring(1); + } + + /** + * Returns all but the first and last letter of the String. + */ + public static String middle(String s) { + return s.substring(1, s.length() - 1); + } + + /** + * Returns the length of the given String. + */ + public static int length(String s) { + return s.length(); + } + +} diff --git a/ch09/StringsThings.java b/ch09/StringsThings.java index 20357c1..8b95e7e 100644 --- a/ch09/StringsThings.java +++ b/ch09/StringsThings.java @@ -3,19 +3,17 @@ */ public class StringsThings { - /** - * Reverses a string, returns a new String. - */ - public static String reverse(String s) { - String r = ""; - for (int i = s.length() - 1; i >= 0; i--) { - r = r + s.charAt(i); - } - return r; - } - public static void main(String[] args) { + // Characters + + String fruit = "banana"; + char letter0 = fruit.charAt(0); + + if (letter0 == 'a') { + System.out.println('?'); + } + System.out.print("Roman alphabet: "); for (char c = 'A'; c <= 'Z'; c++) { System.out.print(c); @@ -28,6 +26,47 @@ public static void main(String[] args) { } System.out.println(); + // Strings are immutable + + String name = "Alan Turing"; + String upperName = name.toUpperCase(); + + String text = "Computer Science is fun!"; + text = text.replace("Computer Science", "CS"); + + // String traversal + + for (int i = 0; i < fruit.length(); i++) { + char letter = fruit.charAt(i); + System.out.println(letter); + } + + for (char letter : fruit.toCharArray()) { + System.out.println(letter); + } + + int length = fruit.length(); + char last = fruit.charAt(length - 1); // correct + + System.out.println(reverse(fruit)); + + // Substrings + + System.out.println(fruit.substring(0)); + System.out.println(fruit.substring(2)); + System.out.println(fruit.substring(6)); + + System.out.println(fruit.substring(0, 3)); + System.out.println(fruit.substring(2, 5)); + System.out.println(fruit.substring(6, 6)); + + // The indexOf method + + int index = fruit.indexOf('a'); + int index2 = fruit.indexOf('a', 2); + + // String comparison + String name1 = "Alan Turing"; String name2 = "Ada Lovelace"; if (name1.equals(name2)) { @@ -43,17 +82,24 @@ public static void main(String[] args) { System.out.println("name2 comes before name1."); } - String fruit = "banana"; + // Wrapper classes - for (int i = 0; i < fruit.length(); i++) { - char letter = fruit.charAt(i); - System.out.println(letter); - } + String str = "12345"; + int num = Integer.parseInt(str); - for (char letter : fruit.toCharArray()) { - System.out.println(letter); - } + num = 12345; + str = Integer.toString(num); + } - System.out.println(reverse(fruit)); + /** + * Reverses a string, returns a new String. + */ + public static String reverse(String s) { + String r = ""; + for (int i = s.length() - 1; i >= 0; i--) { + r = r + s.charAt(i); + } + return r; } + } diff --git a/ch10/Max.java b/ch10/Max.java deleted file mode 100644 index a7ecea5..0000000 --- a/ch10/Max.java +++ /dev/null @@ -1,23 +0,0 @@ -import java.util.Arrays; - -/** - * Demonstrates command-line arguments. - */ -public class Max { - - /** - * Converts the command line arguments to integers and prints the max. - */ - public static void main(String[] args) { - System.out.println(Arrays.toString(args)); - - int max = Integer.MIN_VALUE; - for (String arg : args) { - int value = Integer.parseInt(arg); - if (value > max) { - max = value; - } - } - System.out.println("The max is " + max); - } -} diff --git a/ch10/PointRect.java b/ch10/PointRect.java index d0da1df..36801a5 100644 --- a/ch10/PointRect.java +++ b/ch10/PointRect.java @@ -6,6 +6,28 @@ */ public class PointRect { + public static void main(String[] args) { + Point blank; + blank = new Point(3, 4); + System.out.println(blank); + + int x = blank.x; + System.out.println(blank.x + ", " + blank.y); + int sum = blank.x * blank.x + blank.y * blank.y; + + Rectangle box = new Rectangle(0, 0, 100, 200); + moveRect(box, 50, 100); + System.out.println(box); + box.translate(50, 100); + + Rectangle box1 = new Rectangle(0, 0, 100, 200); + Rectangle box2 = box1; + + System.out.println(box2.width); + box1.grow(50, 50); + System.out.println(box2.width); + } + /** * Prints the attributes of a Point object. */ @@ -17,8 +39,8 @@ public static void printPoint(Point p) { * Computes the distance between two points. */ public static double distance(Point p1, Point p2) { - double dx = (double) (p2.x - p1.x); - double dy = (double) (p2.y - p1.y); + int dx = p2.x - p1.x; + int dy = p2.y - p1.y; return Math.sqrt(dx * dx + dy * dy); } @@ -40,22 +62,29 @@ public static void moveRect(Rectangle box, int dx, int dy) { } /** - * Tests the methods in this class. + * Exercise on returning objects. */ - public static void main(String[] args) { - Point blank; - blank = new Point(3, 4); - System.out.println(blank); + public static void exercise2() { + Point blank = new Point(5, 8); - Rectangle box = new Rectangle(0, 0, 100, 200); - moveRect(box, 50, 100); - System.out.println(box); + Rectangle rect = new Rectangle(0, 2, 4, 4); + Point center = findCenter(rect); - Rectangle box1 = new Rectangle(0, 0, 100, 200); - Rectangle box2 = box1; + double dist = distance(center, blank); + System.out.println(dist); + } - System.out.println(box2.width); - box1.grow(50, 50); - System.out.println(box2.width); + /** + * Exercise on aliasing. + */ + public static void exercise3() { + Rectangle box1 = new Rectangle(2, 4, 7, 9); + Point p1 = findCenter(box1); + printPoint(p1); + + box1.grow(1, 1); + Point p2 = findCenter(box1); + printPoint(p2); } + } diff --git a/ch10/Pow.java b/ch10/Pow.java new file mode 100644 index 0000000..9ba0952 --- /dev/null +++ b/ch10/Pow.java @@ -0,0 +1,24 @@ +/** + * BigInteger exercise. + */ +public class Pow { + + /** + * Integer exponentiation. + */ + public static int pow(int x, int n) { + if (n == 0) return 1; + + // find x to the n/2 recursively + int t = pow(x, n / 2); + + // if n is even, the result is t squared + // if n is odd, the result is t squared times x + if (n % 2 == 0) { + return t * t; + } else { + return t * t * x; + } + } + +} diff --git a/ch10/Riddle.java b/ch10/Riddle.java new file mode 100644 index 0000000..cd04a16 --- /dev/null +++ b/ch10/Riddle.java @@ -0,0 +1,23 @@ +import java.awt.Point; + +/** + * Exercise on passing objects as parameters. + */ +public class Riddle { + + public static int riddle(int x, Point p) { + x = x + 7; + return x + p.x + p.y; + } + + public static void main(String[] args) { + int x = 5; + Point blank = new Point(1, 2); + + System.out.println(riddle(x, blank)); + System.out.println(x); + System.out.println(blank.x); + System.out.println(blank.y); + } + +} diff --git a/ch11/Time.java b/ch11/Time.java index 99e75db..53afa0d 100644 --- a/ch11/Time.java +++ b/ch11/Time.java @@ -99,20 +99,4 @@ public void increment(double seconds) { } } - /** - * Test the methods in this class. - */ - public static void main(String[] args) { - Time time = new Time(11, 59, 59.9); - System.out.println(time); - - Time time1 = new Time(9, 30, 0.0); - Time time2 = time1; - Time time3 = new Time(9, 30, 0.0); - - System.out.println(time1 == time2); - System.out.println(time1 == time3); - System.out.println(time1.equals(time2)); - System.out.println(time1.equals(time3)); - } } diff --git a/ch11/TimeClient.java b/ch11/TimeClient.java index 59ef451..aa90401 100644 --- a/ch11/TimeClient.java +++ b/ch11/TimeClient.java @@ -3,12 +3,31 @@ */ public class TimeClient { - /** - * Demonstrates that we cannot access private variables from another - * class. - */ public static void main(String[] args) { Time time = new Time(11, 59, 59.9); - System.out.println(time.hour); // ERROR + System.out.println(time); + + // cannot access private variables from another class + // System.out.println(time.hour); + + String s = time.toString(); + System.out.println(s); + + Time time1 = new Time(9, 30, 0.0); + Time time2 = time1; + Time time3 = new Time(9, 30, 0.0); + + System.out.println(time1 == time2); + System.out.println(time1 == time3); + System.out.println(time1.equals(time2)); + System.out.println(time1.equals(time3)); + + Time startTime = new Time(18, 50, 0.0); + Time runningTime = new Time(2, 16, 0.0); + Time endTime = Time.add(startTime, runningTime); + + // using the instance method + endTime = startTime.add(runningTime); } + } From eaf96b152df96ba3af4eef74917d3fff37665eee Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 16:07:13 -0400 Subject: [PATCH 20/24] review ch12..ch14 --- ch12/Card.java | 1 + ch12/CardTable.java | 11 +++--- ch12/Search.java | 66 ++++++++++++++++++++---------------- ch13/Deck.java | 36 ++++++++++++++++++++ ch13/Test.java | 1 + ch14/CardCollection.java | 72 ++++++++++++++++++---------------------- ch14/Deck.java | 1 + ch14/Eights.java | 62 +++++++++++++++++----------------- ch14/Hand.java | 1 + ch14/Player.java | 8 +---- ch14/Test.java | 8 ++--- 11 files changed, 151 insertions(+), 116 deletions(-) diff --git a/ch12/Card.java b/ch12/Card.java index 7bd1202..e7a0661 100644 --- a/ch12/Card.java +++ b/ch12/Card.java @@ -79,4 +79,5 @@ public int position() { public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; } + } diff --git a/ch12/CardTable.java b/ch12/CardTable.java index 5b2b8ca..727d307 100644 --- a/ch12/CardTable.java +++ b/ch12/CardTable.java @@ -7,13 +7,10 @@ import javax.swing.JFrame; public class CardTable extends Canvas { + private Image[][] images; private int cardWidth, cardHeight; - // this long 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; - /** * Creates a CardTable. * cardset is the name of the folder that contains the card images. @@ -29,7 +26,8 @@ public CardTable(String cardset) { char c = suits.charAt(suit); for (int rank = 1; rank <= 13; rank++) { - String s = String.format("%s/%02d%c.gif", cardset, rank, c); + String s = String.format("%s/%02d%c.gif", + cardset, rank, c); images[rank][suit] = new ImageIcon(s).getImage(); } } @@ -79,7 +77,7 @@ public void paint(Graphics g) { public static void main(String[] args) { // make the frame - JFrame frame = new JFrame(); + JFrame frame = new JFrame("Card Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add the CardTable @@ -91,4 +89,5 @@ public static void main(String[] args) { frame.pack(); frame.setVisible(true); } + } diff --git a/ch12/Search.java b/ch12/Search.java index 3cfe2d1..2f83023 100644 --- a/ch12/Search.java +++ b/ch12/Search.java @@ -3,6 +3,30 @@ */ public class Search { + /** + * Make an array of 52 cards. + */ + public static Card[] makeDeck() { + Card[] cards = new Card[52]; + int index = 0; + for (int suit = 0; suit <= 3; suit++) { + for (int rank = 1; rank <= 13; rank++) { + cards[index] = new Card(rank, suit); + index++; + } + } + return cards; + } + + /** + * Displays the given deck of cards. + */ + public static void printDeck(Card[] cards) { + for (int i = 0; i < cards.length; i++) { + System.out.println(cards[i]); + } + } + /** * Sequential search. */ @@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) { while (low <= high) { System.out.println(low + ", " + high); - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 + } else if (comp < 0) { // step 3 low = mid + 1; - } else { // step 4 + } else { // step 4 high = mid - 1; } } @@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) { /** * Binary search (recursive version). */ - public static int binarySearchRec(Card[] cards, Card target, - int low, int high) { + public static int binarySearch(Card[] cards, Card target, + int low, int high) { System.out.println(low + ", " + high); if (high < low) { return -1; } - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 - return binarySearchRec(cards, target, mid + 1, high); - } else { // step 4 - return binarySearchRec(cards, target, low, mid - 1); + } else if (comp < 0) { // step 3 + return binarySearch(cards, target, mid + 1, high); + } else { // step 4 + return binarySearch(cards, target, low, mid - 1); } } - /** - * Make an array of 52 cards. - */ - public static Card[] makeDeck() { - Card[] cards = new Card[52]; - int index = 0; - for (int suit = 0; suit <= 3; suit++) { - for (int rank = 1; rank <= 13; rank++) { - cards[index] = new Card(rank, suit); - index++; - } - } - return cards; - } - /** * Demonstrates how to call the search methods. */ @@ -96,7 +105,8 @@ public static void main(String[] args) { System.out.println(); System.out.println("Recursive binary search"); - System.out.println(binarySearch(cards, jack)); + System.out.println(binarySearch(cards, jack, 0, 51)); System.out.println(); } + } diff --git a/ch13/Deck.java b/ch13/Deck.java index 864d52e..742120e 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -45,12 +45,40 @@ public void print() { } } + /** + * Returns a string representation of the deck. + */ + public String toString() { + return Arrays.toString(this.cards); + } + + /** + * Chooses a random number between low and high, including both. + */ + public int randomInt(int low, int high) { + return 0; + } + + /** + * Swaps the cards at indexes i and j. + */ + public void swapCards(int i, int j) { + } + /** * Randomly permutes the array of cards. */ public void shuffle() { } + /** + * Finds the index of the lowest card + * between low and high inclusive. + */ + public int indexLowest(int low, int high) { + return 0; + } + /** * Sorts the cards (in place) using selection sort. */ @@ -68,6 +96,13 @@ public Deck subdeck(int low, int high) { return sub; } + /** + * Combines two previously sorted subdecks. + */ + public static Deck merge(Deck d1, Deck d2) { + return null; + } + /** * Returns a sorted copy of the deck using merge sort. */ @@ -80,4 +115,5 @@ public Deck mergeSort() { */ public void insertionSort() { } + } diff --git a/ch13/Test.java b/ch13/Test.java index c5431ee..5f49ef1 100644 --- a/ch13/Test.java +++ b/ch13/Test.java @@ -39,4 +39,5 @@ public static void main(String[] args) { deck.insertionSort(); checkSorted(deck); } + } diff --git a/ch14/CardCollection.java b/ch14/CardCollection.java index 5129496..f9c3be2 100644 --- a/ch14/CardCollection.java +++ b/ch14/CardCollection.java @@ -18,44 +18,46 @@ public CardCollection(String label) { } /** - * Returns the label. + * Returns the label of the card collection. */ public String getLabel() { return label; } /** - * Returns the number of cards. + * Adds the given card to the collection. */ - public int size() { - return cards.size(); + public void addCard(Card card) { + cards.add(card); } /** - * True if the collection is empty, false otherwise. + * Removes and returns the card with the given index. */ - public boolean empty() { - return cards.size() == 0; + public Card popCard(int i) { + return cards.remove(i); } /** - * Randomly permute the cards. + * Removes and returns the last card. */ - public void shuffle() { - Random random = new Random(); - for (int i = size() - 1; i > 0; i--) { - int j = random.nextInt(i); - swapCards(i, j); - } + public Card popCard() { + int i = size() - 1; + return popCard(i); } /** - * Swaps the cards at indexes i and j. + * Returns the number of cards. */ - public void swapCards(int i, int j) { - Card temp = cards.get(i); - cards.set(i, cards.get(j)); - cards.set(j, temp); + public int size() { + return cards.size(); + } + + /** + * True if the collection is empty, false otherwise. + */ + public boolean empty() { + return cards.size() == 0; } /** @@ -76,13 +78,6 @@ public void dealAll(CardCollection that) { deal(that, n); } - /** - * Adds the given card to the collection. - */ - public void addCard(Card card) { - cards.add(card); - } - /** * Returns the card with the given index. */ @@ -99,18 +94,23 @@ public Card last() { } /** - * Removes and returns the card with the given index. + * Swaps the cards at indexes i and j. */ - public Card popCard(int i) { - return cards.remove(i); + public void swapCards(int i, int j) { + Card temp = cards.get(i); + cards.set(i, cards.get(j)); + cards.set(j, temp); } /** - * Removes and returns the last card. + * Randomly permute the cards. */ - public Card popCard() { - int i = size() - 1; - return popCard(i); + public void shuffle() { + Random random = new Random(); + for (int i = size() - 1; i > 0; i--) { + int j = random.nextInt(i); + swapCards(i, j); + } } /** @@ -120,10 +120,4 @@ public String toString() { return label + ": " + cards.toString(); } - /** - * Gets the internal cards array (should only be used for testing). - */ - public Card[] getCards() { - return (Card[]) cards.toArray(); - } } diff --git a/ch14/Deck.java b/ch14/Deck.java index 10ba798..8db1814 100644 --- a/ch14/Deck.java +++ b/ch14/Deck.java @@ -15,4 +15,5 @@ public Deck(String label) { } } } + } diff --git a/ch14/Eights.java b/ch14/Eights.java index 78a767b..13b7961 100644 --- a/ch14/Eights.java +++ b/ch14/Eights.java @@ -40,17 +40,6 @@ public Eights() { in = new Scanner(System.in); } - /** - * Displays the state of the game. - */ - public void displayState() { - one.display(); - two.display(); - discardPile.display(); - System.out.print("Draw pile: "); - System.out.println(drawPile.size() + " cards"); - } - /** * Returns true if either hand is empty. */ @@ -75,19 +64,6 @@ public void reshuffle() { drawPile.shuffle(); } - /** - * One player takes a turn. - */ - public void takeTurn(Player player) { - Card prev = discardPile.last(); - Card next = player.play(this, prev); - - System.out.println(player.getName() + " plays " + next); - System.out.println(); - - discardPile.addCard(next); - } - /** * Returns a card from the draw pile. */ @@ -98,13 +74,6 @@ public Card draw() { return drawPile.popCard(); } - /** - * Waits for the user to press enter. - */ - public void waitForUser() { - in.nextLine(); - } - /** * Switches players. */ @@ -116,6 +85,36 @@ public Player nextPlayer(Player current) { } } + /** + * Displays the state of the game. + */ + public void displayState() { + one.display(); + two.display(); + discardPile.display(); + System.out.print("Draw pile: "); + System.out.println(drawPile.size() + " cards"); + } + + /** + * Waits for the user to press enter. + */ + public void waitForUser() { + in.nextLine(); + } + + /** + * One player takes a turn. + */ + public void takeTurn(Player player) { + Card prev = discardPile.last(); + Card next = player.play(this, prev); + discardPile.addCard(next); + + System.out.println(player.getName() + " plays " + next); + System.out.println(); + } + /** * Plays the game. */ @@ -142,4 +141,5 @@ public static void main(String[] args) { Eights game = new Eights(); game.playGame(); } + } diff --git a/ch14/Hand.java b/ch14/Hand.java index a65b7f5..8089a49 100644 --- a/ch14/Hand.java +++ b/ch14/Hand.java @@ -20,4 +20,5 @@ public void display() { } System.out.println(); } + } diff --git a/ch14/Player.java b/ch14/Player.java index c9c9695..433ff28 100644 --- a/ch14/Player.java +++ b/ch14/Player.java @@ -101,13 +101,6 @@ public int score() { return sum; } - /** - * Returns a string representation of the player. - */ - public String toString() { - return name + ": " + hand; - } - /** * Displays the player's hand. */ @@ -121,4 +114,5 @@ public void display() { public void displayScore() { System.out.println(name + " has " + score() + " points"); } + } diff --git a/ch14/Test.java b/ch14/Test.java index 0e5c243..274710c 100644 --- a/ch14/Test.java +++ b/ch14/Test.java @@ -3,13 +3,9 @@ */ public class Test { - /** - * Test code. - */ public static void main(String[] args) { Deck deck = new Deck("Deck"); deck.shuffle(); - System.out.println(deck); Hand hand = new Hand("Hand"); deck.deal(hand, 5); @@ -17,6 +13,8 @@ public static void main(String[] args) { Hand drawPile = new Hand("Draw Pile"); deck.dealAll(drawPile); - System.out.println(drawPile.size()); + System.out.printf("Draw Pile has %d cards.\n", + drawPile.size()); } + } From 390cbbe83b803fe6a01be0baf659804755082de1 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 6 May 2016 09:33:10 -0400 Subject: [PATCH 21/24] sync with 6.1.0 --- LICENSE | 2 +- README.md | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index 749dcd6..89ed830 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index fe3da55..10592d0 100644 --- a/README.md +++ b/README.md @@ -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. - From bc927971a210887c511c9ccd942c6728a79f806c Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 6 Sep 2016 21:27:43 -0400 Subject: [PATCH 22/24] checkstyle 7.1.1 --- ap02/Moire.java | 2 +- ch05/Conditional.java | 2 -- ch05/Exercise.java | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ap02/Moire.java b/ap02/Moire.java index 750bad3..8e7247f 100644 --- a/ap02/Moire.java +++ b/ap02/Moire.java @@ -18,7 +18,7 @@ public static void main(String[] args) { 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; } } diff --git a/ch05/Conditional.java b/ch05/Conditional.java index 1fd36ce..f2d6d19 100644 --- a/ch05/Conditional.java +++ b/ch05/Conditional.java @@ -1,5 +1,3 @@ -import java.util.Scanner; - /** * Examples from Chapter 5. */ diff --git a/ch05/Exercise.java b/ch05/Exercise.java index 46774e0..dbbb6e9 100644 --- a/ch05/Exercise.java +++ b/ch05/Exercise.java @@ -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) { From 9fb1ef4d514055e2e31c56b786ccbd6c384e41a5 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 27 Dec 2016 12:27:10 -0500 Subject: [PATCH 23/24] sync with 6.1.2 --- ch05/Logarithm.java | 12 ++++++------ ch07/Tables.java | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ch05/Logarithm.java b/ch05/Logarithm.java index bc21157..5c5986a 100644 --- a/ch05/Logarithm.java +++ b/ch05/Logarithm.java @@ -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) { @@ -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(); diff --git a/ch07/Tables.java b/ch07/Tables.java index 44e9e62..3e33905 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -6,7 +6,7 @@ public class Tables { public static void example() { int i = 1; while (i < 10) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x)); i = i + 1; } @@ -15,7 +15,7 @@ public static void example() { public static void example2() { int i = 1; while (i < 10) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x) / Math.log(2)); i = i + 1; } @@ -25,7 +25,7 @@ public static void example3() { final double LOG2 = Math.log(2); int i = 1; while (i < 100) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x) / LOG2); i = i * 2; } From 373dd806db8685fdbf7a5fa2e9c8c8eb4028cf3b Mon Sep 17 00:00:00 2001 From: Jayaprabhakar Date: Wed, 15 Nov 2017 03:48:30 -0800 Subject: [PATCH 24/24] Add link to source code hosted on Codiva online java ide Let students directly edit, compile and run java programs on Codiva.io Online Java IDE. This is suitable for beginneers of Java, because they don't have to understand Git, in their early days of learning to code. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 10592d0..0a68248 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Git is a version control system that allows you to keep track of the files that A collection of files under Git's control is called a repository. There are several ways you can work with the code: +* You can edit and run the code on [Codiva online java IDE](https://www.codiva.io/tutorials/thinkjavacode). * You can create a copy of this repository on GitHub by pressing the "Fork" button in the upper right. If you don't already have a GitHub account, you'll need to create one.