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.
Before starting this mini-lab, you should have completed the earlier mini-lab on Creating Items to go in a Collection.
add
method that takes an appropriate item as a
parameter and adds it to the ArrayList representing the collection. For
example, the add
method might take a RareCoin object as a
parameter and add it to a coin collection ArrayList.
main
method (in the Main Application class you
created in the previous mini-lab). After the existing code that creates
several collection items, construct an object of your new class and
repeatedly call its add
method to add the items to your
collection.
Write a method in your new class that prints all the items in the list,
making use of the toString
method for individual items that
you wrote in the previous mini-lab.
Test your new method by calling it from your
main
method after the items have been added to the
collection.
Write a method that calculates the sum or average of the numeric value in your collection (e.g., the average finish time for the runners in a race, the sum of all the calories of food items eaten in the day, the number of plants being studied in a research area, etc.). Your method should return the sum or average, rather than printing it.
Test your new method by calling it from your main
method
and then printing the result. (The call to
System.out.println
should be in your main
method, not in your new method.)
Write a method that searches through your collection to find and return a
particular item. You may return the whole item, or a particular
attribute of the item. For example, if you were to search through a
list of movie reviews for a review by a particular reviewer, you could
return either the entire review object or just the rating given by that
reviewer.
Test your new method by calling it from your main
method
and printing your result.
Tip: For this method, you should set up your search so that you will find at most one item. In other words, the parameter(s) you pass to the search method should uniquely identify a single item. For example, your parameter could be the runner's race number for a marathon, an ID number for a coin, the name of a menu item, or the name of a movie reviewer.
main
method
and printing all of your results.
main
method
and printing your result.