Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are required, but not limited, to turn in the following source files: Assign

ID: 3807646 • 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 void

Explanation / Answer

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 'A': //Add String System.out.print("Please enter a string to add: "); String str1 = stdin.readLine().trim(); list1.addElement(str1); break; case 'G': //Get a String at a certain index System.out.print("Please enter an index of a string to get: "); inputInfo = stdin.readLine().trim(); int getIndex = Integer.parseInt(inputInfo); try { String getString = (String) list1.getElement(getIndex); System.out.print("The string at the index " + getIndex + " is " + getString + " "); } catch(IndexOutOfBoundsException ex) { System.out.print("The index is not valid "); } break; case 'E': //Check if the linked list is empty boolean empty = list1.isEmpty(); if (empty) System.out.print("The linked list is empty "); else System.out.print("The linked list is not empty "); break; case 'I': //Get the last index of a given String System.out.print("Please enter a string to search: "); String str2 = stdin.readLine().trim(); int searchIndex = list1.indexOfLast(str2); if (searchIndex != -1) System.out.print("The index of the last " + str2 + " is " + searchIndex + " "); else System.out.print("The string " + str2 + " does not exist in the linked list "); break; case 'L': //List Strings System.out.print(list1.toString()); break; case 'Q': //Quit break; case 'R': //Remove a String System.out.print("Please enter an index of a string to remove: "); inputInfo = stdin.readLine().trim(); int removeIndex = Integer.parseInt(inputInfo); try { String removeString = (String) list1.removeElement(removeIndex); System.out.print("The string at the index " + removeIndex + " was " + removeString + " and was removed "); } catch(IndexOutOfBoundsException ex) { System.out.print("The index is not valid "); } break; case 'S': //Search and Replace System.out.print("Please enter a string to replace: "); String original = stdin.readLine().trim(); System.out.print("Please enter a string used to replace with: "); String newStr = stdin.readLine().trim(); list1.searchAndReplace(original, newStr); 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 " + "------ ------ " + "A Add String " + "G Get String " + "E Check if Empty " + "I Index of Last String " + "L List Strings " + "Q Quit " + "R Remove String " + "S Search and Replace " + "? Display Help "); } //end of printMenu() }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote