Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Implement your Thing class Start by choosing what sort of list you would like to keep. For example: Vacation and travel information

imageimageimageimageimageimageimage

Part 1: Implement your Thing class Start by choosing what sort of list you would like to keep. For example: Vacation and travel information Real estate properties pretty much any sort of list You can be creative about your list. It is prohibited to use grocery items, circles, lines, persons, cars, students, book, bank accounts, points in two-dimensional space, Movies, Pet, Animal, or songs. Please note that you will reuse your Thing class across more than one programming assignment, so try to choose something that interests you. Your Thing class must have the following characteristics: has a meaningful name (not Thing). has exactly 2 instance variables such that: one instance variable must be String and this variable will be used as a search key. one instance variable must be int and this variable will be used to find aggregate information about Things that are stored in the collection class as will be explained later. All instance variables must be declared as private. Implement a constructor for your Thing class that takes two input parameters to initialize the two instance variables. The order of the input parameters must be as follows String, then int. Implement getters (but no setters) for all the instance variables of your Thing. Implement toString method that returns a String representation of your Thing class where all the instance variables are in one line and separated by tabs. Implement the equals method for your Thing where two things are considered equal when they have the same values in both of the instance variables. 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 equals IgnoreCase method. For example, the following code prints true: String str1 = "Hello"; String str2 = "hello"; System.out.println(str1.equalslgnoreCase(str2)); Implement the comapre To method that takes a Thing object as input parameter and returns either 0,-1, or 1 based on whether the input Thing is equal to, larger than, or smaller than the current thing respectively. Note that Things in your collection are to be sorted on the String search key first, then on the int instance variable. For example, if your Thing is a Circle that has color and radius as the String and int instance variables respectively, then: (red, 10) < (Red, 20) because: red is equal to Red and 10 < 20. (Blue, 10) < (red, 30) because Blue < red in alphabetical order ignoring the case. The Thing class must also implement the Comparable interface. Assume, for example, your Thing is called Circle, then the class header will be as follows: public class Circle implements Comparable Part 2: Implement a Sorted Set class 1. Implement a collection class of Things that stores the elements in a sorted order using a simple Java array( i.e., Thing []). Note that: 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 we implemented IntArrayBag collection in class. 2. The collection class is a set which mean duplicates are not allowed. 3. 4. The name of your collection class should include the name of your Thing. For example, if your Thing is called Circle, then your collection class should be called Circle SortedArraySet. The collection class has two instance Search documents and file names for text which is an integer that represents the number of things in class (note that you should change Things to be the name of your thing, for example, numCircles) and (2) an array of type Thing [] (e.g., Circle []). Implement a constructor for your collection class that takes one integer input parameter that represents the maximum number of elements that can be stored in your collection. 6. Implement the following methods in your collection class: 5. a. add: a method that takes two input parameters and uses these inputs to instantiate an object of type Thing, initialize its instance variables, and then inserts it in the collection class. Note that (1) the input parameters to the add method must be in the order String, int, (2) the add method must insert elements in a sorted order according to the compare To method of the Thing class, and (3) duplicates are not allowed which means that the item is not inserted if the set already has another item that is equal to the inserted item where equality is determined based on equals method of Thing class. Note that, it is not the responsibility of the collection class to decide on how to sort things or how to test the equality. On the other hand, sorting and equality depend on the semantics of your Thing class. Moreover, according to compareTo, the items will be sorted either in ascending or descending order. Do not use the java Arrays's sort method. b. size: a method that returns the number of things in the collection. c. 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. For example, a list of Circles is represented by the following String: color blue Green red Red radius 10 20 20 30 d. indexOf: this method takes one input parameter of type Thing. The method returns an integer that represents the array index at which the input element is found or -1 if the element is not found. Note that the equality of things is determined by the equals method in your Thing class. e. remove: similar to the indexOf method, this method takes one input parameter of type Thing. The method then searches the collection for an object that equals the input object and removed that object if found. The method then returns true if the object is deleted and false otherwise. Note that, this method must maintain the sorted order which means that just replacing the deleted item with the last element in the array will not work anymore because this may break the sorted order. On the other hand, you should shift the array elements up to fill the gap that is created by the deleted item. Do not use the Java's Array sort method. f. grab: a method that takes an integer, called index, as input and returns as output the Thing that is stored at position index in the collection or null if index is beyond the size of the collection. Note that, the method does not delete the element from the array. g. replace: this method takes two inputs, oldThing and newThing, each of them is of type Thing. The method then searches the collection for an element that is equal to oldThing. If found, the method replaces oldThing with newThing while maintaining the sorted order. On the other hand, if oldThing is not found in the collection, the method does not do anything. The method then returns true or false based on whether a replacement took place or no. h. equals: this is the standard equals method for Java objects that takes one input parameters of type Object and returns true or false based on whether the input object is equal to the current set. Two sets are equalwhen they have the exact set of Things. i. total: this is a static method uses the int instance variable of the Thing class. The method takes one input of type ThingSortedArray Set and calculates and returns the sum of the integer instance variable of all objects stored in the collection. For example, in the Circle collection, this method finds the sum of radius of all circles in the list. This sum can be used, for example, along with the output of the size method to find the average radius. class ThingArraySortedSetIterator This is an inner class inside the ThingSortedArraySet class and it is used to iterate over all elements in the collection. An implementation of the list iterator is discussed in class and can be found on lecture-03-jan-28.ppx file on week-03-Jan28 folder on D2L. Basically, the header of your iterator class should be as follows: public class ThingSorted ArraySetlterator implements Iterator You have to change of the rest of iterator class as necessary to reflect your Thing data type. You also must change your ThingSortedArraySet class to implement Iterable and add the iterator method that returns an iterator of type ThingArrayBagIterator. To test this part, write a for-each loop in the driver to iterate over all items in your collection. What to submit? Upload on D2L only one zip file that includes the following four files: Thing.java: a file that includes the implementation of your Thing. ThingSortedArraySet.java: a file that includes the implementation of the sorted bag as explained above. ThingSortedSetDriver.java: a driver file to demonstrate how you tested your collection to handle a collection of your thing. Assignment2.docx: a word document that includes: UML diagram of your application

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Code Petsjava public class Pets private int Age private String Name private String Type public Petsint age String name String type Age age Name name Type type public int getAge return Age public Strin... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elements Of Chemical Reaction Engineering

Authors: H. Fogler

6th Edition

013548622X, 978-0135486221

More Books

Students also viewed these Programming questions

Question

In Exercises 1558, find each product. (9 - 5x) 2

Answered: 1 week ago

Question

Express the following ratios in its lowest terms.

Answered: 1 week ago