Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write concrete classes that implement Java Interfaces according to specifications given in UML. Implement the major functionality of an array list. Implement the major

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Write concrete classes that implement Java Interfaces according to specifications given in UML. Implement the major functionality of an array list. Implement the major functionality of a linked 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. MyListIterator < > + hasNext(): boolean + next(): Object I I I I I I I MyList < > + append(item: Object) : void + insertAt(index: int, item: Object) : void + removeAt(index: int) : void + getAt( indes: int) : Object + getSize(): int - getiterator(): MyListIterator implements implements MyArrayList - currentCapacity: int - size: int - storage: Object[] + makeCapacity(minCapacity: int) : void + trimExcess(): void MyArrayListIterator UML CLass Diagram: MyArrayList Structure of the Fields As described by the UML Class Diagram above, your MyArrayList class must have the following fields: a private field named currentCapacity 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 append 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. Additional Information 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 current 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 currentCapacity 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 thod trimExcess 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. append method Appends new item to end of list. For example: given the list {1, 2, 3) and an instruction to append (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 NoSuch Element Exception 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 NoSuch Element Exception 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 NoSuch Element Exception if the specified index is less than 0 or greater than or equal to size. 8. getSize method Returns the number of elements currently stored in the list. 1 // Complete the implementation of your MyArrayList class in this file IN3456789 2 4 public class MyArrayList implements MyList { 10 11 12 13 14 15 567 16 17 // Implement the required fields and methods here // Do not alter the code below @Override public MyListIterator getIterator() { return new MyArrayListIterator(); } private class MyArrayListIterator implements MyListIterator { int currentIndex -1; = } } private class MyArrayListIterator implements MyListIterator { int currentIndex = -1; @Override public Object next() { ++currentIndex; return storage [currentIndex]; } @Override public boolean hasNext() { return currentIndex < size - 1; Write concrete classes that implement Java Interfaces according to specifications given in UML. Implement the major functionality of an array list. Implement the major functionality of a linked list. You are not allowed to use any of the standard Java collection types (like ArrayList) for this assignment. You are also not allowed to use arrays for this assignment. 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) MyLinkedList.java (You must complete this file) Main.java (You may use this file to write code to test your MyLinkedList) You must complete the public class named MyLinked List with fields and methods as defined below. Your MyLinkedList will implement the MyList interface that is provided in the myList.java file. MyListIterator < > + hasNext(): boolean + next(): Object MyList < > + append(item: Object) : void + insertAt(index: int, item : Object) : void + removeAt(index: int) : void + getAt( indes: int) : Object + getSize(): int + getiterator(): MyListIterator implements implements! MyLinkedList -head: Node - size : int Node + data : Object + next: Node MyLinked ListIterator UML CLass Diagram: MyLinkedList Structure of the Fields As described by the UML Class Diagram above, your MyLinked List class must have the following fields: a private field named head of type Node, initialized to null a private field named size of type int, initialized to Structure of the Methods As described by the UML Class Diagram above, your MyArrayList class must have the following methods: a public method named append 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 Note that: these methods are declared in the MyList interface. You will be implementing these methods in this MyLinkedList concrete derived class. the getIterator method and the MyArrayListIterator class are already implemented for you in the MyLinked List class. Make no changes to this code. the Node class is already implemented for you in the MyLinekdList class. Make no changes to this code. Additional Information MyLinekdList 1. This concrete class will store its elements in Node objects. Each Node object has a data variable that stores the element and a next variable that stores a reference to the next Node object in the list. Each instance of MyLinked List has a Node variable called head. When size is then head is null. When size is not then head is a reference to the first Node object. For each Node object in the list, if this Node's next is null then this is the last Node in the list. 2. append method Appends new item to end of list. For example: given the list {1, 2, 3) and an instruction to append(99), the result would be this {1, 2, 3, 99}. this method will construct a new Node object who's data is the element to be added, and place this Node appropriately into the list. 3. insertAt method Inserts a new Node at the specified index in the list. Given the list {1, 2, 3} and an instruction to insertAt(1, 99), the result would be this {1, 99, 2, 3). Throws a NoSuch Element Exception if the specified index is less than or greater than size. this method will construct a new Node object who's data is the element to be added, and place this Node appropriately into the list. 4. removeAt method Removes the element at the specified index. For example: given the list {1, 2, 3) and an instruction to removeAt (1), the result would be this {1, 3}. Throws a NoSuch Element Exception if the specified index is less than 0 or greater than or equal to size. 5. 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 NoSuch Element Exception if the specified index is less than 0 or greater than or equal to size. 6. getSize method Returns the number of elements currently stored in the list. // Complete the implementation of your MyLinkedList class in this file public class MyLinked List implements MyList { // Implement the required fields and methods here // Do not alter the code below public MyListIterator getIterator() { return new MyLinkedListIterator(); } private class MyLinkedListIterator implements MyListIterator { Node currentNode = null; @Override public Object next() { if (currentNode != null) else } currentNode = currentNode.next; currentNode head; = return currentNode.data; @Override public boolean hasNext() { if (currentNode != null) return currentNode.next != null; else return head != null; } } } } return head != null; class Node { null; public Object data public Node next = null; =

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Java Concepts Late Objects

Authors: Cay S. Horstmann

3rd Edition

1119186714, 978-1119186717

More Books

Students also viewed these Programming questions

Question

=+c) In what month of the year are gas prices highest?

Answered: 1 week ago