In the Assignment #10, you are given three files Assignment10.java, LinkedList.j
ID: 3812522 • Letter: I
Question
In the Assignment #10, you are given three files Assignment10.java, LinkedList.java, and 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.)
1.
public String toString()
The toString method should concatenate strings in the linked list, and return a string of the following format:
{ Apple Melon Orange Banana }
Thus it starts with "{" and ends with "}", and there is a space between strings and "{" or "}". If the list is empty, it returns "{ }" with a space in between.
2.
public int size()
The size method returns the number of strings that the linked list contains at the time when this method is called.
3.
public void addElement(Object element, int index)
The addElement adds the parameter element at the parameter specified index. The element at the index and any elements at the later indices will be shifted towards the end of the list. If the parameter index is larger or smaller than the existing indices, it should throw an object of the IndexOutOfBoundsException class.
4.
public String findSmallest( )
The findSmallest method should return the string that is smallest lexically among the strings stored in the linked list. It should return null (null pointer) if the linked list is empty.
5.
public void searchAndReplace(Object first, Object second)
The searchAndReplace method should search and replace all strings that match with the first parameter "first" with the second parameter "second". If the linked list does not contain a string that matches with the first parameter, then the linked list content should not change after calling this method.
6.
public void searchAndRemove(Object toBeRemoved)
The searchAndRemove method should search and remove all strings that match with the first parameter "toBeRemoved". If the linked list does not contain a string that matches with the first parameter, then the linked list content should not change after calling this method.
7.
public void reverseFirstSome(int howMany)
The reverseFirstSome method should reverse the number of strings located at the beginning, specified by the parameter integer. For instance, if the parameter "howMany" is 3, then the first 3 strings in the linked list should be reversed.
If the number "howMany" is 0 or less, then the linked list content will not change, and if it is same or more than the size of the linked list, then the entire linked list content will be reversed.
(This file does not need to be modified)
(This file does not need to be modified)
(It needs to be modified)
input 1:
A
Grape
0
L
A
Melon
0
L
C
A
Apple
2
L
A
Peach
0
L
A
Banana
1
L
C
D
L
A
Potato
-1
L
A
Kiwi
5
L
A
Avocado
10
L
A
Lemon
0
L
Q
input 2:
L
C
A
Banana
10
L
A
Apple
0
L
A
Potato
1
A
Grape
2
A
Melon
0
A
Kiwi
1
A
Lemon
2
A
Guava
3
A
Orange
2
A
Olive
3
A
Pear
4
L
T
3
L
T
5
L
T
-3
L
T
1000
L
Q
input 3:
C
R
Orange
Apple
L
S
Orange
L
A
Apple
6
L
A
Orange
0
A
Tomato
1
A
Apple
0
A
Potato
1
A
Orange
2
L
R
Orange
Kiwi
L
R
Kiwi
Lemon
L
R
Tomato
Potato
L
R
Potato
Watermelon
L
S
Apple
L
C
Q
input 4:
L
D
C
A
Tomato
0
A
Mango
0
A
Banana
2
A
Guava
2
L
C
D
A
Lime
2
L
R
Apple
Tomato
L
R
Lime
Guava
L
S
Guava
L
Q
output 1:
Choice Action
------ ------
A Add String
C Count its Size
D Find Smallest
L List Strings
Q Quit
R Replace Strings
S Remove Strings
T Reverse Strings from Beginning
? Display Help
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Grape }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Melon Grape }
What action would you like to perform?
The size of the linked list is 2
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Melon Grape Apple }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Peach Melon Grape Apple }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Peach Banana Melon Grape Apple }
What action would you like to perform?
The size of the linked list is 5
What action would you like to perform?
The smallest string is Apple
What action would you like to perform?
{ Peach Banana Melon Grape Apple }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
The index is out of bounds
What action would you like to perform?
{ Peach Banana Melon Grape Apple }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Peach Banana Melon Grape Apple Kiwi }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
The index is out of bounds
What action would you like to perform?
{ Peach Banana Melon Grape Apple Kiwi }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Lemon Peach Banana Melon Grape Apple Kiwi }
What action would you like to perform?
output 2:
Choice Action
------ ------
A Add String
C Count its Size
D Find Smallest
L List Strings
Q Quit
R Replace Strings
S Remove Strings
T Reverse Strings from Beginning
? Display Help
What action would you like to perform?
{ }
What action would you like to perform?
The size of the linked list is 0
What action would you like to perform?
Please enter a string to add:
Please enter its index:
The index is out of bounds
What action would you like to perform?
{ }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Apple }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Melon Kiwi Orange Olive Pear Lemon Guava Apple Potato Grape }
What action would you like to perform?
Please enter a number of elements to reverse from the beginning:
What action would you like to perform?
{ Orange Kiwi Melon Olive Pear Lemon Guava Apple Potato Grape }
What action would you like to perform?
Please enter a number of elements to reverse from the beginning:
What action would you like to perform?
{ Pear Olive Melon Kiwi Orange Lemon Guava Apple Potato Grape }
What action would you like to perform?
Please enter a number of elements to reverse from the beginning:
What action would you like to perform?
{ Pear Olive Melon Kiwi Orange Lemon Guava Apple Potato Grape }
What action would you like to perform?
Please enter a number of elements to reverse from the beginning:
What action would you like to perform?
{ Grape Potato Apple Guava Lemon Orange Kiwi Melon Olive Pear }
What action would you like to perform?
output 3:
Choice Action
------ ------
A Add String
C Count its Size
D Find Smallest
L List Strings
Q Quit
R Replace Strings
S Remove Strings
T Reverse Strings from Beginning
? Display Help
What action would you like to perform?
The size of the linked list is 0
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ }
What action would you like to perform?
Please enter a string to remove from the linked list:
What action would you like to perform?
{ }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
The index is out of bounds
What action would you like to perform?
{ }
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Apple Potato Orange Orange Tomato }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Apple Potato Kiwi Kiwi Tomato }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Apple Potato Lemon Lemon Tomato }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Apple Potato Lemon Lemon Potato }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Apple Watermelon Lemon Lemon Watermelon }
What action would you like to perform?
Please enter a string to remove from the linked list:
What action would you like to perform?
{ Watermelon Lemon Lemon Watermelon }
What action would you like to perform?
The size of the linked list is 4
What action would you like to perform?
output 4:
Choice Action
------ ------
A Add String
C Count its Size
D Find Smallest
L List Strings
Q Quit
R Replace Strings
S Remove Strings
T Reverse Strings from Beginning
? Display Help
What action would you like to perform?
{ }
What action would you like to perform?
There is no element in the linked list
What action would you like to perform?
The size of the linked list is 0
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Mango Tomato Guava Banana }
What action would you like to perform?
The size of the linked list is 4
What action would you like to perform?
The smallest string is Banana
What action would you like to perform?
Please enter a string to add:
Please enter its index:
What action would you like to perform?
{ Mango Tomato Lime Guava Banana }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Mango Tomato Lime Guava Banana }
What action would you like to perform?
Please enter a string to replace from the linked list:
Please enter a string to replace with:
What action would you like to perform?
{ Mango Tomato Guava Guava Banana }
What action would you like to perform?
Please enter a string to remove from the linked list:
What action would you like to perform?
{ Mango Tomato Banana }
What action would you like to perform?