- No lesson this week
- Read text chapter 9
I’m not requiring the MyProgrammingLab exercises for this chapter, but you may want to complete them anyway as preparation for the assignment and final exam.
Assignment 10.1 [95 points]
The WordCruncher class
Write the Java code for the class WordCruncher. Include the following members:
A default constructor that sets the instance variable ‘word’ to the string “default”.
A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must have no whitespace and no punctuation. If the String parameter has whitespace or punctuation, set the instance variable to the string “default” instead. (This restriction will make your work on some of the following methods much easier.)
A method int numLetters() that returns the number of letters in the instance variable.
A method int numVowels() that returns the number of vowels in the instance variable.
A method boolean beginsWithVowel() that returns true if the first letter of the instance variable is a vowel, and false otherwise.
A method String toPigLatin() that returns a String containing the ‘pig latin’ version of the instance variable.
The rules for translating a word to pig latin are:
- If the word begins with a consonant, take the first letter of the word and move it to the end of the word, followed by ‘ay’
- If the word begins with a vowel, add ‘way’ to the word. Hint: the method beginVowel() makes this easier.
PIG LATIN IS FUN becomes IGPAY ATINLAY ISWAY UNFAY in pig latin.
A method String toGibberish() that returns a String containing the ‘gibberish’ version of the instance variable. The rules for translating a word into gibberish are:
- If the word begins with a consonant, follow the first letter with ‘itha’, and place a ‘g’ before the rest of the original word. So the word ‘big’ would translate to ‘bithagig’.
- If the word begins with a vowel, place ‘itha’ at the front, and put a ‘g’ in front of the original word. So the word ‘is’ becomes ‘ithagis’.
A method String reverse() that returns a String that contains the characters of the instance variable, but in reverse.
A method int numCharOccurrences(char ch) that returns a count of the number of times the parameter char ch occurs in the instance variable.
Write a class named WordCruncherTest that has only a main method that:
- asks the user for a word with the option to enter the word “quit” to quit
- creates a WordCruncher object that contains this word (unless the word is “quit”) and then:
- outputs the number of letters in this object
- outputs the number of vowels in this object
- output the object string in reverse
- outputs the pig latin translation of the string stored in the object
- outputs the gibberish translation of the string stored in the object
- asks the user to enter one letter, and returns a message indicating how many occurrances of that letter are in the word
The program should continue to do this until the user enters the word “quit”
Submit Your Work
Execute the program and copy/paste the output into the bottom of your WordCruncher.java file, making it into a comment. Send an email to email@example.com with the subject “CIS 254 a10”. Attach your two files to the email. In the body of the email let me know whether the programs work as required.
Keep in mind that if your code does not compile you will receive a 0.
Note that the gmail email account is for homework submission only. The account is not monitored for any other purpose.
Assignment 10.2 [5 points]
Participate in the assignment 10 discussion. This could involve asking a question, answering another student’s question, giving an example of something that you struggled with and then overcame (or didn’t!), giving an example of something you found particularly cool, or any other constructive way you can think of to participate.