handout #7

CSE143—Computer Programming II

Programming Assignment #2

due: Sunday, 4/10/05, 9 pm

In this programming assignment we will continue to practice using arrays and classes.  You are to implement a class called LetterInventory that can be used to keep track of an inventory of letters of the alphabet.  The constructor for the class takes a String and computes how many of each letter are in the String.  This is the information the object keeps track of (how many a’s, how many b’s, etc).  It ignores the case of the letters and ignores anything that is not an alphabetic character (e.g., it ignores punctuation characters, digits and anything else that is not a letter).

Your class should have the following public methods.

Method

Description

LetterInventory(String data)

Constructs an inventory (a count) of the alphabetic letters in the given string, ignoring the case of letters and ignoring any non-alphabetic characters

int get(char letter)

Returns a count of how many of this letter are in the inventory.  Letter might be lowercase or uppercase (your method shouldn’t care).  If a nonalphabetic character is passed, your method should throw an IndexOutOfBoundsException

void set(char letter, int value)

Sets the count for the given letter to the given value.  Letter might be lowercase or uppercase.  If a nonalphabetic character is passed, your method should throw an IndexOutOfBoundsException

int size()

Returns the sum of all of the counts in this inventory.  This operation should be “fast” in that  it should store the size rather than having to compute it each time this method is called.

boolean isEmpty()

Returns true if this inventory is empty (all counts are 0).  This operation should be fast in that it should not need to examine each of the 26 counts when it is called.

String toString()

Returns a String representation of the inventory with the letters all in lowercase and in sorted order and surrounded by square brackets.  The number of occurrences of each letter should match its count in the inventory.  For example, an inventory of 4 a’s, 1 b, 1 l and 1 m would be represented as “[aaaablm]”.

LetterInventory add(LetterInventory other)

Constructs and returns a new LetterInventory object that represents the sum of this letter inventory and the other given LetterInventory.  The counts for each letter should be added together.

LetterInventory subtract(LetterInventory other)

Constructs and returns a new LetterInventory object that represents the result of subtracting the other inventory from this inventory (i.e., subtracing the counts in the other inventory from this object’s counts).  If any resulting count would be negative, your method should return null.

You should implement this class with an array of 26 counters (one for each letter) along with any other data fields you find that you need.

You will need to know certain things about the properties of letters and type char.  You might want to look at the Character class for useful methods (e.g., there is a toLowerCase method).  You can compare different values of type char using less-than and greater-than tests.  All of the lowercase letters appear grouped together in type char (‘a’ is followed by ‘b’ followed by ‘c’, and so on) and all of the uppercase letters appear grouped together in type char (‘A’ followed by ‘B’ followed by ‘C’ and so on).  Because of this, you can compute a letter’s displacement (or distance) from the letter “a” with an expression like the following (this expression assumes the letter is a lowercase letter):

letter - 'a'

You should write your own testing program to make sure that your class works properly, but you will not have to turn in your testing program.

In terms of correctness, your class must provide all of the functionality described above and your size and isEmpty methods must be fast, as described.  In terms of style, we will be grading on your use of comments, good variable names, consistent indentation and good coding style to implement these operations.

You should name your file LetterInventory.java and you should turn it in electronically from the “assignments” link on the class web page.