Before you start, fill out a "Project Plan" in PSP
with estimates of how long the program will be, how many defects you'll
encounter, and how long it'll take you. (You will not be graded on how
accurate your estimates are, but you are expected to make some
kind of reasonable estimate.)
As you work on the programming part of the assignment,
keep track of all the error messages and other
defects you encounter using the
PSP forms
(click on "Input" under "Defect Removal Data").
(If you want to practice keeping time logs with
the PSP forms, you're welcome to, but they're not required for this assignment.)
-
This is a program to represent the inventory of a music store. You are
to enhance your Homework 2 in several ways:
Modify the Instrument class so that, instead of having
a "kind" instance variable, it has several subclasses for different
kinds of instruments: a Guitar subclass, a Harmonica
subclass, a Keyboard subclass, etc. Instrument
itself should probably become an abstract class.
- Each of these subclasses should
have appropriate instance variables: for example, the Guitar
subclass might have an instance variable numberOfStrings,
since different guitars have different numbers of strings.
- Each of these subclasses should have a constructor that
initializes its instance variables appropriately.
- Each of these subclasses should have a toString()
method that returns a printable description of
the instrument, in a format you find informative and useful (I would
recommend, at least, including the values of all the instance
variables, and what specific kind of instrument it is).
- Each subclass should of course have a test method. In
addition, the now-abstract Instrument class should have a
test method that creates several different kinds of
instruments and tests each one of them.
Modify the Inventory class to be a little more
realistic:
- Write a toString method that produces a printable
description of the inventory, listing all the instruments in an
informative and useful format. (Hint: you can use
the toString method you've already written for the
Instrument classes.)
- Write an add method that takes an Instrument
parameter and adds it to the array. This is tricky,
since the size of an array is fixed when it is first created. For
this assignment, I recommend creating a new array, one element
larger than the old one, copying all the elements of the old one
into the new one, adding the new element at the end of the new one,
and re-setting the array instance variable in Inventory to
refer to the new array instead of the old one. This isn't the most
efficient solution, but it's reasonably simple; we'll learn more
efficient approaches later in the semester.
- Write a remove method that takes in the index of an
element in the array of Instruments, and removes that
element from the array (if it exists; if not, your method should do
nothing at all). Again, for this assignment I recommend creating a
new array one element smaller than the old one, copying into the new
array all the elements of the old one except the one you're
trying to remove, and re-setting the array instance variable in
Inventory to refer to the new array instead of the old one.
- Modify the test method by adding several test cases for
add and remove. After each call to add
or remove, print out the current version of the Inventory
to make sure the element was correctly added or removed.
-
For each of the methods, and each of the classes, be sure to
follow the design recipe! If you come to me for help
writing the body of a method, and you don't already have a purpose,
contract, examples, and header, I'll tell you to go away.
-
How to turn this in: Once you've got all of this working,
run all of your "test" methods and save the output in a file named
"output.txt" in the same folder as your program. Then "WinZip" the
entire folder into a single file and
send me an
e-mail, attaching this ZIP file.
If you have any comments about problems you encountered in writing the
program, or things you learned, put these in the body of the e-mail.
-
Partners: As mentioned above, I would like you to switch
partners from one homework assignment to the next. Since this
assignment is a modification of homework 2, the new partnership may
choose the homework 2 of either of the two partners as a starting point;
one of the two partners will presumably be familiar with it, and the
other will have the real-life experience of reading and modifying
someone else's program.