Question
---------------------------------------------------------------------------- //assigment10.java import java.io.*; public class Assignment10 { public static void main(String[] args) { char input1; String inputInfo = new String(); int operation2; String line
----------------------------------------------------------------------------
//assigment10.java
import java.io.*;
public class Assignment10
{
public static void main(String[] args)
{
char input1;
String inputInfo = new String();
int operation2;
String line = new String();
//create a linked list to be used in this method.
LinkedList list1 = new LinkedList();
try {
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
do {
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if(line.length() == 1) { //check if a user entered only one character
switch(input1) {
case 'A': //Add String
System.out.print("Please enter a string to add: ");
String str1 = stdin.readLine().trim();
System.out.print("Please enter an index to add: ");
inputInfo = stdin.readLine().trim();
int addIndex = Integer.parseInt(inputInfo);
list1.addElement(addIndex, str1);
break;
case 'D': //Search for the Index of a String
System.out.print("Please enter a string to search: ");
inputInfo = stdin.readLine().trim();
operation2=list1.searchElement(inputInfo);
if(operation2 > -1)
System.out.print("string found at " + operation2 + " ");
else
System.out.print("string not found ");
break;
case 'E': //Search for String at an Index
System.out.print("Please enter an index to search: ");
inputInfo = stdin.readLine().trim();
int searchIndex = Integer.parseInt(inputInfo);
System.out.print("string at the given index is " + list1.getElement(searchIndex) + " ");
break;
case 'L': //List Strings
System.out.print(list1.toString());
break;
case 'O': // List Current Size
System.out.print("The current size is " + list1.size() + " ");
break;
case 'Q': break; //Quit
case 'R': //Remove Last Few
System.out.print("Please enter a number of elements to be removed from the end of the linked list: ");
inputInfo = stdin.readLine().trim();
int howMany = Integer.parseInt(inputInfo);
list1.removeLastFew(howMany);
break;
case 'S': //Set String
System.out.print("Please enter a string to set: ");
String str2 = stdin.readLine().trim();
System.out.print("Please enter an index to set: ");
inputInfo = stdin.readLine().trim();
int setIndex = Integer.parseInt(inputInfo);
list1.setElement(setIndex, str2);
break;
case 'T': //Reverse
list1.reverse();
System.out.print("list reversed ");
break;
case '?': printMenu(); break; //Display Menu
default: System.out.print("Unknown action "); break;
} // switch(input1)
}
else
System.out.print("Unknown action ");
} while (input1 != 'Q' || line.length() != 1); // do-while
}
catch (IOException exception)
{
System.out.print("IO Exception ");
} // try-catch(IOException)
} // main()
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice\t\tAction " +
"------\t\t------ " +
"A\t\tAdd String " +
"D\t\tSearch for Index " +
"E\t\tSearch for String " +
"L\t\tList Strings " +
"O\t\tList Current Size " +
"Q\t\tQuit " +
"R\t\tRemove Last Few " +
"S\t\tSet String " +
"T\t\tReverse " +
"?\t\tDisplay Help ");
} // printMenu()
} // class Assignment10
--------------------------------------------------------------------------------------------------------
//Linkedlist.java
import java.util.*;
public class LinkedList { /ested class to represent a node private class Node { public Object data; public Node next; }
//only instance variable that points to the first node. private Node first;
// Constructs an empty linked list. public LinkedList() { first = null; }
// Returns the first element in the linked list. public Object getFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else return first.data; }
// Removes the first element in the linked list. public Object removeFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { Object element = first.data; first = first.next; //change the reference since it's removed. return element; } }
// Adds an element to the front of the linked list. public void addFirst(Object element) { //create a new node Node newNode = new Node(); newNode.data = element; newNode.next = first; //change the first reference to the new node. first = newNode; }
// Returns an iterator for iterating through this list. public ListIterator listIterator() { return new LinkedListIterator(); }
/********************************************************* Add your methods here *********************************************************/
/ested class to define its iterator private class LinkedListIterator implements ListIterator { private Node position; //current position private Node previous; //it is used for remove() method
// Constructs an iterator that points to the front // of the linked list.
public LinkedListIterator() { position = null; previous = null; }
// Tests if there is an element after the iterator position. public boolean hasNext() { if (position == null) /ot traversed yet { if (first != null) return true; else return false; } else { if (position.next != null) return true; else return false; } }
// Moves the iterator past the next element, and returns // the traversed element's data. public Object next() { if (!hasNext()) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { previous = position; // Remember for remove
if (position == null) position = first; else position = position.next;
return position.data; } }
// Adds an element after the iterator position // and moves the iterator past the inserted element. public void add(Object element) { if (position == null) /ever traversed yet { addFirst(element); position = first; } else { //making a new node to add Node newNode = new Node(); newNode.data = element; newNode.next = position.next; //change the link to insert the new node position.next = newNode; //move the position forward to the new node position = newNode; } //this means that we cannot call remove() right after add() previous = position; }
// Removes the last traversed element. This method may // only be called after a call to the next() method. public void remove() { if (previous == position) /ot after next() is called { IllegalStateException ex = new IllegalStateException(); throw ex; } else { if (position == first) { removeFirst(); } else { previous.next = position.next; //removing } //stepping back //this also means that remove() cannot be called twice in a row. position = previous; } }
// Sets the last traversed element to a different value. public void set(Object element) { if (position == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else position.data = element; } } //end of LinkedListIterator class } //end of LinkedList class
----------------------------------------------------------------------------------------------------------------
//listlterator.java
public interface ListIterator { //Move Moves the iterator past the next element. Object next();
// Tests if there is an element after the iterator position. boolean hasNext();
// Adds an element before the iterator position // and moves the iterator past the inserted element. void add(Object element);
// Removes the last traversed element. This method may // only be called after a call to the next() method. void remove();
// Sets the last traversed element to a different value. void set(Object element); }
--------------------------------------------------------------------------------
input:
A Pink 0 L A Red 0 L C A Black 2 L A Yellow 0 L A White 1 L O L A Indigo 1 L A Blue 5 L A Green 0 L A Gray 0 L Q
Output:
Choice Action
------ ------
A Add String
D Search for Index
E Search for String
L List Strings
O List Current Size
Q Quit
R Remove Last Few
S Set String
T Reverse
? Display Help
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Pink }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Red Pink }
What action would you like to perform?
Unknown action
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Red Pink Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Yellow Red Pink Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Yellow White Red Pink Black }
What action would you like to perform?
The current size is 5
What action would you like to perform?
{ Yellow White Red Pink Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Yellow Indigo White Red Pink Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Yellow Indigo White Red Pink Blue Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Green Yellow Indigo White Red Pink Blue Black }
What action would you like to perform?
Please enter a string to add:
Please enter an index to add:
What action would you like to perform?
{ Gray Green Yellow Indigo White Red Pink Blue Black }
What action would you like to perform?
Assignment 10 ListIterator main(String[D void printMenu0void LinkedListIterator -position Node previous Node L+hasNext0:boolean LinkedListIterator0 +hasNextO.boolean tnext0 Object tadd(Object) void +removeO:void +set(Object) void +next0 Object +add(Object) void tremove0:void +set (Object).void LinkedList first Node +LinkedList0 tgetFirst0: Object tremoveFirst0: Object +addFirst (Object).void HistIterator: ListIterator +toString0:String +sizeO int +searchElement(Object)int +getElement (int) Object +setElement int, Object) void +addElement(int, Object) void +removeLastFew(int)void treverseO:void Node +data Object +- ext: NodeStep 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