PurposeIn the previous homework assignment, you learned about the Java Vector class, which implements a mutable ordered collection. Computer programs often need to manipulate representations of collections of things, and there are many ways to do so. The goals of this homework assignment are two-fold:
A Hashtable represents a mutable unordered collection. By mutable we mean that a program, as it runs, can change the membership of a collection, adding and deleting members. An object of type Hashtable that represents an empty collection when a program starts running might represent a collection with many members by the time the program terminates. By the term unordered, we mean that the members of the collection are not arranged in a linear order. Hashtables are collections which maintain a mapping or correspondence of one object (called a key) to another object (called a value), much like a dictionary stores the correspondence between words (key) and their meanings (value). Just like searching through a dictionary, a Hashtable can be searched for a value by searching for the corresponding key. The Java Hashtable class defines a set of methods to manipulate Java Hashtable objects – which is to say, to manipulate abstractions of unordered collections which have the key-value mapping quality mentioned earlier. There is one file that you have to submit for this homework, called PirateSpeak.java. We are providing you with skeleton code: PirateSpeak.java, which has some (but not all -- see below) of the words entered for the pirate speak translation. BackgroundThe Hashtable class defines methods for (among other things) adding and searching for "things" from Hashtable objects. For this homework, we will be storing Strings in a Hashtable object (objects of class Hashtable). The following code illustrates the use of a Hashtable. Hashtable myHashtable = new Hashtable(); // Creates a new Hashtable object myHashtable myHashtable.put ("hello", "ahoy"); // Adds the mapping of "hello" to "ahoy" into // myHashtable myHashtable.put ("madam", "proud beauty"); // Adds the mapping of "madam" to "proud beauty" // into myHashtable. At this point, the // Hashtable contains two elements System.out.println (myHashtable.size()); // Prints the size (2) to the screen System.out.println (myHashtable); // Prints the entire Hashtable (meaning the // mappings of each element in the Hashtable) // to the screen String greeting = (String) myHashtable.get("hello"); // Searches for the value mapped to "hello" // (in other words, "ahoy") from the Hashtable, // and stores it in the String variable, greeting System.out.println("hello's value is: " + greeting); // Print out the result of searching for "hello" String person = (String) myHashtable.get("sir"); // Searches for the value mapped to "sir" System.out.println("sir's value is: " + person); // Print out the result of searching for "sir" // NOTE: the key "sir" is NOT in myHashtable, // so null will be printed A few things to notice in this code segment:
Compiler WarningsWhen you compile your program for HW 3 (and the above code), you will receive the following warning: Note: PirateSpeak.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. This is a warning, not an error message. In general, it’s very important that programmers not ignore such warning messages. They sometimes constitute evidence of problems with your code. For this homework assignment, however, you can ignore it. This particular warning is due to how the Hashtable class was written by the designers of Java. Your program will still have compiled properly (assuming there were no other error messages). Hashtable MethodsThe following are the methods defined in the Hashtable class (and thus applicable to Hashtable objects) that you will be using for this homework.
As mentioned above, any time you obtain an element from the Hashtable (via get(), or remove()), you need to cast the returned element back as a String, as shown below. String searchResult = (String) myHashtable.get("madam"); Lastly, you will notice that the methods are defined to take in and return values of type Object, not String. For this homework, you can ignore the distinction between Object and String by treating them as if they are the same thing. (There are complications here that we’re not yet prepared to explain or understand, so we’ll just gloss over them for now.) DesignIn this assignment, you will be writing a simplified English-to-Pirate Speak Translator. It is based on the periodic holiday, International Talk Like a Pirate Day, which was invented in 1995 by two Americans, John Baur and Mark Summers, and is celebrated on September 19 of each year. The general structure and grammar of a Pirate Speak sentence that we will use is as follows: <greeting> <person>, <question> <article> <adjective> <place>? <desire> <action>. For example, here are some English sentences and their corresponding Pirate Speak:
English: hello madam, where is the
old bank? I would like to make a withdrawal.
English: pardon me sir, could you help me
find the old hotel? I would like to take a nap. The program that you write should do the following:
Below is a table that summarizes some basic English to Pirate Speak phrases.
We are providing you with skeleton code, PirateSpeak.java, which has some of the words entered for the pirate speak translation. In particular, the four actions are not entered -- that we are leaving to you. Note that all words (in both English and pirate-speak) are all lower case, except for proper nouns ('Skull & Scuppers' and 'Rumpot') and the word 'I'. You are welcome to add additional words, if you would like -- you can do a Google search for 'pirate dictionary', and a number of such sites come up. Note that if you do add more words, be sure you add them to the given categories above -- don't add new categories! And don't remove any words that are there, as that is what we will be testing on. This won't get any extra credit, but is just if you want to play around a bit more. WarningsAs the course progresses, we will provide you with fewer step-by-step instructions for the homeworks -- for example, we don't specify that you need to create a Scanner object to obtain the input (this should be obvious from the fact that you need to get user input), or that you need to print out a legend (which is included in the good programming practices, below). The easiest way is to progress through the steps above, in order, is to use incremental development. Start with the Java program that does nothing. Make sure it runs properly. Then add one or two steps worth of code at a time, and test them to make sure they work. Move ahead only when you have justifiable confidence that your program so far is right. Doing a simple test or two is easy (although comprehensive program testing is very hard in the general case): just run your program and see if it works properly up to that point. The steps where you print out the Vectors allow you to see “what's going on”, i.e., to see the current states of the vector objects, and to see how the just-executed statements changed their states. Although there are many steps, each step is relatively small, and most will only take a few lines of code. The Scanner class sometimes acts a bit finicky, and this homework may encounter some strange behavior. For this homework, we will be testing your code with Strings that may contain spaces, so use nextLine()) to read in a String. Since you will be exclusively using nextLine() in your program, this should allow you to avoid any of these issues. Note: You should only create ONE Scanner object in your code -- creating more than one will cause your program to not work properly with the grading routines, which will cause a loss of points. Good programming practicesThe good programming practices listed in HW 1 must be included in this (and all) homeworks. Sample executionThe following is a sample execution run. User input is indicated in red. Responses by the program based on the user input are indicated in green.Welcome to the English-Pirate Speak Translation Program! This program will prompt you through entering an English sentence and will translate it to Pirate Speak for you. Note that capitalization matters! So entering 'hello' is not the same as entering 'Hello'. The size of the dictionary is: 35 Here are the contents of the English-Pirate Speak dictionary: {my mother told me to=me dear ol' mum, bless her soul, tol' me t', old=barnacle-covered, nearby=broadside, happy=grog-filled, stranger=scurvy dog, I wish I knew how to=I be hankerin' t', take a nap=have a bit of a lie-down, sir=matey, miss=comely wench, excuse me=arrr, I desire=I've a fierce fire in me belly t', mall=market, bank=buried treasure, the=th', where is=whar be, make a withdrawal=seize all me gold, my=me, any=some forsaken, can you help me find=know ye, find=come across, officer=foul blaggart, hello=ahoy, my companion would like to=me mate, ol' Rumpot, wants t', pub=Scull & Scuppers, have a cocktail=swill a pint or two o' grog, restaurant=galley, attractive=comely, your=ye, pardon me=avast, I would like to=I be needin' t', a=a briny, hotel=fleabag inn, how far is it to=how many leagues to, is that=be that, madam=proud beauty} Please enter a greeting: hello | pardon me | excuse me: pardon me Please enter a person: sir | madam | miss | stranger | officer : sir Please enter a question: where is | can you help me find | is that | how far is it to : how far is it to Please enter an article: the | a | any | nearby | my | your : any Please enter an adjective: old | attractive | happy : old Please enter a place: restaurant | hotel | mall | pub | bank : pub Please enter a desire: I would like to | I desire | I wish I knew how to | my mother told me to | my companion would like to : I would like to Please enter an action: find | take a nap | make a withdrawal | have a cocktail: take a nap You entered: pardon me sir, how far is it to any old pub? I would like to take a nap. Here's your sentence translated into Pirate Speak: avast matey, how many leagues to some forsaken barnacle-covered Skull & Scuppers? I be needin' t' have a bit of a lie-down. Thank you for using the English-to-Pirate Speak Translator! Ahoy, have a good day, arrr! SubmissionWhen you are finished, submit the PirateSpeak.java file.
|