You are required, but not limited, to turn in the following source files: Assign
ID: 3809003 • Letter: Y
Question
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. Note that all elements in the linked list will be added in alphabetical order.
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.
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.
Files provided -
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) -
Assignment 10 +main(StringO): void printMenuo void LinkedList first Node +LinkedList() getFirst(): Object tremove First(): Object +addFirst object): void rtlistlteratoro: Listlterator +toString (0:String size():int +addElement(Object, int) void tfindSmallesto:String searchAndReplace(Object,Object):void tsearchAndRemove(Object):void +reversed FirstSome(int) void You will need to implement only the methods in red. LinkedListIterator position: Node -previous: Node +LinkedListlterator0 +has Next():boolean +next0: Object +add(Object): void +remove(): void +set (Object):void Node data: Object +next:Node Arizona State University CSE205, Assignment 10, Spring 2017 ListIterator +has Nexto: boolean +next(0:Object +add(Object): void +remove(): void +set objecto voidExplanation / Answer
package assgn10;
import java.util.NoSuchElementException;
public class LinkedList {
// nested 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;
private int size;
// Constructs an empty linked list.
public LinkedList() {
first = null;
size=0;
}
// 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.
size--;
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;
size++;
}
// Returns an iterator for iterating through this list.
public ListIterator listIterator() {
return new LinkedListIterator();
}
/*********************************************************/
public void addElement(String str1, int index) {
if(index<0 || (index>size-1 && size !=0)){
System.out.println(index+" : "+size);
throw new IndexOutOfBoundsException();
}
int pos = 0;
Node temp = first;
if(index==0){
addFirst(str1);
}
else{
Node newNode = new Node();
newNode.data = str1;
while(pos<=index){
if(pos==index-1){
newNode.next = temp.next;
temp.next = newNode;
}
pos++;
temp = temp.next;
}
}
size++;
}
public int size() {
// TODO Auto-generated method stub
return size;
}
public String findSmallest() {
if(size==0){
return null;
}
Node temp = first;
String smallString = "";
if(temp!=null){
smallString = (String) temp.data;
}
while(temp!=null){
String a = (String) temp.data;
if(a.compareToIgnoreCase(smallString)>0){
smallString = a;
}
temp = temp.next;
}
return smallString;
}
public void searchAndReplace(String first2, String second) {
if(size==0){
return;
}
Node temp = first;
while(temp!=null){
String a = (String) temp.data;
if(a.equalsIgnoreCase(first2)){
temp.data = second;
}
temp = temp.next;
}
}
public void searchAndRemove(String inputInfo) {
if(size==0){
return;
}
Node temp = first;
if(first!=null && first.data.equals(inputInfo)){
removeFirst();
return;
}
Node prev = first;
while(temp!=null){
String a = (String) temp.data;
if(a.equalsIgnoreCase(inputInfo)){
prev.next = temp.next;
temp.data = null;
}
prev = temp;
temp = temp.next;
}
size--;
}
public void reverseFirstSome(int howMany) {
if(size==0 || howMany ==0){
return;
}
Node current = first;
Node next = null;
Node prev = null;
int count = 0;
/*reverse first k nodes of the linked list */
while (first != null && count < howMany)
{
next = first.next;
first.next = prev;
prev = current;
current = next;
count++;
}
first = prev;
}
/*********************************************************/
// nested 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) // not 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) // never 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) // not 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
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.