Question
PLEASE HELP! I NEED MY CODE FOR THE MyLinkedList.java section fixed! The code below is the code I submitted but I keep on getting this
PLEASE HELP! I NEED MY CODE FOR THE MyLinkedList.java section fixed! The code below is the code I submitted but I keep on getting this error.
[
Failed to compile
MyLinkedList.java:4: error: MyLinkedList is not abstract and does not override abstract method addToEnd(Object) in MyList public class MyLinkedList implements MyList { ^ MyLinkedList.java:36: error: method does not override or implement a method from a supertype @Override ^ 2 errors
]
__________________________________________________________
17.2 MyLinkedList (Individual Assignment)
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 MyLinkedList with fields and methods as defined below. Your MyLinkedList will implement the MyList interface that is provided in the myList.java file.
UML CLass Diagram: MyLinkedList
Structure of the Fields
As described by the UML Class Diagram above, your MyLinkedList 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 0
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
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 MyListIterator class are already implemented for you in the MyLinkedList class. Make no changes to this code.
the Node class is already implemented for you in the MyLinkedList class. Make no changes to this code.
_____________________________________________________
MyLinkedList.java
// Complete the implementation of your MyLinkedList class in this file import java.util.NoSuchElementException;
public class MyLinkedList implements MyList { // Implement the required fields and methods here private Node head = null; private int size = 0;
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; public Node next; }
// appends the o at end of the list @Override public void append(Object o) { Node node = new Node(); node.data = o; node.next = null; if (head == null) { head = node; } else { Node curr = head; while (curr.next != null) { curr = curr.next; } curr.next = node; } size++; }
// inserts the o at index if the index is not valid then trows the exception @Override public void insertAt(int index, Object o) { Node curr = head,prev = null; int pos = 0; while(curr != null) { if(pos == index) { break; } prev = curr; curr = curr.next; pos++; } if(curr != null) { Node node = new Node(); node.data = o; node.next = null; if(prev == null) { node.next = head; head = node; } else { node.next = curr; prev.next = node; } size++; } else { throw new NoSuchElementException(); } }
// removes the element at index if the index is not valid then throws exception @Override public void removeAt(int index) { Node curr = head,prev = null; int pos = 0; while(curr != null) { if(pos == index) { break; } prev = curr; curr = curr.next; pos++; } if(curr != null) { if(prev == null) { head = head.next; } else { prev.next = curr.next; } size--; } else { throw new NoSuchElementException(); } }
// returns the element at index if the index is not valid then throws the exception @Override public Object getAt(int index) { Node curr = head,prev = null; int pos = 0; while(curr != null) { if(pos == index) { break; } prev = curr; curr = curr.next; pos++; } if(curr != null) { return curr.data; } else { throw new NoSuchElementException(); } }
// returns the size @Override public int getSize() { return size; }
@Override public MyListIterator getIterator() { return new MyLinkedListIterator(); }
}
_____________________________________________________
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 wrote and run code to test your MyArrayList class
public class Main { public static void main(String[] args) { } }
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