Question
URGENT PLEASE HELP WILL THUMBS UP ******NOTE******** ONLY NEED TO WRITE COLLECTION CLASS The DataCollection.java is as follows: public interface DataCollection { public void insert(E
URGENT PLEASE HELP WILL THUMBS UP
******NOTE********
ONLY NEED TO WRITE COLLECTION CLASS
The DataCollection.java is as follows:
public interface DataCollection{ public void insert(E oneThing); public int size(); public String toString(); public E find(String key); public int countOccurrences(boolean lookFor); public boolean contains(E oneThing); public int total(); public double average(); public int countRange(int low, int high); }
Thing Class
Your Thing class must have the following characteristics:
- a meaningful name (not "Thing")
- at least 3 instance variables such that:
- the first instance variable must be of type String and this variable will be used as a search key
- the second instance variable must be integer (i.e., int) and this variable will be used to find aggregate information about Things that are stored in a collection class as will be explained later
- the third variable must be of type boolean
- All instance variables must be private
- Implement a three-arguments constructor for your Thing class. The arguments must be in the order (String, int, boolean).
- Implement getters and setters for all the attributes of your Thing. (Note that Eclipse will automagically create them for you with Source | Generate Getters and Setters)
- Implement a toString() method that returns a String representation of your Thing where all the instance variables are in one line and separated by tabs (Eclipse will automagically create a toString() method for you with Source | Generate toString(). However, the format will not be correct.)
- Implement the equals() method for your Thing where two Things are considered equal when they have the same search key. Note that, the equality of String attributes should be case insensitive. For example, MATH, math and Math match each other. In order to compare strings in Java use the String's equalsIgnoreCase() method. For example, the following code should print true:
String str1 = "Hello";
String str2 = "hello";
System.out.println(str1.equalsIgnoreCase(str2));
(Again, Eclipse will automagically create an equals() method for you with Source | Generate hashcode() and equals(). However, you may have to make changes to it to meet this program specification.)
- Implement the Comparable interface and the compareTo() method for your Thing. compareTo() returns
- a negative number when the invoking object's search key is less than the parameter's search key
- 0 when the two search keys are equal
- a positive number when the invoking object's search key is greater than the parameter's search key
- For example, "ant".compareTo("bat") will return a negative number.
- Your compareTo() method must compare two Things.
- Note that the comparison of String attributes should be case insensitive. For example, MATH, math and Math must match each other.
Collection Class
- Implement a collection class of your things using an array.
- You may NOT use the Java library classes ArrayList or Vector or any other java collection class.
- You must use simple java Arrays in the same way as discussed in class.
- The name of your collection class should include the name of your Thing. For example, if your Thing is called Student, then your collection class should be called StudentCollection.
- Implement a constructor for your collection class that takes one input parameter that represents the maximum number of elements that can be stored in your collection.
- Your collection class should implement the DataCollection interface which has been provided in D2L with the programming assignment. The interface file is named DataCollection.java. Do not change the interface file in any way, but simply add it to your Eclipse project.
- With the exception of an error message in insert(), no method in the collection class should print anything.
- Implement the following methods in your collection class, as defined in the DataCollection interface:
- void insert(Thing): a method that takes one input parameter matching the type of your Thing and then inserts it in the collection class IN DESCENDING ORDER according to the search key instance variable. Write a try/catch block to handle any exception that might occur (such as attempting to insert into a full list). You may print a message from your catch block. Unlike program #1, simply add any duplicate Things.
- int size(): a method that returns the number of objects in the collection.
- String toString(): a method that returns a String representation of the collection that includes all elements that are stored in the collection. The output string must be nicely formatted in a tabular format, one Thing per line. toString() will not print but rather will return a String. For example, a String representing a list of students (in reverse order by name) could be formatted as follows:
Name Age Registered
-----------------------------------
Reem 27 true
Mohammed 25 false
Maria 20 true
Hanna 30 true
- Thing find(String): this method depends on the search key attribute of your Thing class. The method takes as input a String value and returns the first object with a search key that matches the input search value. Use the compareTo() method from your Thing class. Note that you must convert the String parameter into a Thing object by calling the Thing constructor.
- int countOccurrences(boolean): this method has a boolean parameter. Depending on the value of the parameter, the method returns the number of Things whose boolean instance variable is true or the number of Things whose boolean instance variable is false.
- boolean contains(Thing): this method takes one input parameter matching the type of your Thing. The method returns true or false based on whether the collection contains at least one Thing that is equal to the input parameter. Use the equals() method from your Thing class to determine if two objects are equal.
- int total(): this method uses the int instance variable of your Thing class. The method calculates and returns the sum of the int instance variables of all objects stored in the collection. For example, in the student collection, this method finds the sum of age of all students in the list (102 in the example).
- double average(): this method returns the average of the int instance variables of all objects stored in the collection.
- int countRange(int low, int high): this method depends on the int instance variable of your Thing class. The method takes as input two int values and counts how many objects in the list have a value that lies in the given range including the endpoints. For example, in the student collection class that is displayed above, countRange(25,30) returns 3 because there are 3 students with age values that fall in the range between 25 and 30 inclusive. Note that if the first input parameter is greater than the second input parameter then the method always returns 0 (i.e., countRange(30,25) returns 0).
- Write an iterator class for your collection that iterates through your collection and returns the items in the list in the order in which they are stored. The results will be similar to toString() except that the iterator returns one Thing at a time. Your iterator will have constructor, hasNext() and next() methods. Implement your iterator as an embedded class as shown in class. Add an iterator() method to your collection class.
Driver Class
Implement a Driver class that includes a test program to:
- Create a collection class with capacity 10.
- Use the insert() method to insert 5-10 items in the collection. You may use any arbitrary values in the objects to be inserted.
- The driver tests all the methods of the collection class. Display the results of each operation after it is performed. Label your output clearly.
- Do not use a Scanner to read any inputs from the user. Instead, use hard coded values for the different methods' inputs.
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