Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[JAVA] Please help with this program about how to use Iterators. I provided the assignment and code I have below. Thank you! package lab7; import

[JAVA] Please help with this program about how to use Iterators. I provided the assignment and code I have below. Thank you!

image text in transcribed

package lab7;

import list.OrderedList; import list.UnorderedList; import sort.Sort;

import java.util.Iterator; public class ListApp { public static void main(String[] args){ String[] str = {"hello","this","is","the","beginning"}; UnorderedList ulist = new UnorderedList(); OrderedList olist = new OrderedList(); for(String s : str){ ulist.add(s); //System.out.println(ulist); olist.add(s); System.out.println(olist); } try{ System.out.println(ulist); ulist.removeFirst(); System.out.println(ulist); System.out.println(olist); Iterator it = ulist.iterator(); while(it.hasNext()) System.out.println("->" + it.next()); } catch(Exception e){}

System.out.println(ulist.search(str[4])); System.out.println(olist.search(str[4])); System.out.println(ulist.search("hello")); System.out.println(olist.search("hello")); System.out.println(ulist.search("Hello")); olist.remove("beginning"); ulist.remove("beginning"); System.out.println(olist); System.out.println(ulist); Integer[] x = {5, 4, 2, 6, 1, 7}; Sort.insertionSort(x); for(int n : x) System.out.println(n); Sort.insertionSort(str); for(String s : str) System.out.println(s); } }

------------------------------------

package list;

import jdk.jshell.spi.ExecutionControl;

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

public abstract class List> implements ListADT {

protected class Node>{ private E data; private Node next; /** * default constructor */ public Node(){ data = null; next = null; } /** * parameterized constructor with the item * @param item */ public Node(E item){ data = item; next = null; } /** * parameterized constructor with the item and a reference to the next Node * @param item * @param n */ public Node(E item, Node n){ data = item; next = n; } /** * sets (changes) the reference to the next Node * @param n */ public void setNext(Node n){ next = n; } /** * returns a reference to the next Node in the list * @return next */ public Node getNext(){ return next; } /** * sets (changes) the data in the Node * @param item */ public void setData(E item){ data = item; } /** * returns the data in the Node * @return data - the data in the node */ public E getData(){ return data; } }

class ListIterator> implements Iterator { /** * singly linked list used to store the contents of this list */ private Node list; /** * count the number of items in this list */ private int count; /** * current the index of current item in the list */ private int current; /** * Parameterized constructor for the ListIterator class. * @param d the list * @param c the number of items in the list */ public ListIterator(Node head, int c){ list = head; count = c; current = 0; } /** * Returns true if there is another item in the list. * @return true if there is another item in the list */ public boolean hasNext() { return current

protected Node front = new Node(); protected int size;

@Override abstract public void add(T item);

@Override public T first() throws ListEmptyException { // TODO Auto-generated method stub return null; }

@Override public T last() throws ListEmptyException { // TODO Auto-generated method stub return null; }

@Override public T removeFirst() throws ListEmptyException { // TODO Auto-generated method stub return null; }

@Override public T removeLast() throws ListEmptyException { // TODO Auto-generated method stub return null; }

@Override public T removeAtPosition(int location) throws IndexOutOfBoundsException { // TODO Auto-generated method stub return null; }

@Override abstract public void remove(T item);

@Override public void set(T item1, T item2) throws ListEmptyException { // TODO Auto-generated method stub }

@Override public T set(T item, int location) throws IndexOutOfBoundsException { // TODO Auto-generated method stub return null; }

@Override abstract public int search(T item);

@Override public boolean contains(T item) throws ListEmptyException { if(isEmpty()) throw new ListEmptyException(); return search(item) != -1; }

@Override public boolean isEmpty() { return size == 0; }

@Override public int getSize(){ return size; }

@Override public void clear() { size = 0; front.setNext(null); System.gc(); }

@Override public Iterator iterator() { return new ListIterator(front.next, size); }

public String toString(){ String str = "["; Iterator iter = iterator(); while(iter.hasNext()) str += iter.next() + (iter.hasNext() ? ", " : "]"); return str; } }

-----------------------------------

package list;

import java.util.Iterator;

public interface ListADT> {

/** * Adds an item to the list * * @param item * the item to be added */ void add(T item);

/** * Returns the first item in the list * * @return the first item * @throws ListEmptyException * if the list is empty */ T first() throws ListEmptyException;

/** * Returns the last item in the list * * @return the last item * @throws ListEmptyException * if the list is empty */ T last() throws ListEmptyException;

/** * Removes and returns the first item in the list * * @return the first item * @throws ListEmptyException * if the list is empty */ T removeFirst() throws ListEmptyException;

/** * Removes and returns the last item in the list * * @return the last item * @throws ListEmptyException * if the list is empty */ T removeLast() throws ListEmptyException;

/** * Removes and returns an item stored at a specified location in the list * * @param location * the location of the item to removed from the list * @return the item stored at that location * @throws IndexOutOfBoundsException * if location = size() */ T removeAtPosition(int location) throws IndexOutOfBoundsException;

/** * Removes first occurrence of an item from the list * * @param item * the item to removed from the list * @throws ListEmptyException * if the list is empty */ void remove(T item) throws ListEmptyException;

/** * Replaces all occurrences of the item stored in the list * * @param item1 * the old item * @param item2 * the new item * @throws ListEmptyException * if the list is empty */ void set(T item1, T item2) throws ListEmptyException;

/** * Replaces the item stored at the given location and returns the old item * * @param item * the new item * @param location * the location * @return the old item * @throws IndexOutOfBoundsException * if location = size() */ T set(T item, int location) throws IndexOutOfBoundsException;

/** * Searches for an item in the list and returns its location or -1 if the * item is not in the list * * @param item * the item to search for * @return the location of the item * @throws ListEmptyException * if the list is empty */ int search(T item) throws ListEmptyException;

/** * Searches for an item in the list and returns true if it can be found * * @param item * the item to search for * @return true if an item is in the list or false otherwise * @throws ListEmptyException * if the list is empty */ boolean contains(T item) throws ListEmptyException;

/** * Returns the number of items in the list * * @return the number of items in the list */

/** * Returns true if the list is empty, or false otherwise * * @return true if the list is empty, or false otherwise */ boolean isEmpty();

/** * Returns the number of items in the list * * @return the number of items in the list */ int getSize();

/** * Removes all of the items in this list. */ void clear(); /** * Returns an iterator over the items in this list in proper sequence. */ Iterator iterator(); }

-----------------------------------------------------

package list;

public class ListEmptyException extends Exception {

public ListEmptyException() { super("List Empty Exception..."); }

public ListEmptyException(String message) { super(message); } }

----------------------------------------------------

package list;

public class OrderedList> extends List {

@Override public void add(T item) { Node trav = front; while(trav.getNext() != null && trav.getNext().getData().compareTo(item) (item, trav.getNext())); size++; }

@Override public void remove(T item) { boolean found = false, okay = true; Node trav = front; while(trav.getNext() != null && !found && okay){ if(trav.getNext().getData().compareTo(item) == 0){ found = true; trav.setNext(trav.getNext().getNext()); size--; } else if(trav.getNext().getData().compareTo(item) > 0) okay = false; else{ trav = trav.getNext(); } } } @Override public int search(T item) { int pos = -1, index = 0; boolean found = false, okay = true; Node trav = front.getNext(); while(trav != null && !found && okay){ if(trav.getData().compareTo(item) == 0){ found = true; pos = index; } else if(trav.getData().compareTo(item) > 0) okay = false; else{ index++; trav = trav.getNext(); } } return pos; } }

------------------------------------------------------

package list;

public class UnorderedList> extends List {

@Override public void add(T item) { Node trav = front; while(trav.getNext() != null) trav = trav.getNext(); trav.setNext(new Node(item)); size++; }

@Override public void remove(T item) { boolean found = false; Node trav = front; while(trav.getNext() != null && !found){ if(trav.getNext().getData().compareTo(item) == 0){ found = true; trav.setNext(trav.getNext().getNext()); size--; } else{ trav = trav.getNext(); } } }

@Override public int search(T item) { int index = 0; boolean found = false; Node trav = front.getNext(); while(trav != null && !found){ if(trav.getData().compareTo(item) == 0){ found = true; } else{ index++; trav = trav.getNext(); } } return (found) ? index : -1; }

}

-------------------------------------------------------------

package sort;

public class Sort{ public static > void bubbleSort(T[] array){ for(int i=0; i 0){ T temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } public static > void selectionSort(T[] array){ for(int i=0; i> void insertionSort(T[] array){ for(int i=1; i=0; j--){ if(array[j].compareTo(temp) > 0){ array[pos--] = array[j]; } } array[pos] = temp; for(T n : array) System.out.print(n + " "); System.out.println(); } } }

Open the zip file containing the code for the Singly Linked List implementation of the list class. Add the code for the remaining methods, make modifications to the code as necessary to ensure that it is correct, and add appropriate documentation to each method. (Marked by TODO) Create a class, Lab7App, to test each method. You should add about 10 items to each of the lists, prior to testing each of the other methods. Open the zip file containing the code for the Singly Linked List implementation of the list class. Add the code for the remaining methods, make modifications to the code as necessary to ensure that it is correct, and add appropriate documentation to each method. (Marked by TODO) Create a class, Lab7App, to test each method. You should add about 10 items to each of the lists, prior to testing each of the other methods

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

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions