Computer Science 261
Assignment # 5
Due: Wednesday, Oct. 10
Purpose:
- To develop the first in a series of programs exploring data structures
that can be used to store a list of objects with a standard interface.
To learn the advantages and disadvantages of each approach. In the
course of the next several exercises in 'lists of objects', we will cover
- Storing objects in linked lists
- Storing objects in binary search trees
- (if time permits) Hash tables and indexes
- Please note that Java already contains built-in data structures to do all
this. Our purpose is to learn how they work so that we can make
informed choices about which one to use in a particular situation, and also
to be able to use them in programming languages that are not as rich in data
structures as is Java.
Write an ArrayInventoryObject class description for parts with the
following fields and methods:
- Fields
- Array of part objects (from earlier assignment)
- Inventory size (actual number of parts in the inventory)
- sorted (an internal flag to say if the array is in order by the PNo
field).
- Methods
- Constructor accepting an integer which represents the maximum
number of objects in the inventory. This constructor will then
create the array of part objects with that many elements in it.
- Other Methods
- print - prints a listing of all the parts in the inventory.
You can do this by using the PartObject's print method, or by
using the GetPartNumber, etc. commands to get the contents of
individual fields and generate a nicely formatted listing.
The list should be in ascending part number order. If it is
not, the sort routine (see below) should be called to sort the array.
- AddPart - accepts a part object as an argument, adds it to
the list. If the array is full, or if the part already exists, print a message and return doing
nothing. Important note: We will later want to throw an exception
in this sort of case. Please don't do this here - the test driver program
will not be written to catch exceptions.
- DeletePart - accepts a part number as an argument) and deletes the corresponding
object if it is in the inventory. If not in the inventory, print
a message and return doing nothing. As discussed Tuesday, you can
delete either by moving everything up into the cell that has been
deleted (preserving the sort order), or by moving the last element
of the array into the location being deleted.
- FindPart - accepts a part number as an argument and returns the part
with that part number if found. If not found returns null. If the
array is in order by part number, use a binary search, otherwise use
a sequential search Note that this public routine will also be
useful internally in AddPart (to check for an existing part), and
DeletePart.
- sort (a private routine, only for use by the print routine). Sort
the array in ascending order by part number.
I will shortly provide a main program to test your program out
Questions? Please ask!