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); } + }