Question
Can someone please help me with the MyArrayList.java and MyLinkedList.java code for this assignment? The words in bold is what I need help with. I
Can someone please help me with the MyArrayList.java and MyLinkedList.java 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 sample MyArrayList.java file, the sample MyLinkedList.java file, the MyList.java file, the MyListIterator.java file and the OOPTest.java files. The MyArrayList.java file implements the MyList.java file. The MyLinkedList.java code implements the MyList.java file.
Only the MyArrayList.java file and the MyLinkedList.java file need to be changed.
Below are the instructions:
*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
===============================================================================================================================
*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
===============================================================================================================================
public interface MyList {
public void add(Object o);
public void insert(int index, Object o);
public void remove(int index); public Object get(int index); public int size(); public MyListIterator getIterator(); }
===============================================================================================================================
MyListIterator.java
===============================================================================================================================
public interface MyListIterator { Object next();
boolean hasNext(); }
===============================================================================================================================
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++; } }
===============================================================================================
OOPTest.java
================================================================================================
import java.io.PrintStream;
import java.lang.reflect.*;
public class OOPTest {
public static boolean testHasField(PrintStream out, String className, String fieldSig) {
if (hasField(className, fieldSig)) {
out.println("PASS - correct field found in class " + className);
out.println(" " + fieldSig);
out.println();
return true;
} else {
out.println("FAIL - missing field in class " + className);
out.println("!!!! " + fieldSig);
out.println();
return false;
}
}
public static boolean testHasConstructor(PrintStream out, String className, String ctorSig) {
if (hasConstructor(className, ctorSig)) {
out.println("PASS - correct constructor found in class " + className);
out.println(" " + ctorSig);
out.println();
return true;
} else {
out.println("FAIL - missing constructor in class " + className);
out.println("!!!! " + ctorSig);
out.println();
return false;
}
}
public static boolean testHasMethod(PrintStream out, String className, String methodSig) {
if (hasMethod(className, methodSig)) {
out.println("PASS - correct method found in class " + className);
out.println(" " + methodSig);
out.println();
return true;
} else {
out.println("FAIL - missing method in class " + className);
out.println("!!!! " + methodSig);
out.println();
return false;
}
}
static boolean testAExtendsB(PrintStream out, String classAName, String classBName) {
try { // must handle exception that may be thrown by Class.forName
Class a = Class.forName(classAName);
Class b = Class.forName(classBName);
if (b.isAssignableFrom(a)) {
out.println("PASS - correct inheritance in " + classAName);
out.println(" " + classAName + " extends (or implements) " + classBName);
out.println();
return true;
} else {
out.println("FAIL - incorrect inheritance in" + classAName);
out.println("!!!! " + classAName + " extends (or implements) " + classBName);
out.println();
return false;
}
} catch (Exception e) {
}
return false;
}
public static boolean hasField(String className, String sig) {
try { // must handle exception that may be thrown by getDeclaredFields
Class c = Class.forName(className);
for (Field f : c.getDeclaredFields()) {
// simplify the fully qualified sig string returned by Java's
// reflection services
String fSig = f.toString();
fSig = fSig.replaceAll("java\\.lang\\.", "");
fSig = fSig.replaceAll("java\\.util\\.", "");
fSig = fSig.replaceAll(className + "\\.", "");
fSig = fSig.replaceAll(className + "\\$", "");
if (sig.equals(fSig))
return true;
}
} catch (Exception e) {
}
return false;
}
public static boolean hasConstructor(String className, String sig) {
try { // must handle exception that may be thrown by getDeclaredMethods
Class c = Class.forName(className);
for (Constructor m : c.getDeclaredConstructors()) {
// simplify the fully qualified sig string returned by Java's
// reflection services
String cSig = m.toString();
cSig = cSig.replaceAll("java\\.lang\\.", "");
cSig = cSig.replaceAll("java\\.util\\.", "");
if (sig.equals(cSig))
return true;
}
} catch (Exception e) {
}
return false;
}
static boolean hasMethod(String className, String sig) {
try { // must handle exception that may be thrown by getDeclaredMethods
Class c = Class.forName(className);
for (Method m : c.getDeclaredMethods()) {
// simplify the fully qualified sig string returned by Java's
// reflection services
String mSig = m.toString();
mSig = mSig.replaceAll("java\\.lang\\.", "");
mSig = mSig.replaceAll("java\\.util\\.", "");
mSig = mSig.replaceAll(className + "\\.", "");
if (sig.equals(mSig))
return true;
}
} catch (Exception e) {
}
return false;
}
}
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 2 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