From 64b73a64c122a5c2504f79763855b680ad82321a Mon Sep 17 00:00:00 2001 From: drichardi Date: Mon, 12 Dec 2016 23:15:33 -0500 Subject: [PATCH 1/7] Update Series.java added notes --- ch06/Series.java | 71 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/ch06/Series.java b/ch06/Series.java index 10d2b41..693a496 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -3,6 +3,7 @@ */ public class Series { + //standard void method public static void countup(int n) { if (n == 0) { System.out.println("Blastoff!"); @@ -12,41 +13,90 @@ public static void countup(int n) { } } + //value method, must do something with the return value + //Compared to void methods, value methods differ in two ways + //They declare the type of the return value (the return type) + //They use at least one return statement to provide a return value + + //takes a double as a parameter and returns the area of a circle with a radius public static double calculateArea(double radius) { - double result = Math.PI * radius * radius; - return result; + double result = Math.PI * radius * radius; //temporary variables make debugging easier, especially stepping through + return result; //return immediately from this method and use the following expression as the return value } - + //same function written more concisely + //note return expression must match declared return type public static double calculateArea2(double radius) { return Math.PI * radius * radius; } - + //multiple return statements, one in each branch of a conditional + //only one will be executed + //code that cannot be executed is called dead code public static double absoluteValue(double x) { if (x < 0) { return -x; } else { return x; } + System.out.println("This line is dead code."); } - + public static double absoluteValue(double x) { + if(x < 0) { + return -x; + } else if(x> 0) { + return x; + } + //syntax error - every possible path must reach a return statement + //no return path for x == 0 -error: missing return statement + + //Writing Methods + //Incremental Development + //Start with a working program and make small, incremental changes. + //At any point, if there is an error, you will know where to look. + + //Use variables to hold intermediate values so you can check them, + //either with print statements or by using a debugger. + + //Once the program is working, you can consolidate multiple statements into + //compound expressions (but only if it does not make the program more difficult + //to read). + + //Distance Formula - distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)) + //what are inputs? -- parameters + //what is output? -- return value + + //use to make 'stub' contains method signature and a return statement + public static double distance + (double x1, double y1, double x2, double y2) { + return 0.0; //placeholder needed for program to compile. + } + //Good idea to think about testing before making methods + //in main double dist = distance(1.0,2.0,4.0,6.0); + //Horizontal distance is 3.0 and vertical 4.0, answer 5.0 (3,4,5) triangle + //helpful to know right answer + + + + //first step - find differences of x2-x1 and y2-y1 + //store in temp variables 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); + //remove when finished called "scaffolding" + System.out.println("dx is " + dx); //should be 3.0 + System.out.println("dy is " + dy); //should be 4.0 return 0.0; } - + //next step to square distances 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); + System.out.println("dsquared is " + dsquared); //should get 25.0 return 0.0; } - + //finally sqrt to get answer and return result public static double distance3 (double x1, double y1, double x2, double y2) { double dx = x2 - x1; @@ -56,6 +106,7 @@ public static double absoluteValue(double x) { return result; } + //METHOD COMPOSITION public static double circleArea (double xc, double yc, double xp, double yp) { double radius = distance(xc, yc, xp, yp); From 84ec8feeeb07af6ca0cadc9dadf359d53310820a Mon Sep 17 00:00:00 2001 From: drichardi Date: Sun, 18 Dec 2016 21:10:54 -0500 Subject: [PATCH 2/7] Update Loops.java --- ch07/Loops.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ch07/Loops.java b/ch07/Loops.java index dfc03ce..a1c8765 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -10,7 +10,8 @@ public static void countdown(int n) { } System.out.println("Blastoff!"); } - + //Collatz_conjecture - still not proven/disproven + // facts do lean towards being true. public static void sequence(int n) { while (n != 1) { System.out.println(n); From 29af598cddc727d74baa7b2a2b136b6974d51494 Mon Sep 17 00:00:00 2001 From: drichardi Date: Tue, 3 Jan 2017 20:12:01 -0500 Subject: [PATCH 3/7] Update ArrayExamples.java --- ch08/ArrayExamples.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ch08/ArrayExamples.java b/ch08/ArrayExamples.java index a4cb6c3..06e54c9 100644 --- a/ch08/ArrayExamples.java +++ b/ch08/ArrayExamples.java @@ -10,9 +10,11 @@ public class ArrayExamples { */ public static void main(String[] args) { int size = 10; + //declare and initialize int[] counts = new int[4]; double[] values = new double[size]; + //[] brackets to access elements of an array, use anywhere counts[0] = 7; counts[1] = counts[0] * 2; counts[2]++; @@ -31,16 +33,18 @@ public static void main(String[] args) { } int[] array = {1, 2, 3, 4}; - printArray(array); + printArray(array); //make this function // printing an array as an object - System.out.println(array); + System.out.println(array); //shows type, address // printing with Arrays class System.out.println(Arrays.toString(array)); // copying an array + double[] a = {1.0, 2.0, 3.0}; + //double[] b = a; creates a reference to the same array. double[] b = new double[a.length]; for (int i = 0; i < a.length; i++) { b[i] = a[i]; @@ -55,11 +59,11 @@ public static void main(String[] args) { } // search - int index = search(a, 2.0); + int index = search(a, 2.0);//make this function System.out.println("index = " + index); // reduce - double total = sum(a); + double total = sum(a); //make this function System.out.println("total = " + total); } From 58637ff8e39045606460e9d2dab7578dd8df3542 Mon Sep 17 00:00:00 2001 From: drichardi Date: Tue, 3 Jan 2017 20:15:12 -0500 Subject: [PATCH 4/7] Update ArrayExamples.java --- ch08/ArrayExamples.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch08/ArrayExamples.java b/ch08/ArrayExamples.java index 06e54c9..707821b 100644 --- a/ch08/ArrayExamples.java +++ b/ch08/ArrayExamples.java @@ -57,6 +57,9 @@ public static void main(String[] args) { for (int i = 0; i < a.length; i++) { a[i] = Math.pow(a[i], 2.0); } + //take the above block and make a method called powArray that takes a double array + //and returns a new array that contain the elements of the argument array squared. + //generalize it to take a second parameter and raise the elements to the given power. // search int index = search(a, 2.0);//make this function From fadfd8b87d65eb2e51589119627bb9609c6be66d Mon Sep 17 00:00:00 2001 From: drichardi Date: Fri, 6 Jan 2017 00:01:04 -0500 Subject: [PATCH 5/7] Update Histogram.java --- ch08/Histogram.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch08/Histogram.java b/ch08/Histogram.java index 32b029c..5fe4fd6 100644 --- a/ch08/Histogram.java +++ b/ch08/Histogram.java @@ -44,11 +44,25 @@ public static void main(String[] args) { // making a histogram int[] counts = new int[100]; + for(int i = 0; i < counts.length; i++) { + count[i] = inRange(scores, i, i+1); + } + + //more efficient loop that only traverses array once for (int i = 0; i < scores.length; i++) { int index = scores[i]; counts[index]++; } - + + //foreach + //basic for loop + for(int i = 0; i < values.length; i++){ + System.out.println(values[i]); + } + //written as for each + for(int value : values) { + System.out.println(value); + } // histogram with enhanced for loop counts = new int[100]; for (int score : scores) { From e54458e322dbf4aff73791cfac31d1642b0cd3ff Mon Sep 17 00:00:00 2001 From: drichardi Date: Fri, 6 Jan 2017 00:09:45 -0500 Subject: [PATCH 6/7] Update Fruit.java --- ch08/Fruit.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ch08/Fruit.java b/ch08/Fruit.java index 50428c5..045a524 100644 --- a/ch08/Fruit.java +++ b/ch08/Fruit.java @@ -1,5 +1,14 @@ /** * Fruit exercise. + * The purpose of this exercise is to practice reading code and recognizing + * the traversal patterns we have learned so far. The following methods + * are hard to read, because instead of using meaningul names for the variables + * and methods, they use the names of fruit. + * + * For each method, write one sentence that describes what the method does, without + * getting into the details of how it works. + * + * For each variable, identify the role it plays. */ public class Fruit { From a9b76003ca1c2c395bfe16fb637290270553ffda Mon Sep 17 00:00:00 2001 From: drichardi Date: Fri, 6 Jan 2017 00:13:07 -0500 Subject: [PATCH 7/7] Update Histogram.java --- ch08/Histogram.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ch08/Histogram.java b/ch08/Histogram.java index 5fe4fd6..ec411ea 100644 --- a/ch08/Histogram.java +++ b/ch08/Histogram.java @@ -68,6 +68,12 @@ public static void main(String[] args) { for (int score : scores) { counts[score]++; } + + /* For the foreach loop above, write a method called histogram that + * takes an int array of scores from 0 to (but not including) 100, and + * returns a histogram of 100 counters. Generalize it to take the number + * counters as an argument. + */ } }