Question
Everything needs to be according to the instructions, thank you! SUPPORTING CODE: MyList.java /** This interface specifies the basic operations of any list-like object. This
Everything needs to be according to the instructions, thank you!
SUPPORTING CODE:
MyList.java
/** This interface specifies the basic operations of any list-like object. This interface contains a variation of the methods of the standard java.util.List interface. */ public interface MyList { /** Adds an element at the end of the list. */ public void addToEnd(Object o);
/** Inserts an element at the specified index Throws NoSuchElementException if index is out of bounds. */ public void insertAt(int index, Object o);
/** Removes the element at the specified index Throws NoSuchElementException if index is out of bounds. */ public void removeAt(int index); /** Returns the element at the specified index Throws NoSuchElementException if index is out of bounds. */ public Object getAt(int index); /** Returns the size of the list. @return the number of elements in the list */ public int getSize(); /** Returns a list iterator for this list. @return a list iterator for this list */ public MyListIterator getIterator(); }
MyListIterator.java
/** A list iterator allows access of a position in a list. This interface contains a subset of the methods of the standard java.util.ListIterator interface. The methods for backward traversal are not included. */ public interface MyListIterator { /** Moves the iterator past the next element. @return the traversed element */ Object next(); /** Tests if there is an element after the iterator position. @return true if there is an element after the iterator position */ boolean hasNext(); }
Main.java
// you may use this file to write and run code to test your MyArrayList class
public class Main { public static void main(String[] args) { } }
FILE THAT NEEDS THAT NEEDS CODE:
MyArrayList.java
// Complete the implementation of your MyArrayList class in this file
public class MyArrayList implements MyList { // Implement the required fields and methods here private int capacity = 8;
private Object[ ] array = new Object [capacity];
private int size = 0;
@Override
public void add(Object o) {
if (size >= capacity){
Object[] temp = new Object[2*array.length];
for(int i=0;i temp[i] = array[i]; } this.capacity = 2*array.length; array = temp; array[size++] = o; return; } else { array[size++] = o; } } @Override public int size() { return size; } @Override public Object at(int index) { if (index >= capacity) return null; else return array[index]; } @Override public void insertAt(int index, Object o) { if (index >= capacity) return; else { size++; for (int x = size - 1; x > index; x--) { array[x] = array[x - 1]; } array[index] = o; } } @Override public void removeAt(int index) { if (index >= size || size == 0) return; else { Object e = array[index]; for (int x = index; x array[x] = array[x + 1]; } size--; } } public void ensureCapacity(int minCapacity) { } public void trimToSize() { ensureCapacity(size); } // Do not alter the code below @Override public MyListIterator getIterator() { return new MyArrayListIterator(); } private class MyArrayListIterator implements MyListIterator { int currentIndex = -1; @Override public Object next() { ++currentIndex; return storage[currentIndex]; } @Override public boolean hasNext() { return currentIndex You are not allowed to use any of the standard Java collection types (like ArrayList) for this assignment. You may use simple arrays. Problem Description and Given Info For this assignment you are given the following Java source code files: - MyListiterator. java (This file is complete - make no changes to this file) - MyList. java (This file is complete - make no changes to this file) - MyArraylist.java (You must complete this file) - Main.java (You may use this file to write code to test your MyArrayList) You must complete the public class named MyArrayList with fields and methods as defined below. Your MyArrayList will implement the MyList interface that is provided in the myList. java file. Structure of the Fields As described by the UML Class Diagram above, your MyArrayList class must have the following fields: - a private field named capacity of type int, initialized to 8 - a private field named size of type int, initialized to - a private field named storage of type object [ ], initialized to an object array of 8 elements Structure of the Methods As described by the UML Class Diagram above, your MyArrayList class must have the following methods: - a public method named addToEnd that takes an object argument and returns nothing - a public method named insertAt that takes an int argument and an object argument and returns nothing - a public method named removeAt that takes an int arguments and returns nothing - a public method named getAt that takes an int argument and returns an object - a public method named getsize that takes no arguments and returns an int - a public method named makeCapacity that takes an int argument and returns noting - a public method named trimExcess that takes no arguments and returns nothing Note that five of these methods are declared in the MyList interface. You will be implementing these methods in this MyArrayList concrete derived class. Also note that the getIterator method and the MyArrayListiterator class are already implemented for you in the MyArrayList class. Make no changes to this code. MyArraylist 1. This concrete class will store its elements in an array of Object. The initial capacity of this array will be 8 elements. Since an array is a fixed size structure, you may need to allocate a new array with increased capacity in order to accommodate adding new elements. For this purpose you must implement the makeCapacity method. 2. makeCapacity method - This method will take a minCapacity as an int argument. - If minCapacity is less than current size or equal to the capacity, then this method should take no action. - Otherwise the capacity of this MyArraylist must be changed to either 8 or minCapacity (whichever is greater). - If capacity is to be changed, then this method will allocate a new array of object sized to the new capacity - Then copy over all elements from the old array to the new array - Then store the new array in the private storage variable for this instance 3. trimExcess method - This method will remove any excess capacity by simply calling the makeCapacity method with an argument value that is equal to the current size of this list. 4. addToEnd method - Appends new item to end of list. For example: given the list {1,2,3} and an instruction to addToEnd(99), the result would be this {1,2,3,99}. - If the current size is equal to the current capacity, then this list is full to its current capacity, and capacity will need to be increased before we can append the new element. To increase the capacity, call the makeCapacity method with an argument. value that is twice the current capacity. - This method will add the new element to the list at the next available index in the array storage. 5. insertAt method - Makes a place at the specified index by moving all items at this index and beyond to the next larger index. For example: given the list {1,2,3} and an instruction to insertAt (1,99), the result would be this {1,99,2,3}. - Throws a NoSuchElementException if the specified index is less than or greater than size. - If the current size is equal to the current capacity, then this list is full to its current capacity, and capacity will need to be increased before we can insert the new element. To increase the capacity, call the makeCapacity method with an argument. value that is twice the current capacity. 6. removeAt method - Removes the element at the specified index and moves all elements beyond that index to the next lower index. For example: given the list {1,2,3} and an instruction to removeAt (1), the result would be this {1,3}. - Throws a NoSuchElementException if the specified index is less than 0 or greater than or equal to size. 7. getAt method - Returns the item at the specified index. For example: given the list {1,2,3} and an instruction to getAt (1), the return value would be 2 . - Throws a NoSuchElementException if the specified index is less than or greater than or equal to size. 8. getsize method - Returns the number of elements currently stored in the list. You are not allowed to use any of the standard Java collection types (like ArrayList) for this assignment. You may use simple arrays. Problem Description and Given Info For this assignment you are given the following Java source code files: - MyListiterator. java (This file is complete - make no changes to this file) - MyList. java (This file is complete - make no changes to this file) - MyArraylist.java (You must complete this file) - Main.java (You may use this file to write code to test your MyArrayList) You must complete the public class named MyArrayList with fields and methods as defined below. Your MyArrayList will implement the MyList interface that is provided in the myList. java file. Structure of the Fields As described by the UML Class Diagram above, your MyArrayList class must have the following fields: - a private field named capacity of type int, initialized to 8 - a private field named size of type int, initialized to - a private field named storage of type object [ ], initialized to an object array of 8 elements Structure of the Methods As described by the UML Class Diagram above, your MyArrayList class must have the following methods: - a public method named addToEnd that takes an object argument and returns nothing - a public method named insertAt that takes an int argument and an object argument and returns nothing - a public method named removeAt that takes an int arguments and returns nothing - a public method named getAt that takes an int argument and returns an object - a public method named getsize that takes no arguments and returns an int - a public method named makeCapacity that takes an int argument and returns noting - a public method named trimExcess that takes no arguments and returns nothing Note that five of these methods are declared in the MyList interface. You will be implementing these methods in this MyArrayList concrete derived class. Also note that the getIterator method and the MyArrayListiterator class are already implemented for you in the MyArrayList class. Make no changes to this code. MyArraylist 1. This concrete class will store its elements in an array of Object. The initial capacity of this array will be 8 elements. Since an array is a fixed size structure, you may need to allocate a new array with increased capacity in order to accommodate adding new elements. For this purpose you must implement the makeCapacity method. 2. makeCapacity method - This method will take a minCapacity as an int argument. - If minCapacity is less than current size or equal to the capacity, then this method should take no action. - Otherwise the capacity of this MyArraylist must be changed to either 8 or minCapacity (whichever is greater). - If capacity is to be changed, then this method will allocate a new array of object sized to the new capacity - Then copy over all elements from the old array to the new array - Then store the new array in the private storage variable for this instance 3. trimExcess method - This method will remove any excess capacity by simply calling the makeCapacity method with an argument value that is equal to the current size of this list. 4. addToEnd method - Appends new item to end of list. For example: given the list {1,2,3} and an instruction to addToEnd(99), the result would be this {1,2,3,99}. - If the current size is equal to the current capacity, then this list is full to its current capacity, and capacity will need to be increased before we can append the new element. To increase the capacity, call the makeCapacity method with an argument. value that is twice the current capacity. - This method will add the new element to the list at the next available index in the array storage. 5. insertAt method - Makes a place at the specified index by moving all items at this index and beyond to the next larger index. For example: given the list {1,2,3} and an instruction to insertAt (1,99), the result would be this {1,99,2,3}. - Throws a NoSuchElementException if the specified index is less than or greater than size. - If the current size is equal to the current capacity, then this list is full to its current capacity, and capacity will need to be increased before we can insert the new element. To increase the capacity, call the makeCapacity method with an argument. value that is twice the current capacity. 6. removeAt method - Removes the element at the specified index and moves all elements beyond that index to the next lower index. For example: given the list {1,2,3} and an instruction to removeAt (1), the result would be this {1,3}. - Throws a NoSuchElementException if the specified index is less than 0 or greater than or equal to size. 7. getAt method - Returns the item at the specified index. For example: given the list {1,2,3} and an instruction to getAt (1), the return value would be 2 . - Throws a NoSuchElementException if the specified index is less than or greater than or equal to size. 8. getsize method - Returns the number of elements currently stored in the list
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