SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 209 Weeks Ago, 3 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 05 Jan 2018 My Price 10.00

//create a linked list to be used in this method.

please do it correctly thanks ! :) 

please do it correctly thanks ! :)

please do it correctly thanks ! :)

please do it correctly thanks ! :)

  • // Assignment #: 10
    //         Name: Your name
    //    StudentID: Your id
    //  Lab Lecture: Your lecture
    //  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();


           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?\n");
               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:\n");
                     String str1 = stdin.readLine().trim();
                     System.out.print("Please enter its index:\n");
                     int index = Integer.parseInt(stdin.readLine().trim());
                     try
                      {
                        list1.addElement(str1, index);
                      }
                     catch(IndexOutOfBoundsException ex)
                      {
                          System.out.print("The index is out of bounds\n");
                      }
                     break;
                   case 'C':   //Count the size of the linked list
                     int size = list1.size();
                     System.out.print("The size of the linked list is " + size + "\n");
                     break;
                   case 'D':   //Find the smallest
                     String smallest = list1.findSmallest();
                     if (smallest != null)
                         System.out.print("The smallest string is " + smallest + "\n");
                     else
                         System.out.print("There is no element in the linked list\n");
                     break;
                   case 'L':   //List Strings
                     System.out.print(list1.toString());
                     break;
                   case 'Q':   //Quit
                     break;
                   case 'R':  //Replace Strings
                     System.out.print("Please enter a string to replace from the linked list:\n");
                     String first = stdin.readLine().trim();
                     System.out.print("Please enter a string to replace with:\n");
                     String second = stdin.readLine().trim();
                     list1.searchAndReplace(first, second);
                     break;
                   case 'S':   //Remove String from List
                     System.out.print("Please enter a string to remove from the linked list:\n");
                     inputInfo = stdin.readLine().trim();
                     list1.searchAndRemove(inputInfo);
                     break;
                   case 'T':   //Reverse Strings from Beginning
                     System.out.print("Please enter a number of elements to reverse from the beginning:\n");
                     inputInfo = stdin.readLine().trim();
                     int howMany = Integer.parseInt(inputInfo);
                     list1.reverseFirstSome(howMany);
                     break;
                   case '?':   //Display Menu
                     printMenu();
                     break;
                   default:
                     System.out.print("Unknown action\n");
                     break;
                  }
               }
              else
               {
                 System.out.print("Unknown action\n");
                }
              } while (input1 != 'Q' || line.length() != 1);
            }
           catch (IOException exception)
            {
              System.out.print("IO Exception\n");
            }
        }

        /** The method printMenu displays the menu to a user **/
        public static void printMenu()
         {
           System.out.print("Choice\t\tAction\n" +
                            "------\t\t------\n" +
                            "A\t\tAdd String\n" +
                            "C\t\tCount its Size\n" +
                            "D\t\tFind Smallest\n" +
                            "L\t\tList Strings\n" +
                            "Q\t\tQuit\n" +
                            "R\t\tReplace Strings\n" +
                            "S\t\tRemove Strings\n" +
                            "T\t\tReverse Strings from Beginning\n" +
                            "?\t\tDisplay Help\n\n");
        } //end of printMenu()
    }
    // A linked list is a sequence of nodes with efficient
    // element insertion and removal.
    // This class contains a subset of the methods of the
    // standard java.util.LinkedList class.

    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;

       // Constructs an empty linked list.
       public LinkedList()
       {
          first = null;
       }


       // 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.
             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;
       }

       // Returns an iterator for iterating through this list.
       public ListIterator listIterator()
       {
          return new LinkedListIterator();
       }


       /*********************************************************
       Add your methods here
       *********************************************************/


       //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
    } //end of LinkedList class

    // The ListIterator interface allows access of a position in a linked list.
    // This interface contains a subset of the methods of the
    //  standard java.util.ListIterator interface. The methods for
    //  backward traversal are not included.

    public interface ListIterator
    {
       //Move Moves the iterator past the next element.
       Object next();

       // Tests if there is an element after the iterator position.
       boolean hasNext();

       // Adds an element before the iterator position
       // and moves the iterator past the inserted element.
       void add(Object element);


       // Removes the last traversed element. This method may
       // only be called after a call to the next() method.
       void remove();

       // Sets the last traversed element to a different value.
       void set(Object element);
    }

Attachments:

Answers

(5)
Status NEW Posted 05 Jan 2018 10:01 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)