Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your job is to write your own array list (growable array) that stores elements that are objects of a parent class or any of its

Your job is to write your own array list (growable array) that stores elements that are objects of a parent class or any of its child classes but does not allow any other type of objects to be stored. You will create a general data class and two specific data classes that inherit from that class. You will then write the array list to hold any objects of the general data class. Finally, you will test your array list using JUnit tests to prove that it does what it should.

Your array list must be named DataList. Start with an array with room for 10 elements. Keep all elements contiguous (no blank places) at the low end of the indices. When the array is full, grow it by doubling the size. (Do not use any built-in methods to copy the array elements. Write your own code to do this.) You do not have to shrink the array when elements are removed. The array list must have the following public methods (listed in UML style where GenClass is your general data class):

insert(index: int, obj: GenClass) : boolean o Inserts the given object at the given index, moving all elements up one from that index forward to make room, growing the array if necessary. If the index is out of range, the method returns false. If the object is successfully inserted, it returns true. add(obj: GenClass): int o Adds the given object to the end of the list (the next available place in the array), gorwing the array if necessary, and returns the index where that object was added. set(index: int, obj: GenClass) : GenClass o Replaces the object in the list at the given index with this object. If the index is out of range, the method returns null. If the operation is successful, the method returns the object that was replaced by this one. delete(index: int) : GenClass o Deletes the object at the given index from the list, moving all elements above this index down to fill in the hole in the list. If the index is out of range, the method returns null. If the operation is successful, the method returns the object that was deleted. removeFirst(obj: GenClass) : boolean o Removes the first instance of this object from the list, moving all elements above this index down to fill in the hole in the list. If the object does not exist in the list, the method returns false. If the operation is successful, the method returns true. removeAll(obj: GenClass) : int o Removes all instances of this object from the list, moving elements above each down to fill in the hole in the list. Returns the number of objects removed. report(index: int) : GenClass o Reports the object in the list at the given index. If the index is out of range, returns null. findFirst(obj: GenClass) : int

o Finds the first instance of the given object in the list and returns the index where it was found. Returns -1 if it is not found. findLast(obj: GenClass) : int o Finds the last instance of the given object in the list and returns the index where it was found. (Search from the end for efficiency.) Returns -1 if it is not found. size() : int o Reports the number of elements currently stored in the list. capacity() : int o Reports the current size of the underlying array. toString() : String o Reports a String representation of the list of objects currently stored in it, in the format [first obj String, second obj String, ..., last obj String] where each object is displayed using their toString method.

You are welcome to add private methods to the DataList class if you think they would be helpful, but this is not required. You are not allowed to use a Java ArrayList in this project. The whole idea is to create your own. The JUnit tests need to test all possible outcomes of all the methods listed above for the general class. For example, the set method should be tested for when the list is empty, when the index is larger than the current size, when the index is less than zero, and when the index is good. The tests should confirm that the new value replaced the old in the list and that the old value was returned. The general data class should be named GenClass and represent some real type of object or concept and have at least two private fields, a default and a full constructor, getters, setters, an equals and hashCode method, and a toString() that displays the values of all fields. One of the fields should be some unque identifier that can be used for the equals method. The child classes should each add some unique field or fields that uniquely identify this type of object and distinguish it from any other type. They will need a new default constructor, a modified full constructor, and toString(), and getters and setters for the new fields.

IF SOMEONE CAN SOLVE THIS WITH THIS TEMPLATE WILL BE GREAT!

/** * This is a collection class that acts like as array list for objects * of the Integer type. * * @author YOUR_NAME_HERE * @version VERSION#_&_LAST_EDIT_DATE */ public class DataListTemplate { // private fields private Integer[] array; private int count = 0; public DataListTemplate(int capacity) { this.array = new Integer[capacity]; } /** * Inserts the given object at the given index. * * @param index the index where the object should be inserted * @param obj the object to insert * @return true if the insertion was successful, false if the index * is out of range. */ boolean insert(int index, Integer obj) { //TODO - write this code resize(); //check if the element at the index having a value (not null) //if it does not => insert //else move all the element starting from the index to the right one position // System.arraycopy(); return false; } /** * Adds the given object to the end of the list (the next available place * in the array). * * @param obj the object to add * @return the index where the object was stored */ int add(Integer obj) { //TODO - write this code resize(); return -1; } /** * Replaces the object in the list at the given index with this object. * * @param index the location to place this object * @param obj the object to set at the given index * @return the object that was at this index before replacement */ Integer set(int index, Integer obj) { //TODO - write this code return null; } /** * Deletes the object at the given index from the list. * * @param index the index of the object to delete * @return the object deleted, or null if the index is out of range */ Integer delete(int index) { //TODO - write this code resize(); return null; } /** * Removes the first instance of this object from the list. * * @param obj the object to match for removal * @return true if removed, false if the object is not in the list */ boolean removeFirst(Integer obj) { //TODO - write this code resize(); return false; } /** * Removes all instances of this object from the list. * * @param obj the object to match for removal * @return the number of objects removed */ int removeAll(Integer obj) { //TODO - write this code resize(); return -1; } /** * Reports the object in the list at the given index. * * @param index the index of the object to return * @return the object at the given index, or null if the index is out of * range. */ Integer report(int index) { //TODO - write this code return null; } /** * Finds the first instance of the given object. * * @param obj the object to find * @return the index where the object was found, or -1 if not found. */ int findFirst(Integer obj) { //TODO - write this code return -1; } /** * Finds the last instance of the given object. * * @param obj the object to fine * @return the index where the object was found, or -1 if not found. */ int findLast(Integer obj) { //TODO - Write this code return -1; } /** * Reports the number of elements currently stored in the list. * * @return the number of elements */ int size() { return count; } /** * Reports the current size of the underlying array. * * @return the size of the array */ int capacity() { return array.length; } /** * Returns a String representation of the list of objects currently stored * in the list. * * @return the string representation of the list */ @Override public String toString() { String str = "["; for (Integer obj : array) { str += obj.toString() + ","; } if (array.length > 0) { str = str.substring(0, str.length() - 1); } str += "]"; return str; } /** * Doubles the size of the array and copies the current elements into the * new, larger array. * Use System.arraycopy(); */ private void resize() { if (count >= 3./4. * array.length) { //double the size of the internal array } else if (count <= 1/4 * array.length) { //shrink the size of the internal array by half } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions