Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For Java Please implement LinkedListWithIterator.java for list operations and iterator methods ( hasNext() , next() , and remove() ). You may copy and modify the

For Java

Please implement LinkedListWithIterator.java for list operations and iterator methods (hasNext(), next(), and remove()). You may copy and modify the posted TestArrayListWithIterator.java to create TestLinkedListWithIterator.java to test your LinkedListWithIterator.java class.

ListInferface.java public interface ListInterface { public void add(T newEntry); public boolean add(int newPosition, T newEntry); public T remove(int givenPosition); public void clear(); public boolean replace(int givenPosition, T newEntry); public T getEntry(int givenPosition); public boolean contains(T anEntry); public int getLength(); public boolean isEmpty(); public boolean isFull(); //public T[] toArray(); public void printList(); }

ListWithIteratorInterface.java

import java.util.Iterator;

public interface ListWithIteratorInterface extends ListInterface { public Iterator getIterator(); } // this will provide all methods specified in the ListInterface plus the getIterator

TestArrayListWithIterator.java

import java.util.Scanner; import java.util.Iterator; import java.util.NoSuchElementException;

public class TestArrayListWithIterator { public static void main(String [] args) { ArrayListWithIterator list = new ArrayListWithIterator(); Scanner input = new Scanner(System.in); int choice, givenPosition; String item; boolean success; Iterator myIterator = list.getIterator(); System.out.println("0. exit"); System.out.println("1. add item to the end of the list"); System.out.println("2. add item to a specific location in the list"); System.out.println("3. remove from a specific location in the list"); System.out.println("4. clear the list"); System.out.println("5. replace an item in the list"); System.out.println("6. get an entry from a specific location"); System.out.println("7. check if a specific item exist"); System.out.println("8. get list size"); System.out.println("9. check if list is empty"); System.out.println("10. iterator hasNext: "); System.out.println("11. iterator next: "); System.out.println("12. iterator remove: "); System.out.print("Enter a choice: "); choice = input.nextInt(); while (choice != 0) { switch (choice) { case 1: // add to the end System.out.print("Enter an item: "); item = input.next(); list.add(item); list.printList(); myIterator = list.getIterator(); break; case 2: // add to a specific location System.out.print("Enter a location and an item: "); givenPosition = input.nextInt(); item = input.next(); success = list.add(givenPosition, item); if (success) list.printList(); else System.out.println(item + " could not be added."); myIterator = list.getIterator(); break; case 3: // remove from a specific location System.out.print("Enter a location to be removed: "); givenPosition = input.nextInt(); item = list.remove(givenPosition); System.out.println(item + " removed from the list."); list.printList(); myIterator = list.getIterator(); break; case 4: // clear the list list.clear(); break; case 5: // replace System.out.print("Enter a location and item to be replaced: "); givenPosition = input.nextInt(); item = input.next(); success = list.replace(givenPosition, item); if (success) { System.out.println(item + " replaced from the list."); list.printList(); } else System.out.println(item + " could not be replaced."); break; case 6: // get an entry System.out.print("Enter a location: "); givenPosition = input.nextInt(); item = list.getEntry(givenPosition); System.out.println("position " + givenPosition + " is " + item); list.printList(); break; case 7: // contains System.out.print("Enter an item to be searched: "); item = input.next(); success = list.contains(item); if (success) { System.out.println(item + " exists in the list."); list.printList(); } else System.out.println(item + " does not exist."); break; case 8: // get length System.out.println("list size is: " + list.getLength()); break; case 9: // check if empty if (list.isEmpty()) System.out.println("The list is empty."); else System.out.println("The list is not empty."); break; case 10: // hesNext if (myIterator.hasNext()) System.out.println("iterator has next"); else System.out.println("iterator has reached the end"); break; case 11: // next if (myIterator.hasNext()) { item = myIterator.next(); System.out.println(item + " is next"); } else System.out.println("iterator has reached the end"); break; case 12: // remove myIterator.remove(); list.printList(); break; } System.out.println("0. exit"); System.out.println("1. add item to the end of the list"); System.out.println("2. add item to a specific location in the list"); System.out.println("3. remove from a specific location in the list"); System.out.println("4. clear the list"); System.out.println("5. replace an item in the list"); System.out.println("6. get an entry from a specific location"); System.out.println("7. check if a specific item exist"); System.out.println("8. get list size"); System.out.println("9. check if list is empty"); System.out.println("10. iterator hasNext: "); System.out.println("11. iterator next: "); System.out.println("12. iterator remove: "); System.out.print("Enter a choice: "); choice = input.nextInt(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

Know how to use reservations systems to inventory demand.

Answered: 1 week ago