Question
ArrayList and Classes Imagine you want to create a program to keep track of a collection you have. If you don't actually have a collection,
ArrayList and Classes
Imagine you want to create a program to keep track of a collection you have. If you don't
actually have a collection, you can have your program work as a wish list instead. You will
create 3 class files for this homework:
• The first class will represent the type of object you collect.
• The second class represents the entire collection.
• The third class is your "driver" class. This is what some people call the class that
contains the main method. This class will make use of your other two classes.
You will need to complete the following tasks - each is worth 1 point:
In your first class:
• Task 1: Give your class a name that describes the item.
• Task 2: Make sure your class has at least 3 attributes, one of which can be used to order
items from "lowest" to "highest" in some way.
• Task 3: Add one or more constructors to your class.
• Task 4: Add Javadoc comments to all methods in this class and in the other two.
• Task 5: Create a toString method for your class that overrides the default Object toString
method.
• Task 6: Have your class implement the Comparable interface and create a compareTo
method.
In your second class:
• Task 7: Create an attribute for your class that is an ArrayList to hold the items in your
collection. Create a no-argument constructor for your class.
• Task 8: Create a method to find an item in your collection.
• Task 9: Create a method to add a new item to your collection.
• Task 10: Create a method to remove an item from your collection, using on of its
attributes to identify the item(s) to be removed.
• Task 11: Create a toString method for your collection, overriding the default Object
toString method.
• Task 12: Create a sort method to sort items in your collection.
In your third class:
• Task 13: Create an instance of your collection class. Create at least 5 instances of your
collectible item class and add these items to your collection.
• Task 14: Use your collection's toString method to print out the contents of your
collection. Call the sort method. Then, print out the contents of the collection again.
• Task 15: Use your collectible item's toString method to print out the String
representations of all your highest valued items.
You are provided with an example consisting of 3 classes that satisfy the requirements of this
assignment. You can use this code as a guide, but try to make your items and collections at
least a little different.
For example, perhaps you dream of having a luxury car collection. Instead of a "quality score",
you might use top speed as the attribute to order items with. If you are really into a specific
sport, your "collection" might be a list of players you admire in that sport, and you would need
to think of some meaningful metric with which to order them. If you want to keep a list of
places you'd like to visit, perhaps you might order the locations based on how far away they are
from where you live.
BONUS QUESTIONS ABOUT THE SAMPLE CODE (worth 1 point each):
1. Because we haven't learned better methods yet, the collection list has some issues. In the
sample code, what if we wanted multiple chocolate bars of the same type in our
collection? Think of at least one way to handle this. List at least one downside to your
approach.
2. This issue was mentioned in passing, but, in an effort to be more efficient and avoid
redundancy, the collection is not supposed to allow duplicate items. Explain how
duplicate items could inadvertently end up in the collection currently. Propose one way
of avoiding this possibility.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started