Part 1: Implement your Thing class Start by choosing what sort of list you would like...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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<Circle> 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<Thing> 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<Thing> 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 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<Circle> 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<Thing> 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<Thing> 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
Expert Answer:
Answer rating: 100% (QA)
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... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
QUESTION THREE The following is an extract of a trial balance of Ruslan-Ford Partners as at 28 February 2023 Capital: Ruslan Capital: Ford Current account: Ruslan 01/03/2022 Current account: Ford...
-
Lundquist Company received a 60-day, 9% note for $28,000, dated July 23, from a customer on account. a. Determine the due date of the note. b. Determine the maturity value of the note. c. Journalize...
-
Explain why the concept of relevant range is important when dealing with step costs.
-
Your boss hands you a bag containing four springs, A, B, C, and D, and tells you that they all have the same relaxed length. He wants you to rank them by the values of their spring constants. He...
-
Neal Manufacturing Company makes tents that it sells directly to camping enthusiasts through a mail-order marketing program. The company pays a quality control expert $60,000 per year to inspect...
-
Consider the following information about a roofing business Taylor opened last year: Taylor was a manager at Stater Bros, with an annual salary (including benefits) of $50,000 per year. Taylor has...
-
Read the book The Pelican Brief was written by John Grisham in 1992. It was published the same year by Doubleday, New York. State the theme and the thesis of the book.
-
Machine A makes 50 records in 2m machine B 50 records in 3min with both machines working how long would it take 1000 records?
-
The answers should be in essay paragraph format using APA, Times New Roman. Why is health equity important in society and why would it be financially feasible in the health economy? What are the pros...
-
A small commercial property is for sale near your university. Given its location, you believe a student-oriented business would be very successful there. You have researched several possibili- ties...
-
In Drosophila, white eyes are an X-linked character. The mutant allele for white eyes (w) is recessive to the wild-type allele for brick red eye color (w). A white-eyed female is crossed with a...
-
Evaluate the effectiveness of risk management and the risk control system
-
Joanette, Inc., is considering the purchase of a machine that would cost $530,000 and would last for 8 years, at the end of which, the machine would have a salvage value of $43,000. The machine would...
-
In Exercises 1558, find each product. (9 - 5x) 2
-
The hydrogenation of ethylbenzene to ethylcyclohexane over a nickelmordenite catalyst is zero order in both reactants up to an ethylbenzene conversion of 75% (Ind. Eng. Chem. Res., 28 (3), 260...
-
Solar energy capture has great potential to help meet the worlds growing energy demand, which is 12 terawatts in 2010 and is expected to rise to 36 terawatts in 2050. Professor Al Weiner and his...
-
While on his way home from a concert by Ann Arbors own Bob Seger at Pine Knob, during his visit to Michigan, world famous Inspector Sgt. Nigel Ambercromby was called in from Scotland Yard when the...
-
Consider the data file \(m r o z\) on working wives. Use the 428 observations on married women who participate in the labor force. In this exercise, we examine the effectiveness of alternative...
-
To examine the quantity theory of money, Brumm (2005) ["Money Growth, Output Growth, and Inflation: A Reexamination of the Modern Quantity Theory's Linchpin Prediction," Southern Economic Journal,...
-
Consider the data file mroz on working wives and the model \(\ln (W A G E)=\beta_{1}+\beta_{2} E D U C+\) \(\beta_{3} E X P E R+e\). Use the 428 observations on married women who participate in the...
Study smarter with the SolutionInn App