Exercise 1: Review of Array-Based Lists Back to Top Create a project using the classes in the Doc…

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Order a Similar Paper Order a Different Paper

Exercise 1: Review of Array-Based Lists

Back to Top

Create a project using the classes in the Doc Sharing area labeled A Simple ArrayList class.

Compile it, run it, and review the code that is given carefully. This code tests the ArrayList class provided in the lecture.

Exercise 2: Implementing an Array List

Back to Top

Modify the class ArrayList given in the lecture by adding the methods to it listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs. For example, an error message should be generated when trying to insert an item in a given location in the list and the location is out of range. Create a main class to test your ArrayList class.

a. ArrayList(int size): Create a constructor that sets the size of the array list to the value passed in size (note that the class variable size cannot be final anymore).
b. int getLength(): Create this method to return the number of items in the list (accessor method).
c. int getSize(): Create this method to return the size of the list (accessor method).
d. void clear(): Create this method to remove all of the items from the list. After this operation, the length of the list is zero.
e. void replace(int location, int item): Create this method to replace the item in the list at the position specified by location. The item should be replaced with item.
f. void add(int location, int item): Create this method to add an item to the list at the position specified by location.
g. void remove(int item): Create this method to remove an item from the list. All occurrences of the item in the list should be removed.
h. int get(int location): Create a method that returns the element at location.

In the Doc Sharing area labeled Implementing an ArrayList class, you will find the basic framework of the ArrayList class you need to use to implement your solution.

Exercise 3: Using an Array-Based List

Back to Top

Using the class ArrayList completed in the previous exercise, write a program to store the first 30 Fibonacci numbers in an ArrayList object.

Exercise 4: Implementing a Bag Class

Back to Top

Create a class Bag (multiset) that uses an array to store the bag items. The class should have the methods listed below. Create a main class to test your Bag class. This main class should fill a bag of integers with 10 random numbers, each in the interval [0, 15], and print how many times each integer in the interval [0, 15] appears in the bag.

a. Bag(): default constructor that sets the size of the bag to 20
b. Bag(int size): nondefault constructor that sets the size of the bag to the value passed in size
c. boolean isEmpty(): determines whether the bag is empty
d. void display(): prints the bag elements
e. int getLength(): returns the number of items in the bag
f. int getSize(): returns the size of the bag
g. void clear(): removes all of the items from the bag
h. void add(int item): adds an item to the bag
i. void remove(int item): removes an item from the bag; all occurrences of item in the bag should be removed.
j. int count(int item): counts the number of occurrences of item in the bag

(Note that you can reuse the code completed in Exercise 2 for the ArrayList class to create your Bag class. It will help you to save development time.)