Computer Science 15-110 (Lecture 4), Spring 2010
Homework 8
Due: Thu 25-Mar-2009 at 10pm (email copy to ytay) (no late submissions
accepted).
Hw8 Submission Coordinator: ytay
This is non-collaborative,
so follow the same instructions as
hw1, only
replace "hw1" with "hw8" throughout. There are no
restrictions on what Java constructs you may use.
- Mystery Methods
- Transpose Musical Notes
- Scrabble Score
- The Television Class
- The Coke Machine Class
-
Mystery Methods
In the written portion of your submission, answer the following
questions in general, and
in just a few words of plain
English.
-
In general, what does the following method do?
public static int mystery1(String[] a) {
if (a == null) return 0;
int result = 0;
for (int i=0; i<a.length; i++)
if (a[i] == null)
result++;
return result;
}
-
In general, what does the following method do?
//
Note: mystery2 uses the mystery1 method from abovepublic static String[] mystery2(String[] a) {
if (a == null) return null;
String[] result = new String[a.length - mystery1(a)];
int j = 0;
for (int i=0; i<a.length; i++)
if (a[i] != null) {
result[j] = a[i];
j++;
}
return result;
}
-
In general, what does the following method do?
public static int[] mystery3(int[] a, int x) {
if (a == null) return null;
int[] result = new int[a.length + 1];
for (int i=0; i<a.length; i++)
result[i] = a[i];
result[a.length] = x;
return result;
}
-
In general, what does the following method do?
public static boolean mystery4(int x) {
if (x < 2) return false;
for (int i=2; i<x; i++)
if (x % i == 0)
return false;
return true;
}
-
In general, what does the following method do?
//
Note: mystery5 uses both the mystery3 and the mystery4 methods from
abovepublic static int[] mystery5(int n) {
int[] result = new int[0];
for (int i=0; i<=n; i++)
if (mystery4(i))
result = mystery3(result, i);
return result;
}
-
Transpose Musical Notes
In order to have an interesting application of arrays, we will use a
simple musical note player. First, download and compile the following
program: MusicalNotePlayer.java.
Run it, listen to the results. Now, look inside that file. You will see a
method with this signature:
public static void
playNotes(int[] notes, int[] times) throws Exception {
}
Here is a simple explanation of how this works, taken from the header
comment in the file:
// Simple demo of a
simple musical note player.
// It takes two arrays -- one containing the notes, the other containing
the times for each note.
// For the notes, 60 is middle C. 61 is C# (the next note). 62 is D (the
note after that).
// Legal note range: 1 to 88 (inclusive)
// For the times, 64 is a typical quarter note, 256 a typical whole note.
// Legal time range: 1 to 256 (inclusive)
// If the "times" array is null, it will play all quarter notes.
You do not need
to understand music theory in order to complete this exercise. However, the
methods you write here will create arrays of integers which can be used as
arguments to the "playNotes" method. So you can hear your
results! Fun!
Now, in the file Hw8.java, write the method:
public static int[]
transposeNotes(int[] notes, int step) {
}
This method makes a song higher-pitched or lower-pitched. This method takes
a non-null array of notes (as described above), and another int, the "step",
and returns a new array of notes where each note in the resulting
"transposed" array equals the corresponding note in the original array plus
the (possibly-negative) step. There is this exception, however: in no case
shall you transpose so that any notes fall outside of the legal range of
[1,88]. In such a case, you should reduce the step's magnitude by just
enough to keep the transposed result in range. For example, if the highest
note in the incoming array is 80, and the step is +10, then you should
reduce the step to +8. Similarly, if the lowest note in the incoming array
is 8, and the step is -10, you should change the step to be -7. Be sure to
include an appropriate test method! Also, for fun, try transposing the
sample song in the MusicalNotePlayer, perhaps as such:
for (int step=0; step <
12; step++)
playNotes(transpose(songNotes,step), songTimes); // a familiar song,
now transposed repeatedly
You should hear the song played repeatedly, increasing its pitch by a little
bit (one-half step) each time it plays.
- Scrabble Score
In the game of Scrabble, players receive points for spelling words,
where each letter has an associated point value (1 for A, 3 for B, 3 for C,
2 for D, and so on...). For example, ignoring special squares (which we
will), the word "CAB" is worth 3+1+3, or 7 points. For more details, here
is the Wikipedia page on Scrabble:
http://en.wikipedia.org/wiki/Scrabble
In the file Hw8.java, write the method:
public static int
scrabbleScore(String s, int[] letterValues)
This method takes a String s and an array of 26 integers representing
point values of the letters from A to Z in order, and returns the sum of the
point values for each letter in the string. If the string is null or empty
or if it contains any characters besides uppercase letters, or if the
letterValues array is null or not of length 26, the result should be -1 to
indicate an error.
If you are interested, here is an array holding the actual letter values for
the English-language version of Scrabble:
private static int[]
letterValues = {
// A, B, C, D, E, F, G, H, I, J, K, L, M
1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
// N, O, P, Q, R, S, T, U, V, W, X, Y, Z
1, 1, 3,10, 1, 1, 1, 1, 4, 4, 8, 4,10
};
Note: The letterValues array may contain point values besides the
official point values (say, 25 for 'A', 18 for 'B', -33 for 'C', and so
on). Your method must work for whatever letterValues array is provided.
- The Television Class
Start with this file: Hw8Television.java
Do not modify the Hw8Television main class. Make this code work by adding
the appropriate classes with the appropriate methods as described by the
test methods called by this main method. Note that you do not have to add
any code to the test cases, though you do have to solve them with
general-purpose solutions (and not just hard-code the example test cases!).
Hint #1: look carefully at the test code to infer the behavior of the
Television class. It is straightforward (no tricks, really!), but you will
not be provided with any description beyond this test code. "Use the force,
read the source!"
Hint #2: to solve this incrementally, you may wish to comment out parts of
the test code so the parts you have implemented will compile and can be
tested as you go.
- The Coke Machine Class
Start with this file: Hw8CokeMachine.java
As with the preceding problem, do not modify this problem's main class, and
make this code work by adding the appropriate classes with the appropriate
methods as described by the test methods called by this main method. Same
hints apply, too.
Carpe diem!