Question
Can someone please help me with the Linked List code for this assignment? The words in bold is what I need help with. I am
Can someone please help me with the Linked List code for this assignment?
The words in bold is what I need help with.
I am not allowed to use any of the standard Java collection types (like ArrayList). I can use simple arrays. Thank you!
I have included the Main file, the Linked list file and the myList file. The Linked list code implements the myListfile.
Main.java
============================================================================================================================
public class Main {
static int pass = 0;
static int fail = 0;
public static void main(String[] args) {
System.out.println("========== MyArrayList Class Structure ==========");
testMyArrayListSuperClass();
testMyArrayListFields();
testMyArrayListMethods();
System.out.println("========== MyLinkedList Class Structure ==========");
testMyLinkedListSuperClass();
testMyLinkedListFields();
testMyLinkedListMethods();
System.out.println("-------------------------------------------------");
System.out.println("Passed: " + pass);
System.out.println("Failed: " + fail);
System.out.println(" --------");
System.out.printf (" %.2f %% correct ", (float)pass / (fail + pass) * 100);
System.out.println("-------------------------------------------------");
}
private static void testMyArrayListSuperClass() {
count(OOPTest.testAExtendsB(System.out, "MyArrayList", "MyList"));
}
private static void testMyLinkedListSuperClass() {
count(OOPTest.testAExtendsB(System.out, "MyLinkedList", "MyList"));
}
static void testMyArrayListFields() {
String className = "MyArrayList";
String[] sigs = {"private int capacity",
"private Object[] array",
"private int size"};
for (String sig : sigs)
count(OOPTest.testHasField(System.out, className, sig));
}
static void testMyArrayListMethods() {
String className = "MyArrayList";
String[] sigs = {"public void add(Object)",
"public int size()",
"public Object get(int)",
"public void insert(int,Object)",
"public void remove(int)",
"public void ensureCapacity(int)",
"public void trimToSize()",
"public MyListIterator getIterator()"};
for (String sig : sigs)
count(OOPTest.testHasMethod(System.out, className, sig));
}
static void testMyLinkedListFields() {
String className = "MyLinkedList";
String[] sigs = {"private Node head",
"private int size"};
for (String sig : sigs)
count(OOPTest.testHasField(System.out, className, sig));
}
static void testMyLinkedListMethods() {
String className = "MyLinkedList";
String[] sigs = {"public void add(Object)",
"public int size()",
"public Object get(int)",
"public void insert(int,Object)",
"public void remove(int)",
"public MyListIterator getIterator()"};
for (String sig : sigs)
count(OOPTest.testHasMethod(System.out, className, sig));
}
private static void count(boolean testResult) {
if (testResult)
pass++;
else
fail++;
}
}
MyLinkedList.java
==============================
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 void insert(int index, Object o) {
}
@Override
public void remove(int index) {
}
@Override
public Object get(int index) {
}
@Override
public int size() {
}
// 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) currentNode = currentNode.next; else currentNode = head;
return currentNode.data; }
@Override public boolean hasNext() { if (currentNode != null) return currentNode.next != null; else return head != null; } } class Node { public Object data = null; public Node next = null; } }
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 add(Object o); /** Inserts an element at the specified index Throws NoSuchElementException if index is out of bounds. */ public void insert(int index, Object o); /** Removes the element at the specified index Throws NoSuchElementException if index is out of bounds. */ public void remove(int index); /** Returns the element at the specified index Throws NoSuchElementException if index is out of bounds. */ public Object get(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(); }
MyArrayList.java
=======================================================================================
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 void insert(int index, Object o) {
}
@Override public void remove(int index) {
} @Override public Object get(int index) {
}
@Override public int 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 array[currentIndex]; }
@Override public boolean hasNext( ) { return currentIndex +1
MyListIterator.java
===============================================================================
/** 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(); }
Assignment 02 : 100 pts CSE 205 Object Oriented Programming & Data Structures Assignment 02 - Array List & Linked List You may work in pairs (that is, as a group of two) with a partner on this assignment if you wish, or you may work alone. If you work with a partner, only submit one lab project with both of your names in the source code file(s) for grading: you will each earn the same number of points. What to hand in, and by when, is 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: MyListIterator.java (This file is complete - make no changes to this file) MyList.java (This file is complete - make no changes to this file) MvArrayList.java (you must complete this file) MyLinkedlist.java (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 collection types (like ArrayList). You may use simple arrays. All MyList Concrete Classes (MyArrayList & MyLinkedList) 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. insert method a. 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 insert(1, 99), the result would be this {1, 99, 2, 3). b. Throws a NoSuchElementException if the specified index is less than 0 or greater than size. 3. remove method a. 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 remove(1), the result would be this {1, 3} b. Throws a NoSuchElementException if the specified index is less than 0 or greater than size. 4. get method a. Returns the item at the specified index. For example: given the list {1, 2, 3) and an instruction to get(1), the return value would be 2. b. Throws a NoSuchElementException if the specified index is less than 0 or greater than or equal to size. 5. size method a. Returns the number of elements currently stored in the list 6. getIterator method a. returns a MyListIterator object for this list initialized to the start of this list MyArrayList 1. 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 ensureCapacity method. 2. ensureCapacity method a. This method will take an int mincapacity as an argument. Page 1 CSE 205 Object Oriented Programming & Data Structures Assignment 02 - 100 pts b. If minCapacity is less than current size, then simply return without taking any action. c. If mincapacity is less than 16 then the new capacity should be 16. d. This method will allocate a new array of Object with the new capacity i. Then copy over all elements from the old array to the new array ii. Then store the new array in the private array variable for this instance 3. trimToSize method a. This method will remove any excess capacity by simply calling the ensureCapacity method with an argument value that is equal to the current size of this list. 4. add method a. If the current size is equal to the current capacity, then this method will call the ensureCapacity method with the argument (capacity * 2) before adding the new element. MyLinkedList 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 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. add and insertAt methods a. 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. UML Class Diagram MyList > + hasNext(): boolean + next(): Object + add(item: Object) : void + insert(index:int, item: Object) : void + remove(index: int) : void + get indes: int) : Object + size(): int + getiterator(): MyListIterator implements! implements MyArrayList MyLinkedlist - capacity: int - Size : int - array: Object[] -head: Node - Size : int + ensureCapacity(minCapacity: int) : void + trimToSize(): void Node MyArrayListiterator + data: Object + next : Node -- -- Implements MyLinkedlistiterator ements Page 2Step 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