Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Defining a linkedlist of dynamically allocated nodes with mutilple java files. Help creating ListNode java code and the LinkedList code for this java program. The

Defining a linkedlist of dynamically allocated nodes with mutilple java files. Help creating ListNode java code and the LinkedList code for this java program. The other two files are provided and the code listed at the bottom.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed\

Code for EmptyListException.java:

// Filename: EmptyListException.java // Defines the Exception - EmptyListException that can be thrown // when an operation is requested that is not supported for an empty list // // This class will be stored in the ExceptionPkg package.

package ExceptionPkg; public class EmptyListException extends RuntimeException { // constructor with no parameter public EmptyListException () { this ("List"); } // constructor with .String. parameter public EmptyListException (String name) { // call super class constructor super (name + " is empty"); } }

------------------------------------------------------------

Code for LinkedListTest.java:

// Filename: LinkedListTest.java

// // This file contains a main method to create an object of type LinkedList and // test all the methods defined for the LinkedList class.

// Import the LinkedList and EmptyListException classes from their appropriate packages import ListPkg.LinkedList; import ExceptionPkg.EmptyListException;

import java.util.Scanner;

public class LinkedListTest { public static void main(String[] args) {

// Create the linked list object 'myList' to hold Integer objects and print the list LinkedList myList = new LinkedList("Integer List");

System.out.println(); myList.print(); System.out.println();

// Add five items to the front of the list and print the list System.out.println("Adding five Integers to the front of the list"); for(int x = 0; x

// Add five items to the back of the list and print the list System.out.println("Adding five Integers to the back of the list"); for(int x = 0; x

// remove an item from the front and from the back of the list // print the list System.out.println("Removing an item from the front of the list"); try { myList.removeFromFront(); } catch(EmptyListException ELE) { System.out.print(ELE.getMessage()); } printLengthAndList(myList); System.out.println("Removing an item from the back of the list"); try { myList.removeFromBack(); } catch(EmptyListException ELE) { System.out.print(ELE.getMessage()); } printLengthAndList(myList);

// Try to remove the Integer value 4 from the list // catch the EmptyListException if thrown // display the list System.out.println ("Attempt to remove a value not in the list: 4"); try { if(myList.findAndRemove(4)) { System.out.println("4 removed"); } else { System.out.println("4 not removed"); } } catch(EmptyListException ELE) { System.out.println(ELE.getMessage()); } printLengthAndList(myList);

// Try to remove the Integer value 0 from the list // catch the EmptyListException if thrown // display the list System.out.println ("Attempt to remove a value in the list: "); try { if(myList.findAndRemove(0)) { System.out.println("0 removed"); } else { System.out.println("0 not removed"); } } catch(EmptyListException ELE) { System.out.println(ELE.getMessage()); } printLengthAndList(myList);

// Try to find position of an item not in the list System.out.println("Attempt to find the position of an item not in the list - 5"); int position = myList.findItemPos(5); if(position!=-1) { System.out.printf("Value of 5 found at %d ", position); } else { System.out.println("Value of 5 not found"); }

// Try to find position of an item in the list System.out.println(" Attempt to find the position of an item in the list - 0"); position = myList.findItemPos(0); if(position!=-1) { System.out.printf("Value of 0 found at position %d in the list ", position); } else { System.out.println("Value of 0 not found"); } // Remove the item at 'position' in the list System.out.printf(" Value at position %d being removed ", position); myList.removeItemAt(position); printLengthAndList(myList);

// Remove the first element in the list - item at position 0 System.out.println("Removing element at position 0"); myList.removeItemAt(0); printLengthAndList(myList);

// Remove the first element in the list - item at the front System.out.println("Removing from front"); myList.removeFromFront(); printLengthAndList(myList);

// get the object at position 2 in the list System.out.println("Getting value at position 2"); Integer temp = myList.getItemAt (2); System.out.printf("Value at position is %d ", temp);

// Test the 'getItemAt' method for the IndexOutOfBoundsException System.out.println(" Begin Testing IndexOutOfBoundsException"); try { myList.getItemAt(4); } catch(IndexOutOfBoundsException IOBE) { System.out.println("\tgetItemAt (index too large): " + IOBE); } try { myList.getItemAt(-1); } catch(IndexOutOfBoundsException IOBE) { System.out.println("\tgetItemAt (index too small): " + IOBE); }

// Test the 'remove' method for the IndexOutOfBoundsException try { myList.removeItemAt(4); } catch(IndexOutOfBoundsException IOBE) { System.out.println("\tremoveItemAt (index too large): " + IOBE); }

try { myList.removeItemAt(-1); } catch(IndexOutOfBoundsException IOBE) { System.out.println("\tremoveItemAt (index too small): " + IOBE); }

System.out.println("End of IndexOutOfBoundsException test ");

// Clear list System.out.println("If list not empty - Clear the list"); if(!myList.isEmpty()) { myList.clear(); } printLengthAndList(myList);

System.out.println(" Begin Testing EmptyListException");

// test removeFromFront, removeFromBack, findAndRemove, removeItemAt and getItemAt with // the empty list try { myList.removeFromFront(); } catch(EmptyListException ELE) { System.out.println("\tremoveFromFront: " + ELE.getMessage()); }

try { myList.removeFromBack(); } catch(EmptyListException ELE) { System.out.println("\tremoveFromBack: " + ELE.getMessage()); }

try { myList.findAndRemove(0); } catch(EmptyListException ELE) { System.out.println("\tfindAndRemove: " + ELE.getMessage()); }

try { myList.removeItemAt(0); } catch(EmptyListException ELE) { System.out.println("\tremoveItemAt: " + ELE.getMessage()); }

try { myList.getItemAt(0); } catch(EmptyListException ELE) { System.out.println("\tgetItemAt: " + ELE.getMessage()); }

System.out.println("End of EmptyListException test "); }

// Method: printLengthAndList (LinkedList list) // Method to print both the length of the list and the // contents of the list to the screen public static void printLengthAndList(LinkedList list) { System.out.printf("Length is: %d ", list.lengthIs()); list.print(); } }

Objectives: This programming exercise will provide experience defining a linked list of dynamically allocated nodes, multiple java files, implementation of a data structure to support Generic objects using the Integer type wrapper class and implementing programmer defined exceptions. Program Description: The main method defined in LinkedListTest.java tests the creation of your LinkedList object and tests all of the methods defined for your LinkedList class. The following four classes, defined in four separate Java files are required ListNode -Node of a Dynamic Data Structure LinkedList Class that implements the LinkedList Dynamic Data Structure Empty ListException Holds the user defined Exception provided LinkedList Test Tests all the methods of the LinkedList provided Two of the necessary classes are provided and can be downloaded from Blackboard with the posting of this Assignment. DO NOT CHANGE ANY OF THE CODE IN THESE TWO PROVIDED FILES. n separate files a rib below. A You are required to implement the ListNode and LinkedList java rtion asse OeSCI the code was reviewed in the lectures and a version of those methods can be found in the text book. Not combines the List Nod nd LinkedList a single Java file, and therefore, the b k cod nextNod asl and data' attributes direct in the List class methods. In our version you will need to use the constructor getNext setNext et Data and "set Data methods to access the private data members of the ListNode class UML DIAGRAM DISCUSSION OF ListNode NOTE THIS CLASS WILL BE REUSED IN THE NEXT PROGRAM Place the ListNode class in the ackage List Pk At the top of the ListNode.java file insert the following line o package ListPkg; List Node data :T nextNode: ListNode Discussion of the class ListNode: ListNode(object T) sets data object and nextNode to null ListNode(object T, node ListNode returns nextNode

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago