Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Books example of ArrayList: CIS 232 - Assignment 2 In this assignment you will get practice working with the book's (p. 586) ArrayList implementation. You

image text in transcribed

Books example of ArrayList:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

CIS 232 - Assignment 2 In this assignment you will get practice working with the book's (p. 586) ArrayList implementation. You will amend the basic ArrayList class provided in the weiss.util package. All existing functionality must be preserved unless explicitly specified otherwise. Remember that ArrayList is generic. Implement the Java class depicted below, using your own code. You may not use the API's ArrayList class or any other API collection save arrays. Be sure to follow the specs exactly. Please keep with the spirit of the assignment or risk a significant penalty You will revise the Weiss ArrayList class with the following modifications (Note that from this point onward substitute "Whatever" with your class name - details below) 1) The list must be sorted in ascending order at all times, with no null references in the middle of the data. The list must have this order maintained at the end of every public method call, so some existing methods will need to be modified 2) Modify the add ( (and any other methods as you see fit) method of the class so that each new item is inserted in sorted (ascending) order. 3) Overload add ) with signature specified below. This method inserts the item at the specified index, adjusting incumbent items as needed. Note that if the index parameter is not the proper one for that item based on ordering the method should insert the item at the proper index that places it in order, not the requested one public void add (int index, AnyType element) 4) Modify the set () method so that it replaces and returns the item at the specified index. Observe and adjust for any issues due to the requested index and order as specified for the overloaded add () method in 3) 5) Adjust the constructor that takes a Collection parameter so that it creates the Whatever with the same size as the Collection passed to it. The constructor still initializes the Whatever with the contents of the Collection parameter. 6) Add another constructor that takes in a lone integer parameter. The Whatever is initially created to this size rather than the default size 7) Add an instance method named getMode () that returns the most-occurring value in the list. The method's return type is Result, a generic interface that has two methods: AnyType mode (), which returns the most-occurring item in the list, and int count (), which returns how many times that item occurs. Tiebreak on first- occurring item of the highest frequency. The code for the Result interface will be provided to you to incorporate into your package. Do not send this interface file with your code. Note that the object you return needs to be a very simple container with the values necessary to support the interface methods. It does not include any computation logic- all work should be done by your list class 8) Add a generic static method that takes in a (your) Whatever and performs a binary search on it. The method takes exactly two parameters: a Whatever, and the item to search for. The type of the second parameter needs to reflect what could possibly be in the Whatever instance. This method returns an int that is the index in the list where the item resides, or-1 if not found. The method header will look similar (same name but you need to revise it to make it use generics) to public static int binSearch (Whatever list, AnyType item) 1 package weiss.util; 2 3 public class ArrayList extends AbstractCollection 4 5 6 7 8 figure 15.13 ArrayList implementation (part 1) implements List private static final int DEFAULT_CAPACITY 10; private static final int NOT FOUND 1; 9 private AnyType [ theItems; private int theSize; private int modCount 0; 10 12 13 public ArrayList() clear); public ArrayList( Collection extends AnyType other ) 16 17 18 19 20 21 clear(); for( AnyType obj: other ) add( obj); 23 public int size( ) 24 25 26 public void clear 27 28 29 30 31 32 33 public AnyType get( int idx 1 34 35 36 37 38 39 40 public AnyType set int idx, AnyType newVal) return theSize; } theSize 0; theItems(AnyType []) new Objectl DEFAULT_CAPACITY; modCount++; if( idx iterator 103 104 105 public ListIterator listIterator( int idx ) 106 107 108 // This is the implementation of the ArrayListIterator 109 110 { return new ArrayListIterator( 0 ); J { return new ArrayListIterator( idx ); J private class ArrayListIterator implements ListIterator 0; public AnyType next( ) 136 137 138 139 140 141 142 143 144 145 if( !hasNext( ) throw new NoSuchElementException(); nextCompleted -true; prevcomp I eted = fa l se ; return theItems[ current++1: public AnyType previous) 146 147 if( !hasPrevious() ) throw new NoSuchElementException(); 148 149 150 151 152 153 154 155 156 157 158 159 160 161 prevCompleted -true; nextcomp leted false; return theItems --current1: public void remove() if( expectedModCount != modCount ) throw new ConcurrentModificationException( ); if( nextCompleted ) else if( prevCompleted ) ArrayList.this.remove( --current); ArrayList.this.remove( current ); throw new IllegalStateException(; 162 163 164 165 166 167 168 169 170 else prevcomp leted nextcomp leted - false; expectedModCount++; CIS 232 - Assignment 2 In this assignment you will get practice working with the book's (p. 586) ArrayList implementation. You will amend the basic ArrayList class provided in the weiss.util package. All existing functionality must be preserved unless explicitly specified otherwise. Remember that ArrayList is generic. Implement the Java class depicted below, using your own code. You may not use the API's ArrayList class or any other API collection save arrays. Be sure to follow the specs exactly. Please keep with the spirit of the assignment or risk a significant penalty You will revise the Weiss ArrayList class with the following modifications (Note that from this point onward substitute "Whatever" with your class name - details below) 1) The list must be sorted in ascending order at all times, with no null references in the middle of the data. The list must have this order maintained at the end of every public method call, so some existing methods will need to be modified 2) Modify the add ( (and any other methods as you see fit) method of the class so that each new item is inserted in sorted (ascending) order. 3) Overload add ) with signature specified below. This method inserts the item at the specified index, adjusting incumbent items as needed. Note that if the index parameter is not the proper one for that item based on ordering the method should insert the item at the proper index that places it in order, not the requested one public void add (int index, AnyType element) 4) Modify the set () method so that it replaces and returns the item at the specified index. Observe and adjust for any issues due to the requested index and order as specified for the overloaded add () method in 3) 5) Adjust the constructor that takes a Collection parameter so that it creates the Whatever with the same size as the Collection passed to it. The constructor still initializes the Whatever with the contents of the Collection parameter. 6) Add another constructor that takes in a lone integer parameter. The Whatever is initially created to this size rather than the default size 7) Add an instance method named getMode () that returns the most-occurring value in the list. The method's return type is Result, a generic interface that has two methods: AnyType mode (), which returns the most-occurring item in the list, and int count (), which returns how many times that item occurs. Tiebreak on first- occurring item of the highest frequency. The code for the Result interface will be provided to you to incorporate into your package. Do not send this interface file with your code. Note that the object you return needs to be a very simple container with the values necessary to support the interface methods. It does not include any computation logic- all work should be done by your list class 8) Add a generic static method that takes in a (your) Whatever and performs a binary search on it. The method takes exactly two parameters: a Whatever, and the item to search for. The type of the second parameter needs to reflect what could possibly be in the Whatever instance. This method returns an int that is the index in the list where the item resides, or-1 if not found. The method header will look similar (same name but you need to revise it to make it use generics) to public static int binSearch (Whatever list, AnyType item) 1 package weiss.util; 2 3 public class ArrayList extends AbstractCollection 4 5 6 7 8 figure 15.13 ArrayList implementation (part 1) implements List private static final int DEFAULT_CAPACITY 10; private static final int NOT FOUND 1; 9 private AnyType [ theItems; private int theSize; private int modCount 0; 10 12 13 public ArrayList() clear); public ArrayList( Collection extends AnyType other ) 16 17 18 19 20 21 clear(); for( AnyType obj: other ) add( obj); 23 public int size( ) 24 25 26 public void clear 27 28 29 30 31 32 33 public AnyType get( int idx 1 34 35 36 37 38 39 40 public AnyType set int idx, AnyType newVal) return theSize; } theSize 0; theItems(AnyType []) new Objectl DEFAULT_CAPACITY; modCount++; if( idx iterator 103 104 105 public ListIterator listIterator( int idx ) 106 107 108 // This is the implementation of the ArrayListIterator 109 110 { return new ArrayListIterator( 0 ); J { return new ArrayListIterator( idx ); J private class ArrayListIterator implements ListIterator 0; public AnyType next( ) 136 137 138 139 140 141 142 143 144 145 if( !hasNext( ) throw new NoSuchElementException(); nextCompleted -true; prevcomp I eted = fa l se ; return theItems[ current++1: public AnyType previous) 146 147 if( !hasPrevious() ) throw new NoSuchElementException(); 148 149 150 151 152 153 154 155 156 157 158 159 160 161 prevCompleted -true; nextcomp leted false; return theItems --current1: public void remove() if( expectedModCount != modCount ) throw new ConcurrentModificationException( ); if( nextCompleted ) else if( prevCompleted ) ArrayList.this.remove( --current); ArrayList.this.remove( current ); throw new IllegalStateException(; 162 163 164 165 166 167 168 169 170 else prevcomp leted nextcomp leted - false; expectedModCount++

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

List and describe examples of discrimination laws.

Answered: 1 week ago

Question

2. Describe how technology can impact intercultural interaction.

Answered: 1 week ago

Question

7. Define cultural space.

Answered: 1 week ago