This mini-lab may be done individually or in pairs. It is okay to get help from the TAs and/or the instructor if you get stuck, but you (and your partner, if you have one) should try to do it on your own first.
Tip: You should get into the habit of making backup copies of your work.
A common pattern in computer programming is to have a collection of items that one has to traverse (step through) in order to print information about the items, search for a particular item, do a calculation based on all the items, etc.
The table below shows a number of different contexts for such collections, but all of them have a few characteristics in common. The items in each of these collections include:
For example, a program keeping track of runners in a marathon or swimmers in a swim meet might include a race number, athlete name, team name, and finish time. A program tracking grocery store inventory might keep track of item codes, descriptions, department (produce, meat, deli, etc), and price. Software for an art gallery might include an ID number for each art work on sale, a title or desciption, the name of the artist, and price. Environmental researchers might keep track of the number of members of various plant or animal species in a geographic location by recording each species name, whether it is endangered or in some other category, and how many individuals (or maybe mating pairs) are in the location being studied.
Context | Item Type | ID | Name/Description | Category | Numeric Comparator |
---|---|---|---|---|---|
Athletic Event | Athlete | Race Number | Athlete Name | Team or Country | Finish time, number of goals, etc. |
Collection | Coin/Stamp | ID | Description | Country | Value |
Menu | Menu Items | Name | Description | Appetizer, Dessert, etc. | Price (or Calories) |
Store | Inventory Items | Item Code | Name | Department | Price |
Garden | Plants | Item Code | Plant Name | Category | Number of plants (or rows or acres) to plant |
Ecol. Research | Species | Latin Name | Common Name | Endangered, Critical, etc. | # Individuals |
Gallery | Pieces of Art | Item Code | Title/Description | Artist | Price |
Library | Books, Songs, Albums | ID | Name | Author, Artist | Price (or Length) |
Cookie Sales | Boxes of Cookies | Address | Family | Type of Cookie | Number of boxes |
Faculty List | Faculty | ID | Name | Department | Years of Service |
toString()
that should
return a string containing all four characteristics, nicely formatted.
For example, if the collection were K faculty, their department
affiliation, and their years at
K, the toString()
method might return:
"123456: Alyce Brady, Computer Science, 21".
main
method yet, by constructing an
object of your class (right-click or control-click on the class diagram
and select the constructor) and invoking your accessor methods
one-by-one (right-click or control-click on the method names).
Edit
the main method to construct at least four items using the constructor
you defined above, putting them in four variables (for example,
item1
, item2
, etc.).
Print the values of your new items using your new
toString()
method. For example, if item1
were
one of the four items you constructed, you could print its information
with the following code.
System.out.println(item1.toString());
Tip: If you pass an object toSystem.out.println
it will always look for atoString
method, so you don't have to call thetoString
method explicitly. The code below is equivalent to the code above.System.out.println(item1);
You do not need to submit this mini-lab. You will be adding to it in a follow-up mini-lab.