Minimal Submitted Files You are required, but not limited, to turn in the follow
ID: 3686923 • Letter: M
Question
Minimal Submitted Files
You are required, but not limited, to turn in the following source files:
Assignment10.java (This file does not need to be modified)
LinkedList.java (It needs to be modified)
ListIterator.java (This file does not need to be modified)
Requirements to get full credits in Documentation
The assignment number, your name, StudentID, Lecture number, and a class description need to be included at the top of each file/class.
A description of each method is also needed.
Some additional comments inside of long methods (such as a "main" method) to explain codes that are hard to follow should be written.
You can look at the Java programs in the text book to see how comments are added to programs.
New Skills to be Applied
In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
Linked Lists
Program Description
Class Diagram:
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 Banana Melon Orange }
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 addElementAt(Object element, int index)
The addElementAt 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 void addFewAtEnd(Object element, int howMany)
The addFewAtEnd method adds the parameter element at the end of the linked list for the number of times specified by the parameter howMany. For instance, if the parameter integer is 3, then it should add the parameter Object three times at the end of the linked list. If the parameter integer is less than or equals to 0, nothing should be added to the linked list (so the linked list will be unchanged.)
5.
public void removeLastFew(int howMany)
The removeLastFew method removes the number of elements at the end of the linked list, which was specified by the parameter howMany. For instance, if the parameter integer is 3, then the last three elements of the linked list will be removed. If the parameter integer is 0 or less, then the linked list content will not be changed. If the parameter integer is same or more than the size of the linked list, then all elements will be removed and the linked list will be empty.
6.
public void removeAllOccurrences(Object stringToBeRemoved)
The removeAllOccurrences method removes all occurrences of the parameter string (object) in the list. If the parameter string does not exist in the linked list, then the linked list content will not change.
7.
public void reverseLastFew(int howMany)
The reverseLastFew method reverses the number of elements at the end of the linked list, which was specified by the parameter howMany. For instance, if the parameter integer is 3, then only the last three elements of the linked list will be reversed.
For instance, if the linked list contains { Apple Banana Melon Orange }, then after calling this method with the parameter 3, its content becomes { Apple Orange Melon Banana }. 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.
Test your LinkedList class with the given Assignment10.java file.
It is recommended to test boundary cases such as the cases when the linked list is empty, when it contains only one element, when adding/removing at the beginning or at the end of the linked list.
Assignment 10.java (dont change)
LinkedList.java (needs changed)
ListIterator.java (dont change)
Expected Input Case
Expected Output
Explanation / Answer
import java.io.*;
import java.lang.*;
import java.util.*;
public class Assignment10
{
public static void main(String[] args) throws Exception
{
char input1;
String inputInfo = new String();
int operation2;
String line = new String();
LinkedList list1 = new LinkedList();
try
{
printMenu();
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();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
switch (input1)
{
case 'A': System.out.print("Please enter a string to add: ");
String str1 = stdin.readLine().trim();
System.out.print("Please enter its index: ");
int index = Integer.parseInt(stdin.readLine().trim());
try
{
list1.addElementAt(str1, index);
}
catch(IndexOutOfBoundsException ex)
{
System.out.print("The index is out of bounds ");
}
break;
case 'B':
System.out.print("Please enter a string to add at the end: ");
String str2 = stdin.readLine().trim();
System.out.print("Please enter a number of times to add: ");
String times = stdin.readLine().trim();
int howMany = Integer.parseInt(times);
list1.addFewAtEnd(str2, howMany);
break;
case 'C':
int size = list1.size();
System.out.print("The size of the linked list is " + size + " ");
break;
case 'L':
System.out.print(list1.toString());
break;
case 'Q':
break;
case 'R':
System.out.print("Please enter a number of elements to remove from the end: ");
inputInfo = stdin.readLine().trim();
int howMany2 = Integer.parseInt(inputInfo);
list1.removeLastFew(howMany2);
break;
case 'S':
System.out.print("Please enter a string to remove from the linked list: ");
inputInfo = stdin.readLine().trim();
list1.removeAllOccurrences(inputInfo);
break;
case 'T':
System.out.print("Please enter a number of elements to reverse from the end: ");
inputInfo = stdin.readLine().trim();
int howMany3 = Integer.parseInt(inputInfo);
list1.reverseLastFew(howMany3);
break;
case '?':
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 ");
}
}
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add String " +
"B Add Strings at End " +
"C Count its Size " +
"L List Strings " +
"Q Quit " +
"R Remove Strings from End " +
"S Remove String from List " +
"T Reverse Strings from End " +
"? Display Help ");
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
public class LinkedList
{
private class Node
{
public Object data;
public Node next;
}
Node first;
public LinkedList()
{
first = null;
}
public Object getFirst()
{
if (first == null)
{
NoSuchElementException ex
= new NoSuchElementException();
throw ex;
}
else
return first.data;
}
public Object removeFirst()
{
if (first == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
Object element = first.data;
first = first.next;
return element;
}
}
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
}
public ListIterator listIterator()
{
return new LinkedListIterator();
}
public String toString() {
String s = { Apple Banana Melon Orange };
if(s ==NULL) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else {
String l = s.toString();
return l;
}
public int size(String s) {
if(s = = NULL) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else {
int size = s.length();
return size;
}
public void addElementAt(String str1, int index,String s) {
if ((index>list1.length) || (list1.length<index)) {
IndexOutOfBoundException e = new IndexOutOfBoundException();
throws e;
}
else {
s.add(str1,index);
newnode.data = str1;
newnode.next = index.next;
}
}
public void addFewAtEnd(String str2,String s, int howmany) {
if(howmany >list1.length) {
IndexOutOfBoundException e = new IndexOutOfBoundException();
throws e;
}
else {
for(int i=0;i<=howmany;i++) {
s.add(str2);
newnode.data = str2;
newnode.next = i.next;
}
}
public void removeLastFew(String s,int howmany) {
if(howmany >list1.length) {
IndexOutOfBoundException e = new IndexOutOfBoundException();
throws e;
}
else {
for(int i=0;i<=howmany;i++) {
s.remove();
newnode.next = i.previous;
}}
}
public void removeAllOccurences( String s,String inputinfo) {
if(s == NULL ) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else {
if(first == null) return true;
while(first.data== inputinfo)
first= first.data;
if(first == null) return true;
Node prev =first;
Node current = prev.next;
while(current != null){
if (current.data ==inputinfo)
prev.next = current.next;
prev = prev.next;
current = prev.next;
}
return true;
}
}
public void reverseLastFew(int howmany,String s) {
if(s == NULL ) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
first = prev;
return first;
}
}
private class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
public LinkedListIterator()
{
position = null;
previous = null;
}
public boolean hasNext()
{
if (position == null)
{
if (first != null)
return true;
else
return false;
}
else
{
if (position.next != null)
return true;
else
return false;
}
}
public Object next()
{
if (!hasNext())
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
previous = position;
if (position == null)
position = first;
else
position = position.next;
return position.data;
}
}
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;
}
public void remove()
{
if (previous == position) {
IllegalStateException ex = new IllegalStateException();
throw ex;
}
else
{
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next; }
position = previous;
}
}
public void set(Object element)
{
if (position == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
position.data = element;
}
} }
public interface ListIterator
{
Object next();
boolean hasNext();
void add(Object element);
void remove();
void set(Object element);
}