// need help with following program. Assignment 10 Class should be good LinkedLi
ID: 3762409 • Letter: #
Question
// need help with following program.
Assignment 10 Class should be good
LinkedList Class is the only one that should be modified. Please See below
// Description: The Assignment 10 class displays a menu of choices to a user
// and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is
// entered.
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
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
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 'O': // List Current Size
System.out.print("The current size is " + list1.size() + " ");
break;
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.insertElement(addIndex, str1);
System.out.print(str1 + " is inserted at index " + addIndex + " ");
break;
case 'I': //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(inputInfo + " found at index " + operation2 + " ");
else
System.out.print(inputInfo + " 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 index " + searchIndex + " is " + list1.getElement(searchIndex) + " ");
break;
case 'S': //Set a new element at specified index
System.out.print("Please enter a new 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);
System.out.print(str2 + " is set at index " + setIndex + " ");
break;
case 'R': //Remove an element at a specified index
System.out.print("Please enter an index to remove: ");
inputInfo = stdin.readLine().trim();
int removeIndex = Integer.parseInt(inputInfo);
list1.removeElement(removeIndex);
System.out.print("string at index " + removeIndex + " is removed ");
break;
case 'C': //Count the number of occurences of a specific element
System.out.print("Please enter a string to count: ");
inputInfo = stdin.readLine().trim();
int counter1 =list1.countHowMany(inputInfo);
System.out.print("There are " + counter1 + " " + inputInfo + " found inside the linked list ");
break;
case 'D': //Remove all occurences of a given element
System.out.print("Please enter a string to remove: ");
inputInfo = stdin.readLine().trim();
list1.removeDuplicate(inputInfo);
System.out.print(inputInfo + " is removed from the linked list ");
break;
case 'P': //Append a given element a number of times at the end of the linked list
System.out.print("Please enter a string to append at the end: ");
String str3 = stdin.readLine().trim();
System.out.print("Please enter the number of times you want to append: ");
inputInfo = stdin.readLine().trim();
int times = Integer.parseInt(inputInfo);
list1.appendAtEnd(str3, times);
System.out.print(str3 + " is appended " + times + " times at end of the linked list ");
break;
case 'T': //Append a given element after the first occurence of another element
System.out.print("Which string do you want to append after: ");
String str4 = stdin.readLine().trim();
System.out.print("Please enter the string you want to append: ");
inputInfo = stdin.readLine().trim();
list1.appendAfter(str4, inputInfo);
System.out.print(inputInfo + " is appended after " + str4 + " ");
break;
case 'V': //Reverse the first few element of the linked list
System.out.print("Please enter the number of elements you want to reverse: ");
inputInfo = stdin.readLine().trim();
int reverseNum = Integer.parseInt(inputInfo);
list1.reverseFirstFew(reverseNum);
System.out.print("The first " + reverseNum + " elements are reversed ");
break;
case 'L': //List all strings inside the linked list
System.out.print(list1.toString());
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
catch (IOException exception)
{
System.out.print("IO Exception ");
}
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"O List Current Size " +
"I Search Element " +
"E Get Element by Index " +
"S Set Element by Index " +
"A Insert Element by Index " +
"R Remove Element by Index " +
"C Count How Many " +
"D Remove Duplicates " +
"P Append at the End " +
"T Append After " +
"V Reverse First Few " +
"L List Linked List " +
"Q Quit " +
"? Display Help ");
} //end of printMenu()
}
// Description: The LinkedList defines a linked list using its node class
// object and also defines a iterator class to traverse the linked list.
//***********************************************************************
import java.util.NoSuchElementException;
public class LinkedList
{
//only instance variable that points to the first node
private Node first;
//nested class to represent a node
private class Node
{
public Object data;
public Node next;
8 }
// Constructs an empty linked list.
public LinkedList()
{
first = null;
}
//Returns the first element in the linked list.
public Object getFirst()
{
if (first == null)
throw new NoSuchElementException();
return first.data;
}
//Removes the first element in the linked list.
public Object removeFirst()
{
if (first == null)
throw new NoSuchElementException();
Object element = first.data;
first = first.next;
return element;
}
//Adds an element to the front of the linked list.
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
}
//toString() method prints the elements out from front to tail
public String toString()
{
ListIterator iterator = listIterator();
String result = "{ ";
while (iterator.hasNext())
{
result += iterator.next() + " ";
}
result += "} ";
return result;
}
//*************** Below is where you should add the new methods ***********
//*****************************************************************
//size() method returns the number of nodes inside this LinkedList
//*****************************************************************
public int size()
{
}
//***********************************************************************************
//searchElement() method returns the index of the first occurrence of the
//parameter object in the linked list if it exists. It returns -1 if it does not exit.
//***********************************************************************************
public int searchElement(Object element)
{
}
//*****************************************************************
// getElement() method returns the element at the parameter index
// If the index is out of bounds, throw an IndexOutOfBoundException.
//*****************************************************************
public Object getElement(int index)
{
}
//****************************************************************************
//setElement() method sets the parameter object at the parameter index in the
//linked list. If the index is out of bounds, throws an IndexOutOfBoundException
//****************************************************************************
public void setElement(int index, Object element)
{
}
//***********************************************************************************
//insertElement() method inserts the parameter object at the parameter index.
//If the index is out of bounds, throws an IndexOutOfBoundException
//Note: the element can be inserted at the end, i.e. inserted at size() index/position
//************************************************************************************
public void insertElement(int index, Object element)
{
}
//*******************************************************************
//removeElement()method removes and returns element at parameter index
//and throw an IndexOutOfBoundException if the index is out of bounds
//*******************************************************************
public Object removeElement(int index)
{
}
//*****************************************************************
//countHowMany(Object) method returns the number of occurences of
//the parameter object in the LinkedList
//*****************************************************************
public int countHowMany(Object searchedObject)
{
}
//*****************************************************************
//removeDuplicate() method removes all occurences of the parameter
//objects from the LinkedList
//*****************************************************************
public void removeDuplicate(Object removedObject)
{
}
//*******************************************************************************
//appendAtEnd(Object, int) method appends the parameter object number of
//times at the end of the linked list. For example, a call of appendAtEnd("A", 3)
//will append string "A" three times at the end of the linked list.
//*******************************************************************************
public void appendAtEnd(Object element, int howManyTimes)
{
}
//********************************************************************************
//appendAfter(Object element1, Object element2) method appends the second parameter
//object, i.e. element2 right after the first occurence of first parameter object,
//i.e. element1. If element1 is not inside the linked list, then append element2
//at the front/head of the linked list.
//********************************************************************************
public void appendAfter(Object element1, Object element2)
{
}
//**************************************************************************************
//reverseFistFew(int howMany) reverse the first parameter number of elements inside the
//linked list. For example, if the original linked list is { A B C D E }, a call of
//reverseFirstFew(3) will change the linked list to { C B A D E }. Note: (1)you need to
//consider the boundary value, i.e.cases where howMany <= 0 or howMany > size()
//(2)list.reverseFirstFew(list.size()) should reverse the whole linked list
//**************************************************************************************
public void reverseFirstFew(int howMany)
{
}
//***************************************************************
//Method creates an iterator on the current LinkedList
//***************************************************************
public ListIterator listIterator()
{
return new LinkedListIterator();
}
//***************************************************************
//nested class to define its iterator
//***************************************************************
private class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
// Constructs an iterator that points to the front
// of the linked list. of the linked list.
public LinkedListIterator()
{
position = null;
previous = null;
}
// Moves the iterator past the next element, and returns
// the traversed element's data.
public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position; // Remember for remove
if (position == null)
position = first;
else
position = position.next;
return position.data;
}
// Tests if there is an element after the iterator position position
public boolean hasNext()
{
if (position == null)
return first != null;
else
return position.next != null;
}
// Adds an element after the iterator position
// and moves the iterator to the inserted element.
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = first;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
}
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)
throw new IllegalStateException();
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next;
}
position = previous;
}
// Sets the last traversed element to a different value
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
} //end of
}
In the Assignment, you are given three files Assignment10.java, LinkedList.java, ListIterator.java. You will need to add additional methods in the LinkedList class in the LinkedList.java file. The LinkedList will be tested using strings only.
Specifically, the following methods must be implemented in the LinkedList class:
(You should utilize listIterator() method already defined in the LinkedList class to obtain its LinkedListIterator object, and use the methods in the LinkedListIterator class to traverse from the first element to the last element of the linked list to define the following methods.)
public int size( )
The size method returns the number of strings that the linked list contains at the time when this method is called.
public int searchElement(Object element)
The searchElement method checks at which index the parameter element (string) is located, and returns its index. If the linked list does not contain the parameter element, then it returns -1.
public Object getElement(int index)
The getElement method returns an element at the given index passed as its parameter. If the parameter index is larger or smaller than the existing indices, it should throw an IndexOutOfBoundsException.
public void setElement (int index, Object element)
The setElement method set/replace the original element at parameter index with the parameter element, all other elements remain unchanged. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.
public Object insertElement(int index, Object element)
The insertElement method inserts the parameter object at the parameter index. If the index is out of bounds, throws an IndexOutOfBoundException. Note: the element can be inserted at the end of the linked list.
public Object removeElement(int index)
The removeElement method removes and returns element at parameter index and throw an IndexOutOfBoundException if the index is out of bound.
public int countHowMany(Object searchedObject)
The countHowMany method returns the number of occurences of the parameter object in the LinkedList. It returns 0 if the parameter object does not exist in the linked list.
public void removeDuplicate(Object removedObject)
The removeDuplicate method removes all occurences of the parameter object from the LinkedList.
public void appendAtEnd(Object element, int howManyTimes)
The appendAtEnd method appends the parameter object the parameter number of times at the end of the linked list. For example, a call of list1.appendAtEnd("Dog", 3) will append string "Dog" three times at the end of the linked list.
public void appendAfter(Object element1, Object element2)
The appendAfter method appends the second parameter object, i.e. element2 right after the first occurence of first parameter object, i.e. element1. If element1 is not inside the linked list, then element2 will be appended at the front/head of the linked list.
public void reverseFirstFew(int howMany)
The reverseFistFew method reverses the first parameter number of elements inside the linked list. For example, if the original linked list is { A B C D E F G }, a call of list.reverseFirstFew(3) will change the linked list to { C B A D E F G }. Note: (1) You need to consider the boundary value, i.e.cases where howMany <= 0 or howMany > list.size( ). (2): list.reverseFirstFew(list.size( ) ) should be able to reverse the whole linked list
Explanation / Answer
import java.util.NoSuchElementException;
public class LinkedList
{
//only instance variable that points to the first node
private Node first;
//nested class to represent a node
private class Node
{
public Object data;
public Node next;
8 }
// Constructs an empty linked list.
public LinkedList()
{
first = null;
}
//Returns the first element in the linked list.
public Object getFirst()
{
if (first == null)
throw new NoSuchElementException();
return first.data;
}
//Removes the first element in the linked list.
public Object removeFirst()
{
if (first == null)
throw new NoSuchElementException();
Object element = first.data;
first = first.next;
return element;
}
//Adds an element to the front of the linked list.
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
}
//toString() method prints the elements out from front to tail
public String toString()
{
ListIterator iterator = listIterator();
String result = "{ ";
while (iterator.hasNext())
{
result += iterator.next() + " ";
}
result += "} ";
return result;
}
//*************** Below is where you should add the new methods ***********
//*****************************************************************
//size() method returns the number of nodes inside this LinkedList
//*****************************************************************
public int size()
{
int size = 0;
for(Node n = first; n.next != null; n = n.next)
size++;
return size;
}
//***********************************************************************************
//searchElement() method returns the index of the first occurrence of the
//parameter object in the linked list if it exists. It returns -1 if it does not exit.
//***********************************************************************************
public int searchElement(Object element)
{
int index=0;
for(Node n = first; n.next != null; n = n.next)
if(n.data == element.data){
break;
}
index++;
}
return index;
}
//*****************************************************************
// getElement() method returns the element at the parameter index
// If the index is out of bounds, throw an IndexOutOfBoundException.
//*****************************************************************
public Object getElement(int index)
{
int temp=0;
for(Node n = first; n.next != null; n = n.next)
if(temp == index){
return n;
}
temp++;
}
return first;
}
//****************************************************************************
//setElement() method sets the parameter object at the parameter index in the
//linked list. If the index is out of bounds, throws an IndexOutOfBoundException
//****************************************************************************
public void setElement(int index, Object element)
{
int temp=0;
for(Node n = first; n.next != null; n = n.next)
if(temp == index){
n=element;
}
temp++;
}
}
//***********************************************************************************
//insertElement() method inserts the parameter object at the parameter index.
//If the index is out of bounds, throws an IndexOutOfBoundException
//Note: the element can be inserted at the end, i.e. inserted at size() index/position
//************************************************************************************
public void insertElement(int index, Object element)
{
int temp=0;
for(Node n = first; n.next != null; n = n.next)
if(temp == index){ //inserting new object at required position
Node tempElement = n.next;
n.next = element;
element.next = tempElement;
}
temp++;
}
}
//*******************************************************************
//removeElement()method removes and returns element at parameter index
//and throw an IndexOutOfBoundException if the index is out of bounds
//*******************************************************************
public Object removeElement(int index)
{
int temp=0;
for(Node n = first; n.next != null; n = n.next)
if(temp == index){ //inserting new object at required position
Node tempElement = n.next;
n.next = tempElement.next;
return tempElement;
}
temp++;
}
return first;
}
//*****************************************************************
//countHowMany(Object) method returns the number of occurences of
//the parameter object in the LinkedList
//*****************************************************************
public int countHowMany(Object searchedObject)
{
int count=0;
for(Node n = first; n.next != null; n = n.next)
if(n.data == element.data){
count++;
}
}
return count;
}
//*******************************************************************************
//appendAtEnd(Object, int) method appends the parameter object number of
//times at the end of the linked list. For example, a call of appendAtEnd("A", 3)
//will append string "A" three times at the end of the linked list.
//*******************************************************************************
public void appendAtEnd(Object element, int howManyTimes)
{
for(Node n = first; n.next != null; n = n.next)
if(n.next == null){
for(int i=0;i<howManyTimes;i++){
n.next = element;
n = n.next;
}
}
}
}
//***************************************************************
//Method creates an iterator on the current LinkedList
//***************************************************************
public ListIterator listIterator()
{
return new LinkedListIterator();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.