Loading

[NEW SOLN] CSIS 312 ASSIGNMENT 8 SECURERANDOM

[SOLVED] CSIS 312 ASSIGNMENT 8 SECURERANDOM: Write a program that inserts 25 random numbers from 0 to 99 (using SecureRandom) inclusive into a linked-list object in sorted order and then calls the linked-list object’s print() method.
[SOLVED] CSIS 312 ASSIGNMENT 8 SECURERANDOM: The following files are provided for you and must be used for this exercise:
·      SortedList.java
o  Modify the insertSorted() method to create a sorted list. Feel free to use any ListNode or SortedList method. If you add any new methods they must be called either directly or indirectly from insertSorted().
o  Do not modify or change any other aspect of SortedList.java.
o  ListTest.java should not be changed except to enter your name in the place designated for it.
·      EmptyListException.java which is used by SortedList and ListTest and should not be changed or modified at all.
[SOLVED] CSIS 312 ASSIGNMENT 8 SECURERANDOM: Note: If you do not use your modified insertSorted() method to sort your list as you build it, you will not get any credit for your work. Also, if you use Collections.sort() or Arrays.sort() or any pre-written sorting function to sort your list you will not get any credit for the assignment.
 
[SOLVED] CSIS 312 ASSIGNMENT 8 SECURERANDOM: Make sure that your screen shot(s) show your program running and that your runtime display shows that your program does all that is required of it. You only get credit for what you demonstrate.

// Fig. 21.4: EmptyListException.java

// Class EmptyListException declaration.

publicclassEmptyListExceptionextendsRuntimeException

{

  // constructor

  publicEmptyListException()

  {

     this("List");// call other EmptyListException constructor

  }

  // constructor

  publicEmptyListException(String name)

  {

     super(name + " isempty");// call superclass constructor

  }

}// end class EmptyListException

// Based on Fig. 21.5: ListTest.java

// ListTest class to demonstrate SortedList capabilities.

import java.security.SecureRandom;

publicclassListTest

{

  publicstaticvoid main(String[] args)

  {

     System.out.println("<Your name goes here -- Lab #8n");

     SortedList<Integer> list =newSortedList<>();

     SecureRandom rNum =newSecureRandom();

     // insert 25 random (between 0 and 99 inclusive) integers into the list

     for(int i = 0; i < 25; i++)

           //Your job is to modify insertSorted so that it creates a

           //sorted list one element at a time.

         list.insertSorted(rNum.nextInt(100));

     list.print();

}// end class ListTest

// Based on Fig. 21.3: List.java

// ListNode and SortedList class declarations.

// class to represent one node in a list

classListNode<TextendsComparable<T>>

{

  // package access members; SortedList can access these directly

  T data;// data for this node

  ListNode<T> nextNode;// reference to the next node in the list

  // constructor creates a ListNode that refers to object

  ListNode(T object)

  {

     this(object, null);

  }

  // constructor creates ListNode that refers to the specified

  // object and to the next ListNode

  ListNode(T object, ListNode<T> node)

  {

     data = object;  

     nextNode = node;

  }

  // return reference to data in node

  T getData()

  {

     returndata;

  }

  // return reference to next node in list

  ListNode<T> getNext()

  {

     returnnextNode;

  }

}// end class ListNode<T>

// class SortedList definition

publicclassSortedList<TextendsComparable<T>>

{

  privateListNode<T> firstNode;

  privateListNode<T> lastNode;

  privateString name;// string like "list" used in printing

  // constructor creates empty SortedList with "list" as the name

  publicSortedList()

  {

     this("list");

  }

  // constructor creates an empty SortedList with a name

  publicSortedList(String listName)

  {

     name = listName;

     firstNode = lastNode = null;

  }

  // insert "insertItem" into the proper position within the sorted list

  publicvoid insertSorted(T insertItem)

  {

      // Complete insertSorted() so that insertItem is added to the list

      // in sorted order. Use the compareTo() method (part of the interface

      // Comparable) to do lesser/greater than comparisons of type T objects.

      // See Section 20.4 and Fig 21.15 for more information on compareTo and

      // Examples of its use.

      // Use insertAtFront(), insertAtBack(), and insert() as necessary to

      // place insertItem in its proper place in the list.

      // Do not change any of the class signatures nor any of the methods in

      // this file.

      // You may delete these comments, but be sure you follow the guidance

      // provided.

      // If you use Collections.sort() or Arrays.sort() or any similar pre-written

      // sorting function, you will not get any credit for your work.

  }

  privatevoid insert(T insertItem, ListNode<T> previousNode)

  {

      previousNode.nextNode =newListNode(insertItem, previousNode.nextNode);

  }

  // insert item at front of SortedList

  privatevoid insertAtFront(T insertItem)

  {

     if(isEmpty())// firstNode and lastNode refer to same object

        firstNode = lastNode =newListNode<T>(insertItem);

     else// firstNode refers to new node

        firstNode =newListNode<T>(insertItem, firstNode);

  }

  // insert item at end of SortedList

  privatevoid insertAtBack(T insertItem)

  {

     if(isEmpty())// firstNode and lastNode refer to same object

        firstNode = lastNode =newListNode<T>(insertItem);

     else// lastNode's nextNode refers to new node

        lastNode = lastNode.nextNode =newListNode<T>(insertItem);

  }

  // remove first node from SortedList

  publicT removeFromFront() throws EmptyListException

  {

     if(isEmpty())// throw exception if SortedList is empty

        thrownewEmptyListException(name);

     T removedItem = firstNode.data;// retrieve data being removed

     // update references firstNode and lastNode

     if(firstNode == lastNode)

        firstNode = lastNode = null;

     else

        firstNode = firstNode.nextNode;

     returnremovedItem;// return removed node data

  }// end method removeFromFront

  // remove last node from SortedList

  publicT removeFromBack() throws EmptyListException

  {

     if(isEmpty())// throw exception if SortedList is empty

        thrownewEmptyListException(name);

     T removedItem = lastNode.data;// retrieve data being removed

     // update references firstNode and lastNode

     if(firstNode == lastNode)

        firstNode = lastNode = null;

     else// locate new last node

     {

        ListNode<T> current = firstNode;

        // loop while current node does not refer to lastNode

        while(current.nextNode != lastNode)

           current = current.nextNode;

        lastNode = current;// current is new lastNode

        current.nextNode = null;

     }

     returnremovedItem;// return removed node data

  }

  // determine whether list is empty

  publicboolean isEmpty()

  {

     returnfirstNode == null;// return true if list is empty

  }

  // output list contents

  publicvoidprint()

  {

     if(isEmpty())

     {

        System.out.printf("Empty%s%n", name);

        return;

     }

     System.out.printf("The %s is: ", name);

     ListNode<T> current = firstNode;

     // while not at end of list, output current node's data

     while(current != null)

     {

        System.out.printf("%s ", current.data);

        current = current.nextNode;

     }

     System.out.println();

  }

}

<!-- /wp:shortcode -->

<!-- wp:paragraph -->

<p>

Support