Contents [0/4] |
Video [1/4] |
Homework (MyFirstHomework) [2/4] |
Uploading homework [3/4] |
How to talk about code via email [4/4] |
(Click here for one slide per page)
Video [1/4] |
In two parts
Homework (MyFirstHomework) [2/4] |
Install the tools for class. (See separate notes.)
Look on D2L for quiz to complete.
Look on D2L for homework to hand in.
Read this intro to elipse from IBM: Getting Started
Read chapter 1 of Core Java for the Impatient.
Skim chapter 2 of Core Java for the Impatient.
Skim Algorithms (the primary text) through the end of 1.2
There's a free online version in case you don't have the text yet. Check out the links for the textbook on the course homepage.
file:MyFirstHomeworkFor300.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package algs11; import java.util.Arrays; import stdlib.*; /** * This is a skeleton file for your homework. Edit the sections marked TODO. You * may also edit the function "main" to test your code. * * You must not change the declaration of any method. This will be true of every * skeleton file I give you. * * For example, you will get zero points if you change the line * * <pre> * public static int minPosition (double[] list) { * </pre> * * to something like * * <pre> * public static double minPosition (double[] list) { * </pre> * * or * * <pre> * public static int minPosition (double[] list, int i) { * </pre> * * Each of the functions below is meant to be SELF CONTAINED. This means that * you should use no other functions or classes. You should not use any HashSets * or ArrayLists, or anything else! In addition, each of your functions should * go through the argument array at most once. The only exception to this the * java Math.abs. You can use the Math.abs function to compute the absolute * value. */ public class MyFirstHomeworkFor300 { /** * minValue returns the minimum value in an array of doubles. You can assume the * array is nonempty and has no duplicates. Your solution must go through the * array exactly once. Your solution must not call any other functions. Here are * some examples (using "==" informally): * * <pre> * -7 == minValue (new double[] { -7 }) * 1 == minValue (new double[] { 1, 7, 8, 11 }) * -7 == minValue (new double[] { 1, -4, -7, 7, 8, 11 }) * -13 == minValue (new double[] { -13, -4, -7, 7, 8, 11 }) * -13 == minValue (new double[] { 1, -4, -7, 7, 8, 11, -13 }) * </pre> */ public static double minValue(double[] list) { return StdRandom.random(); // TODO: remove this line, add correct answer } /** * minPosition returns the position of the minimum value in an array of doubles. * The first position in an array is 0 and the last is the array.length-1. * * You can assume the array is nonempty and has no duplicates. Your solution * must go through the array exactly once. Your solution must not call any other * functions. Here are some examples (using "==" informally): * * <pre> * 0 == minPosition(new double[] { -7 }) * 2 == minPosition(new double[] { 1, -4, -7, 7, 8, 11 }) * 0 == minPosition(new double[] { -13, -4, -7, 7, 8, 11 }) * 6 == minPosition(new double[] { 1, -4, -7, 7, 8, 11, -9 }) * </pre> */ public static int minPosition(double[] list) { return StdRandom.uniform(100); // TODO: remove this line, add correct answer } /** * distanceBetweenMinAndMax returns difference between the minPosition and the * maxPosition in an array of doubles. * * You can assume the array is nonempty and has no duplicates. Your solution * must go through the array exactly once. Your solution must not call any other * functions. Here are some examples (using "==" informally): * * <pre> * 0 == distanceBetweenMinAndMax(new double[] { -7 }) // -7,-7 are the min and max * 3 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11 }), // -7,11 * 5 == distanceBetweenMinAndMax(new double[] { -13, -4, -7, 7, 8, 11 }) // -13,11 * 1 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 }) // -9,11 * </pre> */ public static int distanceBetweenMinAndMax(double[] list) { return StdRandom.uniform(100); // TODO: remove this line, add correct answer } /** * A test program, using private helper functions. See below. To make typing * tests a little easier, I've written a function to convert strings to arrays. * See below. */ public static void main(String[] args) { // for minValue: array must be nonempty with unique elements testMinValue(-7, "1 -4 -7 7 8 11 9 -5"); testMinValue(-0.5, "0.2 -0.5 -0.1"); testMinValue(9, "11 9 21 31 41"); testMinValue(9, "11 21 9 31 41"); testMinValue(9, "11 21 31 9 41"); testMinValue(9, "11 21 31 41 9"); testMinValue(9, "9 11 21 31 41"); testMinValue(-99, "-11 -99 -21 -31 -41"); testMinValue(-99, "-11 -21 -99 -31 -41"); testMinValue(-99, "-11 -21 -31 -99 -41"); testMinValue(-99, "-11 -21 -31 -41 -99"); testMinValue(-99, "-99 -11 -21 -31 -41"); testMinValue(11, "11"); testMinValue(-11, "-11"); // for minPosition: array must be nonempty with unique elements testMinPosition(2, "1 -4 -7 7 8 11 9 -5"); testMinPosition(1, "0.2 -0.5 -0.1"); testMinPosition(1, "11 9 21 31 41"); testMinPosition(2, "11 21 9 31 41"); testMinPosition(3, "11 21 31 9 41"); testMinPosition(4, "11 21 31 41 9"); testMinPosition(0, "9 11 21 31 41"); testMinPosition(1, "-11 -99 -21 -31 -41"); testMinPosition(2, "-11 -21 -99 -31 -41"); testMinPosition(3, "-11 -21 -31 -99 -41"); testMinPosition(4, "-11 -21 -31 -41 -99"); testMinPosition(0, "-99 -11 -21 -31 -41"); testMinPosition(0, "11"); testMinPosition(0, "-11"); // for distanceBetweenMinAndMax: array must be nonempty with unique elements testDistanceBetweenMinAndMax(3, "1 -4 -7 7 8 11 9 -5"); testDistanceBetweenMinAndMax(3, "0.1 -0.4 -0.7 0.7 0.8 1.1 0.9 -0.5"); testDistanceBetweenMinAndMax(4, "9 11 21 31 41"); testDistanceBetweenMinAndMax(3, "11 9 21 31 41"); testDistanceBetweenMinAndMax(2, "11 21 9 31 41"); testDistanceBetweenMinAndMax(1, "11 21 31 9 41"); testDistanceBetweenMinAndMax(1, "11 21 31 41 9"); testDistanceBetweenMinAndMax(4, "9 -11 -21 -31 -41"); testDistanceBetweenMinAndMax(3, "-11 9 -21 -31 -41"); testDistanceBetweenMinAndMax(2, "-11 -21 9 -31 -41"); testDistanceBetweenMinAndMax(1, "-11 -21 -31 9 -41"); testDistanceBetweenMinAndMax(1, "-11 -21 -31 -41 9"); testDistanceBetweenMinAndMax(0, "11"); testDistanceBetweenMinAndMax(0, "-11"); StdOut.println("Finished tests"); } /* * A main function for debugging -- change the name to "main" to run it (and * rename the existing main method to something else). Change the test as * appropriate. */ public static void main1(String[] args) { Trace.drawStepsOfMethod("minValue"); Trace.run(); testMinValue(9, "11 21 9 31 41"); } /* Test functions --- lot's of similar code! */ private static void testMinValue(double expected, String list) { double[] aList = doublesFromString(list); double actual = minValue(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed minValue([%s]): Array modified\n", list); } if (expected != actual) { StdOut.format("Failed minValue([%s]): Expecting (%.1f) Actual (%.1f)\n", list, expected, actual); } } private static void testMinPosition(int expected, String list) { double[] aList = doublesFromString(list); int actual = minPosition(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed minPosition([%s]): Array modified\n", list); } if (expected != actual) { StdOut.format("Failed minPosition([%s]): Expecting (%d) Actual (%d)\n", list, expected, actual); } } private static void testDistanceBetweenMinAndMax(int expected, String list) { double[] aList = doublesFromString(list); int actual = distanceBetweenMinAndMax(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed distanceBetweenMinAndMax([%s]): Array modified\n", list); } if (expected != actual) { StdOut.format("Failed distanceBetweenMinAndMax([%s]): Expecting (%d) Actual (%d)\n", list, expected, actual); } } /* A utility function to create an array of doubles from a string. */ // The string should include a list of numbers, separated by single spaces. private static double[] doublesFromString(String s) { if ("".equals(s)) return new double[0]; // empty array is a special case String[] nums = s.split(" "); double[] result = new double[nums.length]; for (int i = nums.length - 1; i >= 0; i--) { try { result[i] = Double.parseDouble(nums[i]); } catch (NumberFormatException e) { throw new IllegalArgumentException( String.format("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i])); } } return result; } }
file:MyFirstHomeworkFor300PartTwo.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package algs11; import java.util.Arrays; import stdlib.*; /** * This is a skeleton file for your homework. Edit the sections marked TODO. You * may also edit the function "main" to test your code. * * You must not change the declaration of any method. This will be true of every * skeleton file I give you. * * For example, you will get zero points if you change the line * * <pre> * public static double minValue (double[] list) { * </pre> * * to something like * * <pre> * public static void minValue (double[] list) { * </pre> * * or * * <pre> * public static double minValue (double[] list, int i) { * </pre> * * Each of the functions below is meant to be SELF CONTAINED. This means that * you should use no other functions or classes. You should not use any HashSets * or ArrayLists, or anything else! In addition, each of your functions should * go through the argument array at most once. The only exception to this * removeDuplicates, which is allowed to call numUnique and then go through the * array once after that. */ public class MyFirstHomeworkFor300PartTwo { /** * allSame returns true if all of the elements in list have the same value. * allSame returns false if any two elements in list have different values. The * array may be empty and it may contain duplicate values. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions. Here are some examples * (using "==" informally): * * <pre> * true == allSame(new double[] { }) * true == allSame(new double[] { 11 }) * true == allSame(new double[] { 11, 11, 11, 11 }) * false == allSame(new double[] { 11, 11, 11, 22 }) * false == allSame(new double[] { 11, 11, 22, 11 }) * true == allSame(new double[] { 22, 22, 22, 22 }) * </pre> */ public static boolean allSame(double[] list) { return StdRandom.bernoulli(); } /** * numUnique returns the number of unique values in a sorted array of doubles. * The array may be empty and it may contain duplicate values. Assume that the * list array is sorted. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions. Here are some examples * (using "==" informally): * * <pre> * 0 == numUnique(new double[] { }) * 1 == numUnique(new double[] { 11 }) * 1 == numUnique(new double[] { 11, 11, 11, 11 }) * 8 == numUnique(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 }) * 8 == numUnique(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 }) * </pre> */ public static int numUnique(double[] list) { return StdRandom.uniform (100); //TODO: fix this } /** * removeDuplicates returns a new array containing the unique values in the * sorted argument array, in the same order that they were found in the original * array. There should not be any extra space in the array --- there should be * exactly one space for each unique element (Hint: numUnique tells you how big * the array should be). * * Assume that the list array is sorted, as you did for numUnique. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions, except numUnique. Here are * some examples (using "==" informally): * * <pre> * new double[] { } * == removeDuplicates(new double[] { }) * new double[] { 11 } * == removeDuplicates(new double[] { 11 }) * == removeDuplicates(new double[] { 11, 11, 11, 11 }) * new double[] { 11, 22, 33, 44, 55, 66, 77, 88 } * == removeDuplicates(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 }) * == removeDuplicates(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 }) * </pre> */ public static double[] removeDuplicates(double[] list) { int resultLength = numUnique(list); double[] result = new double[resultLength]; // TODO: fix this return result; } /* * A main function for debugging -- change the name to "main" to run it (and * rename the existing main method to something else). Change the test as * appropriate. */ public static void main2(String[] args) { Trace.drawStepsOfMethod("removeDuplicates"); Trace.run(); testRemoveDuplicates("11 21 31 41", "11 21 31 41"); testRemoveDuplicates("11 21", "11 11 21"); } /** * A test program, using private helper functions. See below. To make typing * tests a little easier, I've written a function to convert strings to arrays. * See below. */ public static void main(String[] args) { testAllSame(true, "11 11 11 11"); testAllSame(true, "5 5 5"); testAllSame(false, "11 5 11 11"); testAllSame(false, "11 11 5 11"); testAllSame(false, "11 11 11 5"); testAllSame(false, "5 11 11 11"); testAllSame(false, "11 5 5 11 11"); testAllSame(false, "11 11 5 5 11"); testAllSame(false, "11 11 5 11 5"); testAllSame(false, "5 5 11 11 11"); testAllSame(false, "11 11 11 5 5"); testAllSame(true, "11"); testAllSame(true, "2"); testAllSame(true, ""); // for numUnique: array must be sorted testNumUnique(4, "11 21 21 21 31 41 41 41 41"); testNumUnique(1, "11 11 11 11"); testNumUnique(4, "11 21 31 41"); testNumUnique(4, "11 11 11 21 31 31 31 31 41"); testNumUnique(4, "11 11 21 21 21 31 31 41 41 41 41"); testNumUnique(8, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71 81 81"); testNumUnique(8, "11 21 31 41 41 41 41 41 51 51 61 71 81"); testNumUnique(7, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71"); testNumUnique(7, "11 21 31 41 41 41 41 41 51 51 61 71"); testNumUnique(8, "-81 -81 -81 -81 -71 -61 -51 -51 -51 -51 -41 -41 -31 -21 -11 -11 -11"); testNumUnique(3, "-11 -11 -11 0 0 11 11 11"); testNumUnique(2, "0 11 11 11"); testNumUnique(2, "-Infinity 11 11 11"); testNumUnique(2, "11 11 11 Infinity"); testNumUnique(1, "11 11"); testNumUnique(1, "11"); testNumUnique(0, ""); // for removeDuplicates: array must be sorted testRemoveDuplicates("11", "11 11 11 11"); testRemoveDuplicates("11 21", "11 11 21"); testRemoveDuplicates("11 21 31 41", "11 21 31 41"); testRemoveDuplicates("11 21 31 41", "11 11 11 21 31 31 31 31 41"); testRemoveDuplicates("11 21 31 41", "11 21 21 21 31 41 41 41 41"); testRemoveDuplicates("11 21 31 41", "11 11 21 21 21 31 31 41 41 41 41"); testRemoveDuplicates("11 21 31 41 51 61 71 81", "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71 81 81"); testRemoveDuplicates("11 21 31 41 51 61 71 81", "11 21 31 41 41 41 41 41 51 51 61 71 81"); testRemoveDuplicates("11 21 31 41 51 61 71", "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71"); testRemoveDuplicates("11 21 31 41 51 61 71", "11 21 31 41 41 41 41 41 51 51 61 71"); testRemoveDuplicates("-81 -71 -61 -51 -41 -31 -21 -11", "-81 -81 -81 -81 -71 -61 -51 -51 -51 -51 -41 -41 -31 -21 -11 -11 -11"); testRemoveDuplicates("-11 0 11", "-11 -11 -11 0 0 11 11 11"); testRemoveDuplicates("0 11", "0 11 11 11"); testRemoveDuplicates("-Infinity 11", "-Infinity 11 11 11"); testRemoveDuplicates("Infinity 11", "Infinity 11 11 11"); testRemoveDuplicates("11", "11 11"); testRemoveDuplicates("11", "11"); testRemoveDuplicates("", ""); StdOut.println("Finished tests"); } private static void testAllSame(boolean expected, String list) { double[] aList = doublesFromString(list); boolean actual = allSame(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed allSame([%s]): Array modified\n", list); } if (expected != actual) { StdOut.format("Failed allSame([%s]): Expecting (%b) Actual (%b)\n", list, expected, actual); } } private static void testNumUnique(int expected, String list) { double[] aList = doublesFromString(list); int actual = numUnique(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed numUnique([%s]): Array modified\n", list); } if (expected != actual) { StdOut.format("Failed numUnique([%s]): Expecting (%d) Actual (%d)\n", list, expected, actual); } } private static void testRemoveDuplicates(String expected, String list) { double[] aList = doublesFromString(list); double[] actual = removeDuplicates(aList); if (!Arrays.equals(aList, doublesFromString(list))) { StdOut.format("Failed removeDuplicates([%s]): Array modified\n", list); } double[] aExpected = doublesFromString(expected); // != does not do what we want on arrays if (!Arrays.equals(aExpected, actual)) { StdOut.format("Failed removeDuplicates([%s]): Expecting (%s) Actual (%s)\n", list, Arrays.toString(aExpected), Arrays.toString(actual)); } } /* A utility function to create an array of doubles from a string. */ // The string should include a list of numbers, separated by single spaces. private static double[] doublesFromString(String s) { if ("".equals(s)) return new double[0]; // empty array is a special case String[] nums = s.split(" "); double[] result = new double[nums.length]; for (int i = nums.length - 1; i >= 0; i--) { try { result[i] = Double.parseDouble(nums[i]); } catch (NumberFormatException e) { throw new IllegalArgumentException( String.format("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i])); } } return result; } }
Uploading homework [3/4] |
After you have completed the assignment, save your work in eclipse and use drag-and-drop to upload the file to D2L.
If drag-and-drop does not work, you can use an alternative method, shown below.
Be very careful to upload a .java
file from the src
directory.
Do not upload a .class
file from the bin
directory.
How to talk about code via email [4/4] |
If you want advice about an error, send me email, doing one of the following.
Include a screenshot showing the error.
Copy and paste the EXACT TEXT of the error into your email message.
If you have a problem getting a program to work and you want me to look at some of your code in more detail, send me email with the following three things.
Your java file as an attachment.
A description of how the output of the program is different from what you expected.
The output of your program, if it runs.
Revised: 2008/03/17 13:01