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

Using the correct BuggyList.java from Lab 1 (attached below), replace the return

ID: 3871204 • Letter: U

Question

Using the correct BuggyList.java from Lab 1 (attached below),

replace the return value error handling in the removeFront method and in the elementAt method with throwing Java exceptions.

Add try and catch blocks to the driver program (attached below) to test your exception handling.

Submit your BuggyList.java (with Java exceptions) and your driver file (with exception handlers) to C4 for the lab credit.

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

// File:        BuggyList.java
// Author:      Geoffrey Tien/ Rita Ester
// Description: Buggy linked list implementation for CSCI 225 Lab 1
//              Based on SimpleStack at course website

public class BuggyList
{
// private inner Node class
private class Node
{
    // all members public, for convenience
    public int data;
    public Node next;

    // parameterized constructor
    public Node(int x, Node nd)
    {
      data = x;
      next = nd;
    }
}

// private member variables
private int n;    // number of items in list
private Node front; // front of list
private Node back; // back of list

// public member functions

// default constructor
public BuggyList()
{
    n = 0;
    front = null;
    back = null;
}

// copy constructor
public BuggyList(BuggyList ls)
{
    Node nd = ls.front;
    n = 0;             // ERROR: n needs to be initialized
    while (nd != null)
    {
      addBack(nd.data);
      nd = nd.next;    // ERROR: go to the next element in the list
    }
}

// Inserts an int to the front of list
// PARAM: x is the value to be inserted
public void addFront(int x)
{
    Node nd = new Node(x, front);
    front = nd;
    if (n == 0)
    back = nd; // ERROR: needed to set back in special case
    n++; // ERROR: size not updated
}

//Inserts an int to the back of list
// PARAM: x is the value to be inserted
public void addBack(int x)
{
   Node nd = new Node(x, null);
   if (n == 0)
    front = nd; // ERROR: needed to set front in special case
   if (n > 0)
     back.next = nd; // ERROR: next pointer of (old) back needs to be linked to newly created node
   back = nd;
   n++;
}

// Removes and returns the item at the front of list
// Returns -1 if the list is empty (in lieu of proper exception handling)
public int removeFront()
{
if (n == 0) // ERROR: special case not handled
  return -1;
    Node nd = front;
    front = front.next;
    n--;
    return nd.data;
}

// Returns the item at the specified index
//Returns -1 if the list is empty (in lieu of proper exception handling)
public int elementAt(int index)
{
    if (n < 0 || index < 0 || index >= n) return -1; // special cases added (optional)
    else
    {
      Node nd = front;
      // iterate to index
      for (int i = 0; i < index; i++)
      {
        nd = nd.next; // ERROR: changed from nd = front.next, since front does not change, nd will be repeatedly set to the same thing
      }
      return nd.data;
    }
}

// Returns the number of items in the list
public int count()
{
    return n;
}

// Returns true if the list contains no items
public boolean isEmpty()
{
    return (n == 0);
}
}

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

// File:        CSCI225Lab1Driver.java
// Author:      Geoffrey Tien/ Rita Ester
// Description: Driver file with main method for CSCI 225 Lab 1

public class CSCI225Lab1Driver
{
public static void main(String[] args)
{
    int errorcount = 0;
    boolean result = false;
    // create a new list and test if empty
    System.out.print("Creating new list and testing if empty... ");
    BuggyList listA = new BuggyList();
    result = listA.isEmpty();
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: list not empty");
      errorcount++;
    }
   
    // add 3 items to front of list
    System.out.print("Using addFront to add 3 items to front of list... ");
    listA.addFront(12);
    listA.addFront(9);
    listA.addFront(4);
    System.out.print("done. Checking if list is empty... ");
    result = listA.isEmpty();
    if (result)
    {
      System.out.println("ERROR: isEmpty returned true");
      errorcount++;
    }
    else
      System.out.println("done - OK!");
    System.out.print("Checking size of list... ");
    result = listA.count() == 3;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: list size should be 3");
      errorcount++;
    }
    // add two items to back of list
    System.out.print("Using addBack to add 2 items to back of list... ");
    listA.addBack(17);
    listA.addBack(26);
    System.out.print("done. Checking value of item at index 1... ");
    result = listA.elementAt(1) == 9;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(1) should be 9");
      errorcount++;
    }
    System.out.print("Checking value of item at index 2... ");
    result = listA.elementAt(2) == 12;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(2) should be 12");
      errorcount++;
    }
    System.out.print("Checking value of item at index 3... ");
    result = listA.elementAt(3) == 17;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(3) should be 17");
      errorcount++;
    }
    System.out.print("Checking value of item at index 4... ");
    result = listA.elementAt(4) == 26;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(4) should be 26");
      errorcount++;
    }
   
    // Create a new list and attempt to remove from it
    System.out.print("Creating a new list... ");
    BuggyList listB = new BuggyList();
    System.out.print(" done. Attempting to remove an item from front... ");
    int value = listB.removeFront();
    // Add 3 elements using AddBack
    System.out.print("Using addBack to add 2 items to back of list... ");
    listB.addBack(23);
    listB.addBack(37);
    listB.addBack(49);
    System.out.print("Checking value of item at index 2... ");
    result = listB.elementAt(2) == 49;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(2) should be 49");
      errorcount++;
    }
    System.out.println("Test program complete. Detected " + errorcount + " errors.");
}
}

Explanation / Answer

Hi

I have declared exceptions on removeFront method and elementAt method in BuggyList.java and these exceptions handle in CSCI225Lab1Driver.java.


// File:        BuggyList.java
// Author:      Geoffrey Tien/ Rita Ester
// Description: Buggy linked list implementation for CSCI 225 Lab 1
//              Based on SimpleStack at course website

public class BuggyList
{
// private inner Node class
private class Node
{
    // all members public, for convenience
    public int data;
    public Node next;

    // parameterized constructor
    public Node(int x, Node nd)
    {
      data = x;
      next = nd;
    }
}

// private member variables
private int n;    // number of items in list
private Node front; // front of list
private Node back; // back of list

// public member functions

// default constructor
public BuggyList()
{
    n = 0;
    front = null;
    back = null;
}

// copy constructor
public BuggyList(BuggyList ls)
{
    Node nd = ls.front;
    n = 0;             // ERROR: n needs to be initialized
    while (nd != null)
    {
      addBack(nd.data);
      nd = nd.next;    // ERROR: go to the next element in the list
    }
}

// Inserts an int to the front of list
// PARAM: x is the value to be inserted
public void addFront(int x)
{
    Node nd = new Node(x, front);
    front = nd;
    if (n == 0)
     back = nd; // ERROR: needed to set back in special case
    n++; // ERROR: size not updated
}

//Inserts an int to the back of list
// PARAM: x is the value to be inserted
public void addBack(int x)
{
   Node nd = new Node(x, null);
   if (n == 0)
    front = nd; // ERROR: needed to set front in special case
   if (n > 0)
     back.next = nd; // ERROR: next pointer of (old) back needs to be linked to newly created node
   back = nd;
   n++;
}

// Removes and returns the item at the front of list
// Returns -1 if the list is empty (in lieu of proper exception handling)
public int removeFront() throws Exception
{
if (n == 0) // ERROR: special case not handled
return -1;
    Node nd = front;
    front = front.next;
    n--;
    return nd.data;
}

// Returns the item at the specified index
//Returns -1 if the list is empty (in lieu of proper exception handling)
public int elementAt(int index) throws Exception
{
    if (n < 0 || index < 0 || index >= n) return -1; // special cases added (optional)
    else
    {
      Node nd = front;
      // iterate to index
      for (int i = 0; i < index; i++)
      {
        nd = nd.next; // ERROR: changed from nd = front.next, since front does not change, nd will be repeatedly set to the same thing
      }
      return nd.data;
    }
}

// Returns the number of items in the list
public int count()
{
    return n;
}

// Returns true if the list contains no items
public boolean isEmpty()
{
    return (n == 0);
}
}

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


// File:        CSCI225Lab1Driver.java
// Author:      Geoffrey Tien/ Rita Ester
// Description: Driver file with main method for CSCI 225 Lab 1

public class CSCI225Lab1Driver
{
public static void main(String[] args)
{
    int errorcount = 0;
    boolean result = false;
    // create a new list and test if empty
    System.out.print("Creating new list and testing if empty... ");
    BuggyList listA = new BuggyList();
    result = listA.isEmpty();
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: list not empty");
      errorcount++;
    }

    // add 3 items to front of list
    System.out.print("Using addFront to add 3 items to front of list... ");
    listA.addFront(12);
    listA.addFront(9);
    listA.addFront(4);
    System.out.print("done. Checking if list is empty... ");
    result = listA.isEmpty();
    if (result)
    {
      System.out.println("ERROR: isEmpty returned true");
      errorcount++;
    }
    else
      System.out.println("done - OK!");
    System.out.print("Checking size of list... ");
    result = listA.count() == 3;
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: list size should be 3");
      errorcount++;
    }
    // add two items to back of list
    System.out.print("Using addBack to add 2 items to back of list... ");
    listA.addBack(17);
    listA.addBack(26);
    System.out.print("done. Checking value of item at index 1... ");
    try {
       result = listA.elementAt(1) == 9;
   } catch (Exception e4) {
       // TODO Auto-generated catch block
       e4.printStackTrace();
   }
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(1) should be 9");
      errorcount++;
    }
    System.out.print("Checking value of item at index 2... ");
    try {
       result = listA.elementAt(2) == 12;
   } catch (Exception e3) {
       // TODO Auto-generated catch block
       e3.printStackTrace();
   }
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(2) should be 12");
      errorcount++;
    }
    System.out.print("Checking value of item at index 3... ");
    try {
       result = listA.elementAt(3) == 17;
   } catch (Exception e2) {
       // TODO Auto-generated catch block
       e2.printStackTrace();
   }
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(3) should be 17");
      errorcount++;
    }
    System.out.print("Checking value of item at index 4... ");
    try {
       result = listA.elementAt(4) == 26;
   } catch (Exception e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
   }
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(4) should be 26");
      errorcount++;
    }

    // Create a new list and attempt to remove from it
    System.out.print("Creating a new list... ");
    BuggyList listB = new BuggyList();
    System.out.print(" done. Attempting to remove an item from front... ");
  
    try {
       int value = listB.removeFront();
   } catch (Exception e) {
      
       e.printStackTrace();
   }
    // Add 3 elements using AddBack
    System.out.print("Using addBack to add 2 items to back of list... ");
    listB.addBack(23);
    listB.addBack(37);
    listB.addBack(49);
    System.out.print("Checking value of item at index 2... ");
    try {
       result = listB.elementAt(2) == 49;
   } catch (Exception e) {
      
       e.printStackTrace();
   }
    if (result)
      System.out.println("done - OK!");
    else
    {
      System.out.println("ERROR: elementAt(2) should be 49");
      errorcount++;
    }
    System.out.println("Test program complete. Detected " + errorcount + " errors.");
}
}

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