CSE 143: Computer Programming II
This assignment will assess your mastery of the following objectives:
- Implement a well-designed Java class to meet a given specification.
- Maintain proper abstraction between the client and implementation of a class.
- Follow prescribed conventions for code quality, documentation, and readability.
Overview
In this assessment, you will implement a class calledLetterInventorythat can be used to keep track
of an inventory of letters of the English alphabet. The constructor for the class will take aStringas
a parameter and compute how many of each letter are in thatString(i.e. how many a’s, how many
b’s, etc.). LetterInventoryignores any character that is not an English letter (such as punctuation or
digits) and treats upper- and lowercase letters as the same.
YourLetterInventoryclass should include the following constructor:
1 | public LetterInventory (String data) |
Your class should also include the following public methods:
1 | You must in- |
1 | public int get (char letter) |
1 | public void set (char letter, int value) |
1 | public int size () |
1 | public boolean isEmpty () |
1 | public String toString () |
Summer 2021
Take-home Assessment 1: Letter Inventory due J uly 1 , 2021 11: 59 pm
1 | public LetterInventory add (LetterInventory other) |
1 | public LetterInventory subtract (LetterInventory other) |
You may also include any additional private helper methods you think will be helpful.
1 | Make sure any |
As an example, the add method could be called as follows:
1 | LetterInventory inventory1 = new LetterInventory("Sherlock Holmes"); |
Here, inventory1would contain[ceehhkllmoorss],inventory2would contain[adhjnnoorstw],
andsumwould contain[acdeehhhjkllmnnoooorrssstw].
Implementation Guidelines
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. Remember, though, that we want to minimize the number of data
fields when possible.
Your class should avoid unnecessary inefficiencies. For example, you might be tempted to implement the
addmethod by calling thetoStringmethod or otherwise building aStringto pass to theLetterInventory
constructor. But this approach would be inefficient for inventories with large character counts.
You should introduce a class constant for the value 26 to improve readability.
Character operations
It will be helpful to understand certain deatils of thechardatatype for this assessment. Many of these
details are explained in section 4.3 of the textbook.
Values of typecharhave corresponding integer values. There is a character with value 0, a character
with value 1, a character with value 2 and so on. You can compare different values of typecharusing
less-than and greater-than tests, as in:
if (ch >= ‘a’) …
All of the lowercase letters appear grouped together in typechar(i.e.’a’is followed by’b’followed by
’c’, and so on). All of the uppercase letters appear grouped together similarly. 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 variableletteris of typecharand stores a lowercase letter):
letter -‘a’
Going in the other direction, if you know a characters integer equivalent, you can cast the result tochar
to get the character. For example, suppose that you want to get the letter that is 8 away from’a’. You
could do this as follows:
char result = (char) (‘a’+ 8);
This would assign the variableresultthe value’i’. As in these examples, you should write your code
in terms of displacement from a fixed letter like’a’rather than finding and including the specific integer
value (e.g. 97) of a character like’a’.
Hints
Thought it may not seem like it, theArrayIntListexample from lecture provides a good model to use for
implementingLetterInventory. Pay particular attention to the use of fields, avoiding reimplementation
of common functionality, throwing exceptions in error conditions, and documentation/comments.
String and Character
You will likely want to look at the JavaStringandCharacterclasses for useful methods. (For example,
there is atoLowerCasemethod in each.) You will have to pay attention to whether each method is static
or not. TheStringmethods are mostly instance methods because strings are objects. TheCharacter
methods are all static becausecharis a primitive type. For example, if you have a variable calledsthat
is aString, you can turn it to all lowercase as follows:
s = s.toLowerCase();
This is a call to an instance method on an object, so you put the name of the object variable before the
dot. Butcharvalues are not objects and thetoLowerCasemethod in theCharacterclass is a static
method. So if you have a variable calledchthat is of typechar, you would turn it to all lowercase as
follows:
ch = Character.toLowerCase(ch);
Development Strategy
One of the most important techniques for programmers is to develop code in stages rather than trying to
write it all at once. (The technical term for this is “iterative enhancement” or “stepwise refinement.”) It
is also important to be able to test the correctness of your solution at each different stage.
We suggest that you work on your assessment in three stages:
(a) First, work on constructing aLetterInventoryand examining its contents. We will implement the
constructor, thesizemethod, theisEmptymethod, thegetmethod, and thetoStringmethod.
Even within this stage, you should develop the methods slowly. First work on the constructor and
sizemethods. Then add theisEmptymethod, then thegetmethod, then thetoStringmethod.
1 | (b) Next, add thesetmethod to the class that allows the client to change the number of occurrences |
1 | (c) Finally, include theaddandsubtractmethods. We recommend writing theaddmethod first and |
Code Quality Guidelines
In addition to producing the desired behavior, your code should be well-written and meet all expectations
described in thegrading guidelines, Code Quality Guide, andCommenting Guide. For this assessment,
pay particular attention to the following elements:
Data Fields
Properly encapsulate your objects by making data your fieldsprivate. Avoid unnecessary fields; use
fields to store important data of your objects but not to store temporary values only used in one place.
Fields should always be initialized inside a constructor or method, never at declaration.
Exceptions
The specified exceptions must be thrown correctly in the specified cases. Exceptions should be thrown
as soon as possible, and no unnecessary work should be done when an exception is thrown. Exceptions
should be documented in comments, including the type of exception thrown and under what conditions.
Commenting
Each method should have a header comment including all necessary information as described in the
Commenting Guide. Comments should be written in your own words (i.e. not copied and pasted from
this spec) and should not include implemenation details.
Running and Submitting
If you believe your behavior is correct, you can submit your work by clicking the “Mark” button in the Ed
assessment. You will see the results of some automated tests along with tentative grades. These grades
are not final until you have received feedback from your TA.
You may submit your work as often as you like until the deadline; we will always grade your most recent
submission. Note the due date and time carefully— work submitted after the due time will not be
accepted.
Getting Help
If you find you are struggling with this assessment, make use of all the course resources that are available
to you, such as:
- Reviewing relevant examples fromclass
- Reading the textbook
- Visitingoffice hours
- Posting a question on themessage board
Collaboration Policy
Remember that, while you are encouraged to use all resources at your disposal, including your classmates,
all work you submit must be entirely your own. In particular, you should NEVER look at a solution
to this assessment from another source (a classmate, a former student, an online repository, etc.). Please
review thefull policyin the syllabus for more details and ask the course staff if you are unclear on whether
or not a resource is OK to use.
Reflection
In addition to your code, you must submit answers to short reflection questions. These questions will
help you think about what you learned, what you struggled with, and how you can improve next time.
The questions are given in the fileLetterInventoryReflection.txtin the Ed assessment; type your
responses directly into that file.