Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can anyone help me all this homework!!! need to complete the MyarrayList java and MyLinkedList java Main file public class CSE205_Assignment02 { public static void
Can anyone help me all this homework!!! need to complete the MyarrayList java and MyLinkedList java
Main file
public class CSE205_Assignment02 { public static void main(String[] args) { System.out.println("MyArrayList tests -----------------------------"); MyList_ctor_test(new MyArrayList()); MyList_add1_test(new MyArrayList()); MyList_addMany_test(new MyArrayList()); MyList_insertAt0_test(new MyArrayList()); MyList_insertAt1_test(new MyArrayList()); MyList_insertAtEnd_test(new MyArrayList()); MyList_removeAt0_test(new MyArrayList()); MyList_removeAt1_test(new MyArrayList()); MyList_removeAtEnd_test(new MyArrayList()); MyList_emptyIterator_test(new MyArrayList()); MyList_nonEmptyIterator_test(new MyArrayList()); MyList_3ElementIterator_test(new MyArrayList()); System.out.println("MyLinkedList tests -----------------------------"); MyList_ctor_test(new MyLinkedList()); MyList_add1_test(new MyLinkedList()); MyList_addMany_test(new MyLinkedList()); MyList_insertAt0_test(new MyLinkedList()); MyList_insertAt1_test(new MyLinkedList()); MyList_insertAtEnd_test(new MyLinkedList()); MyList_removeAt0_test(new MyLinkedList()); MyList_removeAt1_test(new MyLinkedList()); MyList_removeAtEnd_test(new MyLinkedList()); MyList_emptyIterator_test(new MyLinkedList()); MyList_nonEmptyIterator_test(new MyLinkedList()); MyList_3ElementIterator_test(new MyLinkedList()); } public static void MyList_ctor_test(MyList aList) { assertEqual(0, aList.size(), "MyList_ctor_test: size", false); } public static void MyList_add1_test(MyList aList) { aList.add(1); assertEqual(1, aList.size(), "MyList_add1_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_add1_test: at(0)", false); } public static void MyList_addMany_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); assertEqual(3, aList.size(), "MyList_addMany_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_addMany_test: at(0)", false); assertEqual(2, (int)aList.at(1), "MyList_addMany_test: at(1)", false); assertEqual(3, (int)aList.at(2), "MyList_addMany_test: at(2)", false); } public static void MyList_insertAt0_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.insertAt(0, 999); assertEqual(4, aList.size(), "MyList_insertAt0_test: size", false); assertEqual(999, (int)aList.at(0), "MyList_insertAt0_test: at(0)", false); assertEqual(1, (int)aList.at(1), "MyList_insertAt0_test: at(1)", false); assertEqual(2, (int)aList.at(2), "MyList_insertAt0_test: at(2)", false); assertEqual(3, (int)aList.at(3), "MyList_insertAt0_test: at(3)", false); } public static void MyList_insertAt1_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.insertAt(1, 999); assertEqual(4, aList.size(), "MyList_insertAt1_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_insertAt1_test: at(0)", false); assertEqual(999, (int)aList.at(1), "MyList_insertAt1_test: at(1)", false); assertEqual(2, (int)aList.at(2), "MyList_insertAt1_test: at(2)", false); assertEqual(3, (int)aList.at(3), "MyList_insertAt1_test: at(3)", false); } public static void MyList_insertAtEnd_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.insertAt(2, 999); assertEqual(4, aList.size(), "MyList_insertAtEnd_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_insertAtEnd_test: at(0)", false); assertEqual(2, (int)aList.at(1), "MyList_insertAtEnd_test: at(1)", false); assertEqual(999, (int)aList.at(2), "MyList_insertAtEnd_test: at(2)", false); assertEqual(3, (int)aList.at(3), "MyList_insertAtEnd_test: at(3)", false); } public static void MyList_removeAt0_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.removeAt(0); assertEqual(2, aList.size(), "MyList_removeAt0_test: size", false); assertEqual(2, (int)aList.at(0), "MyList_removeAt0_test: at(1)", false); assertEqual(3, (int)aList.at(1), "MyList_removeAt0_test: at(2)", false); } public static void MyList_removeAt1_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.removeAt(1); assertEqual(2, aList.size(), "MyList_removeAt1_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_removeAt1_test: at(1)", false); assertEqual(3, (int)aList.at(1), "MyList_removeAt1_test: at(2)", false); } public static void MyList_removeAtEnd_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); aList.removeAt(2); assertEqual(2, aList.size(), "MyList_removeAtEnd_test: size", false); assertEqual(1, (int)aList.at(0), "MyList_removeAtEnd_test: at(1)", false); assertEqual(2, (int)aList.at(1), "MyList_removeAtEnd_test: at(2)", false); } public static void MyList_emptyIterator_test(MyList aList) { MyListIterator iter = aList.getIterator(); assertEqual(false, iter.hasNext(), "MyList_emptyIterator_test: hasNext()", false); } public static void MyList_nonEmptyIterator_test(MyList aList) { aList.add(1); MyListIterator iter = aList.getIterator(); assertEqual(true, iter.hasNext(), "MyList_nonEmptyIterator_test: hasNext()", false); } public static void MyList_3ElementIterator_test(MyList aList) { aList.add(1);aList.add(2);aList.add(3); MyListIterator iter = aList.getIterator(); assertEqual(true, iter.hasNext(), "MyList_3ElementIterator_test: hasNext()", false); assertEqual(1, (int)iter.next(), "MyList_3ElementIterator_test: next()", false); assertEqual(true, iter.hasNext(), "MyList_3ElementIterator_test: hasNext()", false); assertEqual(2, (int)iter.next(), "MyList_3ElementIterator_test: next()", false); assertEqual(true, iter.hasNext(), "MyList_3ElementIterator_test: hasNext()", false); assertEqual(3, (int)iter.next(), "MyList_3ElementIterator_test: next()", false); assertEqual(false, iter.hasNext(), "MyList_3ElementIterator_test: hasNext()", false); } public static void assertEqual(int expected, int actual, String message, boolean silentPass) { if (expected == actual && ! silentPass) System.out.printf("--- PASSED: %s ", message); if (expected != actual) System.out.printf("*** FAILED: (expected: %d, actual: %d) %s ", expected, actual, message); } public static void assertEqual(boolean expected, boolean actual, String message, boolean silentPass) { if (expected == actual && ! silentPass) System.out.printf("--- PASSED: %s ", message); if (expected != actual) System.out.printf("*** FAILED: (expected: %d, actual: %d) %s ", expected, actual, message); } }
My MyArrayList.
import java.util.NoSuchElementException; public class MyArrayList implements MyList { private int capacity = 16; private Object[ ] array = new Object [capacity]; private int size = 0; @Override public void add(Object o) { } @Override public int size() { } @Override public Object at(int index) { } @Override public void insertAt(int index, Object o) { } @Override public void removeAt(int index) { } 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 array[currentIndex]; } @Override public boolean hasNext( ) { return currentIndex +1
My Linkedlist
import java.util.NoSuchElementException; public class MyLinkedList implements MyList { private Node head = null; private int size = 0; @Override public void add(Object o) { } @Override public int size() { } @Override public Object at(int index) { } @Override public void insertAt(int index, Object o) { } @Override public void removeAt(int index) { } // Do not alter the code below . . ----- @Override public MyListIterator getIterator() { return new MyLinkedListIterator(); } private class MyLinkedListIterator implements MyListIterator { Node currentNode = null; @Override public Object next() { if (currentNode != null) currentNode = currentNode.next; else currentNode = head; return currentNode.data; } @Override public boolean hasNext() { if (currentNode != null) return currentNode.next != null; else return head != null; } } private class Node { public Object data = null; public Node next = null; } }my list
/** 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 add(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 at(int index); /** Returns the size of the list. @return the number of elements in the list */ public int size(); /** Returns a list iterator for this list. @return a list iterator for this list */ public MyListIterator getIterator(); }
MyListIterator
/** A list iterator allows access of a position in a linked 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(); }or gradinng, you Will eact earfi the same humioer of poiits. What to hand in, and by When,Ts discussed below Assignment Objectives After completing this assignment the student should be able to . 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. Assignment Requirements For this assignment you are given the following Java source code files CSE205_Assignment02.ava (This file is complete-you will make no changes to this file) MyListiterator.java MyList.java MyArrayList.java MyLinkedList.java (This file is complete - you will make no changes to this file) (This file is complete -you will make no changes to this file) (you must complete this file) (you must complete this file) The specifications for the files are given below (including the UML diagram on the following page) Special requirements You are not allowed to use any of the standard Java collation types (like ArrayList). You may use simple arrays. nc 1. add method a. Appends new item to end of list. For example: given the list (1, 2, 3 and an instruction to add( 99), the result would be this (1, 2, 3, 99) 2. 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 f1, 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 0 or greater than or equal to size a. b. 3. 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 11, 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 a. b. 4. at method Returns the item at the specified index. For example: given the list (1, 2, 3) and an instruction to at(1), the return value would be 2. Throws a NoSuchElementException if the specified index is less than 0 or greater than or equal to size a. b. 5. size methood 6. getIterator method MyArrayList a. Returns the number of elements currently stored in the list a. returns a MyListlterator object for this list initialized to the start of this list This concrete class will store its elements in an array of Objects. The initial capacity of this array will be 16 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 ensure capacity method 1. 2. ensureCapacity method
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