char[] word = {'n','e','v','e','r'}; char[] word2 = {'r','i','v','e','r'}; char[] word3 = {'e','v','e','r'}; char[] suffix = commonSuffix (word, word2); printArray (suffix); suffix = commonSuffix (word, word3); printArray (suffix);should print "ver" and "ever". Call your program Suffix.java
char[] word = {'m','a','d','a','m','e'}; // Remove e. char[] word2 = {'p','l','o','p',}; // Remove l char[] word3 = {'p','r','e','f','e','r'}; // Remove p char[] word4 = {'n','e','v','e','r'}; // Won't work. checkAlmostPalindrome (word); checkAlmostPalindrome (word2); checkAlmostPalindrome (word3); checkAlmostPalindrome (word4);should print
madame is an almost-palindrome: remove e in position 5 plop is an almost-palindrome: remove l in position 1 prefer is an almost-palindrome: remove p in position 0 never is NOT an almost-palindromeCall your program AlmostPalindrome.java. To get started, you could write a method that takes in an array, and a designated array index, and then returns a new, shorter array with the element at that index removed.
static char[] shorten (char[] A, int k) { // Return a new array, of length A.length-1 // with element k removed. }You already have a method (from class) that checks whether a char-array is a palindrome. Can you use these methods together to solve the almost-palindrome problem?
To hear this tune, download SoundTool.java. Then, in a separate program called
MyMusic.java
type the following
public class MyMusic {
public static void main (String[] argv)
{
String music = "CCDDCCEECCDDCCEE";
SoundTool.playMusic (music);
}
}
Your program and
SoundTool.java
need to be in the same directory.
Now compile and execute. Make sure that the sound volume is turned up
on your computer. (Note: this will NOT work on hobbes.)
Now to the exercise. Write a method that produces a randomly generated string using the seven musical notes. The string should be at least 24 letters long. Then, play this string as music with the code in main. So, how to generate such a string randomly? First, find the integer codes that correspond to 'A' through 'G'. Then, all you need to do is generate random integers in this range, and cast to char. To generate a random integer, you can generate a random real number in the desired range, and use the Math.round method to round the real number to an integer. Bring your program to class on Tuesday.
Recommended, but not required:
This week, we'll explore computers and music.