Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CSE143 handout #3 CS 132, Winter 2022 Programming Project #3: Letter Inventory (20 points) Due Monday, January 31, 2022, 11:59 PM thanks to Stuart Reges and Marty Stepp for parts of this project This...

2 answer below »

CSE143 handout #3
CS 132, Winter 2022
Programming Project #3: Letter Inventory (20 points)
Due Monday, January 31, 2022, 11:59 PM
thanks to Stuart Reges and Marty Stepp for parts of this project
This project will give you practice creating a class and being a client of it. Although this program is not very exciting in itself,
we will use the LetterInventory class later in another project and it will help us a lot in that project. Turn in files named
LetterInventory.cpp, LetterInventory.h, and inventoryMain.cpp. You may also submit lib132.cpp and
lib132.h if you use them.
Program Description:
In this programming assignment you will practice creating classes, ove
iding operators and being a client of classes you have
written. You are to implement a class called LetterInventory that can be used to keep track of an inventory of letters of the
alphabet. You are also to implement a program with a main in inventoryMain.cpp that prompts the user for two phrases
and prints whether the words are anagrams of each other.
The LetterInventory Class:
The constructor for the LetterInventory class should take a string and compute how many of each letter are in that
string. This is the information the object should keep track of (how many a’s, how many b’s, etc). It should ignore the case
of the letters and ignore anything that is not an alphabetic character (e.g., ignore punctuation characters, digits and anything
else that is not a letter).
Your class should have the following public member functions and operators:
Member Function 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.
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 or if value is negative, your
member function should throw a string exception.
int size() Returns the sum of all 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
member function is called.
ool isEmpty() Returns true if this inventory is empty (all counts are 0). This operation should
e fast in that it should not need to examine each of the 26 counts when it is called.
ool contains(string other)
ool contains(LetterInventory other)
Returns true if this inventory contains all letters at least as many times as they
appear in the passed in string/inventory.

Operator Description
[letter] Returns a count of how many of this letter are in the inventory. letter might be lowercase or uppercase
(your member function shouldn’t care). If a nonalphabetic character is passed, your member function should
throw a string exception.
Outputs a string representation of the inventory with the letters all in lowercase and in sorted order and
su
ounded by square
ackets. The number of occu
ences 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].
+ Constructs and returns a new LetterInventory object that represents the sum of this LetterInventory
and the other given LetterInventory. The counts for each letter should be added together. The two
LetterInventory objects being added together (this and other) should not be changed by this operator.
- Constructs and returns a new LetterInventory object that represents the result of subtracting the other
inventory from this inventory (i.e., subtracting the counts in the other inventory from this object’s counts). If
any resulting count would be negative, your member function should throw a string exception. The two
LetterInventory objects being subtracted (this and other) should not be changed by this operator.
The inventoryMain Program:
Your inventoryMain.cpp should contain a client program that uses your LetterInventory class. It should prompt the
user for two words and print a message telling the user if they are anagrams of each other. See two sample outputs below:
Welcome to the CS 132 anagram solver.
Enter two words, phrases, or series of letters
to find out if they are anagrams of each other.
First word/phrase/letters: the Morse Code
Second word/phrase/letters: here come dots
They are anagrams!
Welcome to the CS 132 anagram solver.
Enter two words, phrases, or series of letters
to find out if they are anagrams of each other.
First word/phrase/letters: listen
Second word/phrase/letters: silence
So
y, no anagrams here!
User input is shown in bold and blue to make it easier to distinguish. Your program does not need to display it in bold or in
lue, but it should otherwise match the above output exactly.
Hints:
Below is an example of how the + operator should work:
LetterInventory inventory1("George W. Bush");
LetterInventory inventory2("Hillary Clinton");
LetterInventory sum = inventory1 + inventory2;
The first inventory would co
espond to [beegghorsuw], the second would co
espond to [achiilllnnorty] and the third
would co
espond to [abceegghhiilllnnoo
stuwy].
You should implement this class with an a
ay of 26 counters (one for each letter) along with any other member variables you
find that you need. Remember, though, that we want to minimize the number of member variables when possible.
You might be tempted to implement the + operator by calling the
operator but you are not allowed to use that approach
ecause it would be inefficient for inventories with large character counts.
You should use a class constant for the value 26 to add to readability.
You will need to know certain things about the properties of letters and type char that we discussed in CS& 131. Look back
at the Caesar Cipher example it you are having trouble remembering how characters and integers work together. One of the
most important ideas is that values of type char have co
esponding 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 type char using less-than and
greater-than tests, as in:
if (ch >= 'a') {
...
}
All the lowercase letters appear grouped together in type char ('a' is followed by 'b' followed by 'c', and so on) and all 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 variable letter is of type char and stores a lowercase letter):
letter - 'a'
Going in the other direction, if you know a character’s integer equivalent, you can cast the result to char to get the character.
For example, suppose that you want to get the letter that is 8 away from 'a'. You could say:
char result = (char) ('a' + 8);
This assigns the variable result the value 'i'.
As in these examples, you should write your code in terms of displacement from a fixed letter like 'a' rather than including the
specific integer value of a character like 'a'.
You will want to look at the built-in string and character functions and possibly write some of your own.
Development Strategy:
One of the most important techniques for software professionals is to develop code in stages rather than trying to write it all at
once (the technical term is iterative enhancement or stepwise refinement). It is also important to be able to test the co
ectness
of your solution at each different stage. It was harder to do this in project 2 as you were using a provided main. However, in
this project you will write the main and additional test code in stages, so it matches the parts of your LetterInventory you
have implemented so far.
Since this is the first project where you will be writing a class and a program that uses it, we will provide you with a detailed
development strategy and some recommendations for testing code to write. We aren’t going to provide instructions for
exhaustive testing code, but we’ll give you some ideas of where to start.
We are suggesting that you develop the program in three stages:
1. In this stage we want to test constructing a LetterInventory and examining it’s contents. So, the member functions
we will implement are the constructor, the size member function, the isEmpty member function, the [] operator,
and the
operator. Even within this stage you can develop the member functions and operators slowly. We suggest
writing them in the following order:
a. The constructor and size member function.
. Add the isEmpty member function.
c. Then add the [] operator.
d. Then add the
operator.
Write a testing program that will call each of these functions and operators as you write them. Always test after you
add something new! Make sure you test with multiple inputs.
2. In this stage add the set member function to the class so that the client can change the number of occu
ences of an
individual letter.
Add code to your testing program to verify that your other member functions and operators work properly in
conjunction with set. You can test this by mixing calls to set in with calls to your other member functions and
operators.
3. In this stage we want to add the two contains member functions. Write the version that takes a LetterInventory
first and test it before moving on to the version that takes a string parameter.
4. In this stage we want to add the + and - operators. You should write the + operator first and make sure it works. Only
once you have + working and tested move on to adding -.
5. Finally, write the required client code in inventoryMain.cpp. This should not be your only testing code! It does not
test many member functions and edge cases of LetterInventory.
You are welcome to discuss how to write testing code with other students. You can discuss exactly what functions and operators
to use, in what order to use them
Answered 5 days After Jan 28, 2022

Solution

Chirag answered on Feb 02 2022
123 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here