spacer.gif

spacer.gif

Purpose

In 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:

  1. To further expose you to writing computer programs that manipulate abstract representations of collections of objects. In particular, for this assignment we will introduce you to the Java Hashtable class.

  2. To show you how to use a specialized collection class to solve an interesting problem

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.

Background

The 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:

  • When you are retrieving an element from the Hashtable, you need to cast it into a String, as shown on the 10th line. We will see why this is necessary at the very end of the course.

  • When you are attempting to retrieve a mapping of a key that isn't in the Hashtable, the String "null" will be printed, as on the 12th line.

Compiler Warnings

When 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 Methods

The following are the methods defined in the Hashtable class (and thus applicable to Hashtable objects) that you will be using for this homework.

  • Hashtable (): The constructor, it creates a new Hashtable object.

  • Object put (Object key, Object value): Adds the mapping of Object key to Object value to the Hashtable.

  • Object get (Object key): Searches the Hashtable to find the value mapped to key. Returns "null" if the key is not found.

  • Object remove (Object key): Removes the key and its associated value from the Hashtable

  • clear (): Clears the Hashtable (removes all keys from it)

  • int size (): Returns the number of keys in the Hashtable.

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.)

Design

In 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.
Pirate Speak:    ahoy proud beauty, whar be th' barnacle-covered buried treasure? I be needin' t' seize all yer doubloons.

English:  pardon me sir, could you help me find the old hotel?  I would like to take a nap.
Pirate Speak:  avast matey, know ye th' barnacle-covered fleabag inn? I be needin' t' have a bit of a lie-down.

The program that you write should do the following:

  1. Print a descriptive header

  2. Declare:

    • a Scanner object that will be used to read in the English sentence that the user would like translated.
    • a Hashtable object that will be used as the English-to-Pirate Speak dictionary (this is done in the skeleton code)
  3. Fill the Hashtable with the English-to-Pirate Speak mappings.  (See the table below).  Some (but not all -- see below) of this is done in the skeleton code.
  4. Print out the size of the dictionary and its contents to verify that the dictionary has been filled in the way you expect.
  5. Prompt the user separately for individual inputs and read them in using nextLine(), in the order given above.  Your prompts should tell the user the possible values for the category. In other words, your program should first prompt the user to input a greeting choice, then read that input into a variable so that the Hashtable, or dictionary, can be searched later.  Then, prompt the user for a person choice and read that  choice in using nextLine(), and so on.
  6. Print out the phrases that the user has entered for translation, adding the necessary punctuation. You may assume that a comma will always follow the <person>, a question mark will always follow the <place>, and a period will always follow the action.  Further, you may assume that all translations you will need to generate will contain a choice for each part of the sentence.  Later on in the course, we will see ways to have a sentence that isn't rigidly in this format, but for now this works.
  7. Search the Hashtable for each of the English words or phrases and retrieve the equivalent Pirate Speak.
  8. Print out the Pirate Speak translation. 

Below is a table that summarizes some basic English to Pirate Speak phrases.

 

Category

English

Pirate Speak

Greetings

hello

ahoy

pardon me

avast

excuse me

arrr

Persons

sir

matey

madam

proud beauty

miss

comely wench

stranger

scurvy dog

officer

foul blaggart

Questions

where is

whar be

can you help me find

know ye

is that

be that

how far is it to

how many leagues to

Articles

the

th’

a

a briny

any

some forsaken

nearby

broadside

my

me

your

yer

Adjectives

old

barnacle-covered

attractive

comely

happy

grog-filled

Places

restaurant

galley

hotel

fleabag inn

mall

market

pub

Skull & Scuppers

bank

buried treasure

Desires

I would like to

I be needin’ t’

I desire

I've a fierce fire in me belly t'

I wish I knew how to

I be hankerin' t'

my mother told me to

me dear ol' mum, bless her  soul, tol' me t'

my companion would like to

me mate, ol' Rumpot, wants t'

Actions

find

come across

take a nap

have a bit of a lie-down

make a withdrawal

seize all me gold

have a cocktail

swill a pint or two o' grog

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.

Warnings

As 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 practices

The good programming practices listed in HW 1 must be included in this (and all) homeworks.

Sample execution

The 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!

spacer.gif

spacer.giffooter-middle.gifspacer.gif