SlideShare a Scribd company logo
1 of 12
Download to read offline
How do I fix it in LinkedList.java?
this is what i did
LabProgram.java
LinkedList.java:
/**
* Defines a doubly-linked list class
* @author
* @author
*/
import java.util.NoSuchElementException;
public class LinkedList {
private class Node {
private T data;
private Node next;
private Node prev;
public Node(T data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private int length;
private Node first;
private Node last;
private Node iterator;
/**** CONSTRUCTORS ****/
/**
* Instantiates a new LinkedList with default values
* @postcondition
*/
public LinkedList() {
first = null;
last = null;
iterator = null;
length = 0;
}
/**
* Converts the given array into a LinkedList
* @param array the array of values to insert into this LinkedList
* @postcondition
*/
public LinkedList(T[] array) {
}
/**
* Instantiates a new LinkedList by copying another List
* @param original the LinkedList to copy
* @postcondition a new List object, which is an identical,
* but separate, copy of the LinkedList original
*/
public LinkedList(LinkedList original) {
}
/**** ACCESSORS ****/
public T getFirst() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
}
return last.data;
}
/**
* Returns the data stored in the iterator node
* @precondition
* @return the data stored in the iterator node
* @throw NullPointerException
*/
public T getIterator() throws NullPointerException {
if (iterator != null){
return iterator.data;
}else{
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/**
* Returns the current length of the LinkedList
* @return the length of the LinkedList from 0 to n
*/
public int getLength() {
return length;
}
/**
* Returns whether the LinkedList is currently empty
* @return whether the LinkedList is empty
*/
public boolean isEmpty() {
return length == 0;
}
/**
* Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return iterator == null;
}
/**** MUTATORS ****/
public void addFirst(T data) {
Node newNode = new Node(data);
if(isEmpty()){
first = newNode;
last = newNode;
}
else{
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if(isEmpty()){
first = newNode;
last = newNode;
}
else{
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
}
/**
* Inserts a new element after the iterator
* @param data the data to insert
* @precondition
* @throws NullPointerException
*/
public void addIterator(T data) throws NullPointerException{
if(iterator != null){
Node newNode = new Node(data);
newNode.next = iterator.next;
iterator.next = newNode;
if (iterator == first){
first = newNode;
}
}else{
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/
public void removeFirst() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("The list is empty");
}
if(length == 1){
first = null;
last = null;
iterator = null;
}
else{
if(iterator == first){
iterator = null;
}
first = first.next;
first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("The list is empty");
}
if(length == 1){
first = null;
last = null;
iterator = null;
}
else{
if(iterator == last){
iterator = null;
}
last = last.prev;
last.next = null;
}
length--;
}
/**
* removes the element referenced by the iterator
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void removeIterator() throws NullPointerException {
if(iterator != null){
if(iterator == first){
first = first.next;
}else{
Node prev = first;
while(prev.next != iterator){
prev = prev.next;
}
prev.next = iterator.next;
if (iterator == last){
last = prev;
}
}
iterator = null;
}else {
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/**
* places the iterator at the first node
* @postcondition
*/
public void positionIterator(){
iterator = first;
}
/**
* Moves the iterator one node towards the last
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void advanceIterator() throws NullPointerException {
if (!offEnd()){
iterator = iterator.next;
}else {
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/**
* Moves the iterator one node towards the first
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void reverseIterator() throws NullPointerException {
if(iterator != first && iterator != null){
Node prev = first;
while (prev.next != iterator){
prev = prev.next;
}
iterator = prev;
}
}
public void clear() {
first = null;
last = null;
iterator = null;
length = 0;
}
public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null){
result.append(temp.data + " ");
temp = temp.next;
}
return result.toString() + "n";
}
@SuppressWarnings("unchecked") //good practice to remove warning here
@Override
public boolean equals(Object obj) {
return false;
}
/**CHALLENGE METHODS*/
public void spinList(int numMoves) throws IllegalArgumentException{
}
public LinkedList altLists(LinkedList list) {
return null;
}
}
java.lang.NullPointerException
java.lang.NullPointerException
Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins
at null, as defined by the LinkedList constructors. Take a moment now to update your default
constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider
now what will happen if the iterator is at the first or last Node when these methods are called.
Update the methods to handle these edge cases now. We will develop additional methods during
this lab to work with the iterator. Remember to fill in the pre-and postconditions for each
method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
the LabProgram creates a list of numbers and inserts each into a list. The main() method then
calls various iterator methods, displaying results of the method operation. Use Develop mode to
test your Linked List iterator code as you develop it. In Submit mode you will need to complete
all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method
positionIterator() moves the iterator to the beginning of the list. public void positionIterator() //
fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method
offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement
getIterator() Method getIterator() returns the element in the Node where the iterator is currently
located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator
forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method
reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement
additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement
removeIterator() Method removeIterator () removes the Node currently referenced by the iterator
and sets the iterator to null.
Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins
at null, as defined by the LinkedList constructors. Take a moment now to update your default
constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider
now what will happen if the iterator is at the first or last Node when these methods are called.
Update the methods to handle these edge cases now. We will develop additional methods during
this lab to work with the iterator. Remember to fill in the pre-and postconditions for each
method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
the LabProgram creates a list of numbers and inserts each into a list. The main() method then
calls various iterator methods, displaying results of the method operation. Use Develop mode to
test your Linked List iterator code as you develop it. In Submit mode you will need to complete
all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method
positionIterator() moves the iterator to the beginning of the list. public void positionIterator() //
fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method
offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement
getIterator() Method getIterator() returns the element in the Node where the iterator is currently
located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator
forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method
reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement
additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement
removeIterator() Method removeIterator () removes the Node currently referenced by the iterator
and sets the iterator to null.
t java.util.scanner; c class LabProgram { ublic static void main(string[] args) { LabProgram lab
= new LabProgram(); // Make and display List LinkedList Integer list = new LinkedList >(); for
(int i=1;i<4;i++){ list. addLast (i); } System.out.print("Created list: " + list.tostring()); //
tostring() has system. out.println("list.offEnd(): " + list.offEnd()); system.out.println("list.
positionIterator()"); list.positionIterator(); system.out.println("list.offend(): " + list.offEnd());
system.out.println("list.getiterator(): " + list.getiterator ()); list.advanceiterator();
System.out.println("list.getiterator (): " + list.getiterator ());
system.out.println("list.advanceIterator ( )n); list.advanceIterator();
System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list.
reverseIterator ()n); list. reverseiterator(); System.out.println("list.getiterator(): " +
list.getiterator()); system.out.println("list.additerator (42)"); list.addIterator (42);
System.out.println("list.getiterator(): " + list.getiterator()); system.out.print "list. tostring(): " +
list.tostring()); system.out.println("list.advanceIterator ( )n); list. advanceiterator();
system.out.println("list.advanceIterator ( )n); list.advanceiterator();
System.out.println("list.additerator (99)"); list.additerator (99); system.out.print ("list. tostring():
" + list.tostring()); system.out.println("list.removeIterator()"); list.removerterator();
system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.offend(): " +
list.offend()); system.out.println("list. positionIterator()"); list. positionIterator();
system.out.println("list. removeIterator()"); list.removerterator();
system.out.println("list.offend(): " + list.offend()); system. out.print("list. tostring(): " +
list.tostring()); system.out.println("list. positionIterator()"); list.positioniterator();
system.out.println("list. advanceIterator ()"); list.advanceiterator();
system.out.println("list.advanceIterator ( ()"); list.advanceiterator(); system.out.println("list.
removeIterator()"); list. removerterator(); system.out.print("list. tostring(): " + list.tostring());

More Related Content

Similar to Fixing Iterator Methods

package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfxlynettalampleyxc
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdffcaindore
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdfasarudheen07
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxmckellarhastings
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfStewart29UReesa
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfsiennatimbok52331
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfarorastores
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfseoagam1
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfcontact41
 
Complete in JavaCardApp.javapublic class CardApp { private.pdf
Complete in JavaCardApp.javapublic class CardApp {   private.pdfComplete in JavaCardApp.javapublic class CardApp {   private.pdf
Complete in JavaCardApp.javapublic class CardApp { private.pdfMAYANKBANSAL1981
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdfangelsfashion1
 

Similar to Fixing Iterator Methods (20)

package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 
Complete in JavaCardApp.javapublic class CardApp { private.pdf
Complete in JavaCardApp.javapublic class CardApp {   private.pdfComplete in JavaCardApp.javapublic class CardApp {   private.pdf
Complete in JavaCardApp.javapublic class CardApp { private.pdf
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 

More from mail931892

I need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdfI need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdfmail931892
 
I need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfI need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfmail931892
 
I need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfI need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfmail931892
 
I need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfI need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfmail931892
 
I have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfI have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfmail931892
 
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfI am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfmail931892
 
how can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfhow can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfmail931892
 
Hi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfHi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfmail931892
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfmail931892
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
For this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfFor this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfmail931892
 
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfGinny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfmail931892
 

More from mail931892 (12)

I need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdfI need help writing test Codepackage org.example;import j.pdf
I need help writing test Codepackage org.example;import j.pdf
 
I need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfI need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdf
 
I need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfI need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdf
 
I need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfI need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdf
 
I have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfI have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdf
 
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfI am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
 
how can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfhow can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdf
 
Hi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfHi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdf
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
For this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfFor this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdf
 
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfGinny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Fixing Iterator Methods

  • 1. How do I fix it in LinkedList.java? this is what i did LabProgram.java LinkedList.java: /** * Defines a doubly-linked list class * @author * @author */ import java.util.NoSuchElementException; public class LinkedList { private class Node { private T data; private Node next; private Node prev; public Node(T data) { this.data = data; this.next = null; this.prev = null; } } private int length; private Node first; private Node last; private Node iterator; /**** CONSTRUCTORS ****/ /** * Instantiates a new LinkedList with default values * @postcondition */ public LinkedList() { first = null;
  • 2. last = null; iterator = null; length = 0; } /** * Converts the given array into a LinkedList * @param array the array of values to insert into this LinkedList * @postcondition */ public LinkedList(T[] array) { } /** * Instantiates a new LinkedList by copying another List * @param original the LinkedList to copy * @postcondition a new List object, which is an identical, * but separate, copy of the LinkedList original */ public LinkedList(LinkedList original) { } /**** ACCESSORS ****/ public T getFirst() throws NoSuchElementException { if (isEmpty()){ throw new NoSuchElementException("The list is empty"); } return first.data; } public T getLast() throws NoSuchElementException { if (isEmpty()){ throw new NoSuchElementException("The list is empty");
  • 3. } return last.data; } /** * Returns the data stored in the iterator node * @precondition * @return the data stored in the iterator node * @throw NullPointerException */ public T getIterator() throws NullPointerException { if (iterator != null){ return iterator.data; }else{ throw new NullPointerException("Iterator is off the end opf the list."); } } /** * Returns the current length of the LinkedList * @return the length of the LinkedList from 0 to n */ public int getLength() { return length; } /** * Returns whether the LinkedList is currently empty * @return whether the LinkedList is empty */ public boolean isEmpty() { return length == 0; } /**
  • 4. * Returns whether the iterator is offEnd, i.e. null * @return whether the iterator is null */ public boolean offEnd() { return iterator == null; } /**** MUTATORS ****/ public void addFirst(T data) { Node newNode = new Node(data); if(isEmpty()){ first = newNode; last = newNode; } else{ newNode.next = first; first.prev = newNode; first = newNode; } length++; } public void addLast(T data) { Node newNode = new Node(data); if(isEmpty()){ first = newNode; last = newNode; }
  • 5. else{ last.next = newNode; newNode.prev = last; last = newNode; } length++; } /** * Inserts a new element after the iterator * @param data the data to insert * @precondition * @throws NullPointerException */ public void addIterator(T data) throws NullPointerException{ if(iterator != null){ Node newNode = new Node(data); newNode.next = iterator.next; iterator.next = newNode; if (iterator == first){ first = newNode; } }else{ throw new NullPointerException("Iterator is off the end opf the list."); } } / public void removeFirst() throws NoSuchElementException { if(isEmpty()){ throw new NoSuchElementException("The list is empty");
  • 6. } if(length == 1){ first = null; last = null; iterator = null; } else{ if(iterator == first){ iterator = null; } first = first.next; first.prev = null; } length--; } public void removeLast() throws NoSuchElementException { if(isEmpty()){ throw new NoSuchElementException("The list is empty"); } if(length == 1){ first = null; last = null; iterator = null; } else{ if(iterator == last){ iterator = null; } last = last.prev; last.next = null;
  • 7. } length--; } /** * removes the element referenced by the iterator * @precondition * @postcondition * @throws NullPointerException */ public void removeIterator() throws NullPointerException { if(iterator != null){ if(iterator == first){ first = first.next; }else{ Node prev = first; while(prev.next != iterator){ prev = prev.next; } prev.next = iterator.next; if (iterator == last){ last = prev; } } iterator = null; }else { throw new NullPointerException("Iterator is off the end opf the list.");
  • 8. } } /** * places the iterator at the first node * @postcondition */ public void positionIterator(){ iterator = first; } /** * Moves the iterator one node towards the last * @precondition * @postcondition * @throws NullPointerException */ public void advanceIterator() throws NullPointerException { if (!offEnd()){ iterator = iterator.next; }else { throw new NullPointerException("Iterator is off the end opf the list."); } } /** * Moves the iterator one node towards the first * @precondition * @postcondition * @throws NullPointerException */ public void reverseIterator() throws NullPointerException { if(iterator != first && iterator != null){
  • 9. Node prev = first; while (prev.next != iterator){ prev = prev.next; } iterator = prev; } } public void clear() { first = null; last = null; iterator = null; length = 0; } public String toString() { StringBuilder result = new StringBuilder(); Node temp = first; while (temp != null){ result.append(temp.data + " "); temp = temp.next; }
  • 10. return result.toString() + "n"; } @SuppressWarnings("unchecked") //good practice to remove warning here @Override public boolean equals(Object obj) { return false; } /**CHALLENGE METHODS*/ public void spinList(int numMoves) throws IllegalArgumentException{ } public LinkedList altLists(LinkedList list) { return null; } } java.lang.NullPointerException java.lang.NullPointerException Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins at null, as defined by the LinkedList constructors. Take a moment now to update your default constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider now what will happen if the iterator is at the first or last Node when these methods are called. Update the methods to handle these edge cases now. We will develop additional methods during this lab to work with the iterator. Remember to fill in the pre-and postconditions for each method, as needed. Also inspect the LabProgram. java file and notice that the main () method of the LabProgram creates a list of numbers and inserts each into a list. The main() method then calls various iterator methods, displaying results of the method operation. Use Develop mode to
  • 11. test your Linked List iterator code as you develop it. In Submit mode you will need to complete all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method positionIterator() moves the iterator to the beginning of the list. public void positionIterator() // fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement getIterator() Method getIterator() returns the element in the Node where the iterator is currently located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement removeIterator() Method removeIterator () removes the Node currently referenced by the iterator and sets the iterator to null. Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins at null, as defined by the LinkedList constructors. Take a moment now to update your default constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider now what will happen if the iterator is at the first or last Node when these methods are called. Update the methods to handle these edge cases now. We will develop additional methods during this lab to work with the iterator. Remember to fill in the pre-and postconditions for each method, as needed. Also inspect the LabProgram. java file and notice that the main () method of the LabProgram creates a list of numbers and inserts each into a list. The main() method then calls various iterator methods, displaying results of the method operation. Use Develop mode to test your Linked List iterator code as you develop it. In Submit mode you will need to complete all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method positionIterator() moves the iterator to the beginning of the list. public void positionIterator() // fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement getIterator() Method getIterator() returns the element in the Node where the iterator is currently located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement removeIterator() Method removeIterator () removes the Node currently referenced by the iterator and sets the iterator to null. t java.util.scanner; c class LabProgram { ublic static void main(string[] args) { LabProgram lab
  • 12. = new LabProgram(); // Make and display List LinkedList Integer list = new LinkedList >(); for (int i=1;i<4;i++){ list. addLast (i); } System.out.print("Created list: " + list.tostring()); // tostring() has system. out.println("list.offEnd(): " + list.offEnd()); system.out.println("list. positionIterator()"); list.positionIterator(); system.out.println("list.offend(): " + list.offEnd()); system.out.println("list.getiterator(): " + list.getiterator ()); list.advanceiterator(); System.out.println("list.getiterator (): " + list.getiterator ()); system.out.println("list.advanceIterator ( )n); list.advanceIterator(); System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list. reverseIterator ()n); list. reverseiterator(); System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list.additerator (42)"); list.addIterator (42); System.out.println("list.getiterator(): " + list.getiterator()); system.out.print "list. tostring(): " + list.tostring()); system.out.println("list.advanceIterator ( )n); list. advanceiterator(); system.out.println("list.advanceIterator ( )n); list.advanceiterator(); System.out.println("list.additerator (99)"); list.additerator (99); system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.removeIterator()"); list.removerterator(); system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.offend(): " + list.offend()); system.out.println("list. positionIterator()"); list. positionIterator(); system.out.println("list. removeIterator()"); list.removerterator(); system.out.println("list.offend(): " + list.offend()); system. out.print("list. tostring(): " + list.tostring()); system.out.println("list. positionIterator()"); list.positioniterator(); system.out.println("list. advanceIterator ()"); list.advanceiterator(); system.out.println("list.advanceIterator ( ()"); list.advanceiterator(); system.out.println("list. removeIterator()"); list. removerterator(); system.out.print("list. tostring(): " + list.tostring());