Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

I need help with the ListNode java code and the LinkedList code for this java program. The other two files are provided and the code listed at the bottom.

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<T> data :T nextNode: ListNode<T constructor ListNode(object T) constructor ListNode(object T, node ListNode<T setData (object: T) getData() T setNext(next ListNode<T get Next List Node <T> Discussion of the class ListNode: <<constructor >ListNode(object T) sets data object and nextNode to null <<constructor >ListNode(object T, node ListNode<T sets data object and nextNode-node setData (object :T) sets data object getData T-returns data set Next (next ListNode<T sets nextNode next getNext ListNode <T> returns nextNode

\

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<Integer> myList = new LinkedList<Integer>("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 < 5; x++)
{
myList.insertAtFront(x);
}
printLengthAndList(myList);

// 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 < 5; x++)
{
myList.insertAtBack(x);
}
printLengthAndList(myList);

// 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\n", position);
}
else
{
System.out.println("Value of 5 not found");
}

// Try to find position of an item in the list
System.out.println("\nAttempt 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\n", position);
}
else
{
System.out.println("Value of 0 not found");
}

// Remove the item at 'position' in the list
System.out.printf("\nValue at position %d being removed\n", 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\n", temp);

// Test the 'getItemAt' method for the IndexOutOfBoundsException
System.out.println("\nBegin 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\n");

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

System.out.println("\nBegin 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\n");
}

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

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.