Need help with fixing errors in Java. The assignment is to create List, Stack, a
ID: 3698342 • Letter: N
Question
Need help with fixing errors in Java. The assignment is to create List, Stack, and Queue with Linked List. I have complete almost all except getting it to work correctly. Below are my cexodes:
List.java
public class List extends LinkedListException
{
/*
* INSTANCE VARIABLE
* • No instance variable except head
* • Declared as inner class instead
*/
private Node1 head;
protected int size = 0; // the size limit
private class Node1 // implementing node as inner class
{
private Node1 head; // declared instance variable inside class instead
public Node1()
{
head = null;
}
public Node1(Node1 head)
{
this.head = head;
}
} // End of Node inner class
// METHOD(S)
/*
* Constructor to set head to null
*/
public List()
{
head = null;
}
/*
* -Override this method-
* ----------------------
* • Override insert() from superclass to simply call push() or enqueue() without using index
* • The case below exist because of the increased complexity involved when
* implementing a linked list, but essentially reduce to determing "do I change head?
* Or a node indirectly attached to head?" Note that not all subcases apply to each
* outer case; for example, you don't worry about adding to the body or tail of a list if
* your adding to an empty list (the first outer case). Read chapter 15 for more details
* on this.
*/
@Override
public void insert(Object next, int index) // (15-30 LOC)
{
// Make a new node that holds the Object "next", and insert this at the position
// specified by index
/*
* In each case above, note that the following subcases exist
* Adding to the head of the list (changes head)
* Adding to the body or the tail of the list (not head)
*/
try
{
// Divide this method into sections/cases using an if-statements
if (isEmpty()) // If adding to an empty list (changes head){}
{
insert(next, index);
size++;
}
else if (index == 1) // else if adding to a single element list (changes head) {}
{
insert(next, index);
size++;
}
else if (index >= 2) // else if adding to a populated list (n >= 2)
{
insert(next, index);
size++;
}
}
catch(Exception e)
{
System.exit(0); // exit if exception caught
}
}
/*
* -Override this method-
* Consider using your superclass methods here rather than building pop by hand
* • Find and delete the node at the position specified by index
*/
@Override
public Object remove(int index)
{
// Note that this method, like add() above, has the following cases
// Deleting on an empty list
// Deleting on a single element list (head only)
// Deleting on a populated list ( n >= 2)
/*
* And, just like above, these are the following subcases:
* • Deleting the head of the list (changes head)
* • Deleting from the body or trail of the list (no changes to head)
*/
try
{
// Divide this method into sections/cases using an if-statements
if (isEmpty()) // If adding to an empty list (changes head){}
{
throw new LinkedListException("index out of bound");
}
else if (index == 1) // else if adding to a single element list (changes head) {}
{
remove(index);
size--;
return true;
}
else if (index >= 2) // else if adding to a populated list (n >= 2)
{
remove(index);
size--;
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
System.exit(0); // exit if exception caught
}
return false;
}
/*
* A standard accessor
*/
public int size()
{
return size; // return size
}
/*
* Enumerate your list and return this enumeration as String
*/
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < size; i++) // for element less than i, add , to end of each element
{
sb.append( head );
if(i != (size - 1)) sb.append(',').append(' ');
}
return sb.append(']').toString();
}
/*
* A sequential search that returns: the index if found | -1 if not found
*/
public int indexOf(Object target)
{
for(int i = 0; i < size; i++) // for any element in size
{
if (target.equals(head)) return i; // return index of element if found
}
return -1; // otherwise return -1
}
/*
* if empty
*/
public boolean isEmpty()
{
return size() == 0;
}
/*
* • Make a main in the list class
* Test inserts for each outlined in the add and remove functions above, to be
* sure each section of logic works in these more complex functions
* Add a test that attempts to remove elements from an empty list
* Add a test that attempts to remove elements past the size of the list
*/
public static void main(String[] args)
{
List empty = new List();
List List();
List multiple = new List();
one.append(5);
multiple.append(10);
multiple.append(20);
multiple.append(30);
System.out.println("Empty: " + empty);
System.out.println("One: " + one);
System.out.println("Multiple: " + multiple);
one.delete(0);
multiple.delete(1);
System.out.println("One (upon delete): " + one);
System.out.println("Multiple (upon delete): " + multiple);
// one.insert(600, 1);
multiple.insert(400, 2);
System.out.println("One (on insert): " + one);
System.out.println("Multiple (on insert): " + multiple);
}
Stack.java
public class Stack extends List
{
/*
* • Insert to the beginning of the list
* • Consider using superclass methods here rather than implementing push
*/
@Override
public void push(Object next)
{
super.insert(next, 0); // 0 for index that is beginning of the list
}
/*
* • Removes from beginning of the list
* • Consider using superclass method here rather than building pop by hand
*/
@Override
public Object pop()
{
return super.remove(0);
}
/*
* A driver to test stack - See Driver Test for more information
* • Add a test that demonstrates pushses and pops actually reverse the order of the stored elements
* • Add a test that attempts to pop elements from an empty list
*/
public static void main(String[] args)
{
Stack empty = new Stack();
Stack Stack();
Stack multiple = new Stack();
one.push("B");
one.push("a");
one.push("t");
System.out.println(one.toString());
while(one.isEmpty() == false)
{
System.out.println(one.pop());
}
}
}
Queue.java
public class Queue extends List
{
/*
* THE QUEUE SUBCLASS
* ------------------
* Now we'll turn our attention to building a Queue. Remember when we used to cut and paste code and
* logic, then make subtle changes to it to build our new class? Instead, we'll use inheritance here,
* automate the copy-and-paste, and just focus on implementing the few methods that differ in our
* subclass (enqueue(), dequeue()). When you're done with this class, test it using the supplied driver
* software.
*/
// METHODS
/*
* public void enqueue(Object next);
* • inserts to the beginning of the list
* • Consider using your superclass methods here rather than implementing enqueue
*/
@Override
public void enqueue(Object next)
{
super.insert(next, 0); // 0 for beginning of list
}
/*
* public Object dequue();
* • removes from the end of the list
* • Consider using superclass methods here rather than building dequeue() by hand
*/
@Override
public Object dequeue()
{
return super.remove(super.size() - 1);
}
/*
* public static void main(String[] args)
* • A driver to test your stack -- see below for more information
* ---------------------------------------------------------------
* • Add a test that demonstrates the FIFO ordering principles of queues
* • Add a test that attempts to dequue elements from an empty dequeue
*/
public static void main(String[] args)
{
Queue empty = new Queue();
Queue Queue();
Queue multiple = new Queue();
one.append(5);
multiple.append(10);
multiple.append(20);
multiple.append(30);
System.out.println("Empty: " + empty);
System.out.println("One: " + one);
System.out.println("Multiple: " + multiple);
one.delete(0);
multiple.delete(1);
System.out.println("One (upon delete): " + one);
System.out.println("Multiple (upon delete): " + multiple);
// one.insert(600, 1);
multiple.insert(400, 2);
System.out.println("One (on insert): " + one);
System.out.println("Multiple (on insert): " + multiple);
}
}
LinkedListException.java
public class LinkedListException extends Exception
{
/*
* THE LinkedListException CLASS
* -----------------------------
* This class is an exercise in inheriting from classes provided to you by the Java API. Your class should
* have only two methods (both constructors - one empty non-parameter constructor and another that
* takes a String as a parameter) and no data items. Throw this exception when an error or exception
* occurs in your code, such as a pop() on an empty stack (determine what these error/exception
* conditions first ahead of time).
*/
// Two Constructors (One empty | One string)
/*
* One empty non-parameter constructor
*/
public LinkedListException()
{
super("Linked List Exception");
}
/*
* Another constructor that takes a String parameter
* • Throw when an error or exception occurs in code, such as pop() on empty stack
* (determine what these error/exception conditions first ahead of time)
*/
public LinkedListException(String message)
{
super(message);
}
}
Explanation / Answer
Note:
Given code modified as required and given code Each class is having main function, and unavailable method is used in the code, code is running and modify it for your requirement.
List.java
public class List extends LinkedListException
{
/*
* INSTANCE VARIABLE
* • No instance variable except head
* • Declared as inner class instead
*/
private Node1 head;
protected int size = 0; // the size limit
private class Node1 // implementing node as inner class
{
private Node1 head; // declared instance variable inside class instead
public Node1()
{
head = null;
}
public Node1(Node1 head)
{
this.head = head;
}
} // End of Node inner class
// METHOD(S)
/*
* Constructor to set head to null
*/
public List()
{
head = null;
}
/*
* -Override this method-
* ----------------------
* • Override insert() from superclass to simply call push() or enqueue() without using index
* • The case below exist because of the increased complexity involved when
* implementing a linked list, but essentially reduce to determing "do I change head?
* Or a node indirectly attached to head?" Note that not all subcases apply to each
* outer case; for example, you don't worry about adding to the body or tail of a list if
* your adding to an empty list (the first outer case). Read chapter 15 for more details
* on this.
*/
//@Override
public void insert(Object next, int index) // (15-30 LOC)
{
// Make a new node that holds the Object "next", and insert this at the position
// specified by index
/*
* In each case above, note that the following subcases exist
* Adding to the head of the list (changes head)
* Adding to the body or the tail of the list (not head)
*/
try
{
// Divide this method into sections/cases using an if-statements
if (isEmpty()) // If adding to an empty list (changes head){}
{
insert(next, index);
size++;
}
else if (index == 1) // else if adding to a single element list (changes head) {}
{
insert(next, index);
size++;
}
else if (index >= 2) // else if adding to a populated list (n >= 2)
{
insert(next, index);
size++;
}
}
catch(Exception e)
{
System.exit(0); // exit if exception caught
}
}
/*
* -Override this method-
* Consider using your superclass methods here rather than building pop by hand
* • Find and delete the node at the position specified by index
*/
//@Override
public Object remove(int index)
{
// Note that this method, like add() above, has the following cases
// Deleting on an empty list
// Deleting on a single element list (head only)
// Deleting on a populated list ( n >= 2)
/*
* And, just like above, these are the following subcases:
* • Deleting the head of the list (changes head)
* • Deleting from the body or trail of the list (no changes to head)
*/
try
{
// Divide this method into sections/cases using an if-statements
if (isEmpty()) // If adding to an empty list (changes head){}
{
throw new LinkedListException("index out of bound");
}
else if (index == 1) // else if adding to a single element list (changes head) {}
{
remove(index);
size--;
return true;
}
else if (index >= 2) // else if adding to a populated list (n >= 2)
{
remove(index);
size--;
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
System.exit(0); // exit if exception caught
}
return false;
}
/*
* A standard accessor
*/
public int size()
{
return size; // return size
}
/*
* Enumerate your list and return this enumeration as String
*/
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < size; i++) // for element less than i, add , to end of each element
{
sb.append( head );
if(i != (size - 1)) sb.append(',').append(' ');
}
return sb.append(']').toString();
}
/*
* A sequential search that returns: the index if found | -1 if not found
*/
public int indexOf(Object target)
{
for(int i = 0; i < size; i++) // for any element in size
{
if (target.equals(head)) return i; // return index of element if found
}
return -1; // otherwise return -1
}
/*
* if empty
*/
public boolean isEmpty()
{
return size() == 0;
}
/*
* • Make a main in the list class
* Test inserts for each outlined in the add and remove functions above, to be
* sure each section of logic works in these more complex functions
* Add a test that attempts to remove elements from an empty list
* Add a test that attempts to remove elements past the size of the list
*/
public static void main(String[] args)
{
List empty = new List();
List List();
List multiple = new List();
// here you tried to append the value to method, append usage is not possible in method
System.out.println("Empty: " + empty);
System.out.println("One: " + one);
System.out.println("Multiple: " + multiple);
one.remove(0);
multiple.remove(1);
System.out.println("One (upon delete): " + one);
System.out.println("Multiple (upon delete): " + multiple);
// one.insert(600, 1);
multiple.insert(400, 2);
System.out.println("One (on insert): " + one);
System.out.println("Multiple (on insert): " + multiple);
}
Stack.java
public class Stack extends List
{
/*
* • Insert to the beginning of the list
* • Consider using superclass methods here rather than implementing push
*/
//@Override
public void push(Object next)
{
super.insert(next, 0); // 0 for index that is beginning of the list
}
/*
* • Removes from beginning of the list
* • Consider using superclass method here rather than building pop by hand
*/
//@Override
public Object pop()
{
return super.remove(0);
}
/*
* A driver to test stack - See Driver Test for more information
* • Add a test that demonstrates pushses and pops actually reverse the order of the stored elements
* • Add a test that attempts to pop elements from an empty list
*/
public static void main(String[] args)
{
Stack empty = new Stack();
Stack Stack();
Stack multiple = new Stack();
one.push("B");
one.push("a");
one.push("t");
System.out.println(one.toString());
while(one.isEmpty() == false)
{
System.out.println(one.pop());
}
}
}
Queue.java
public class Queue extends List
{
/*
* THE QUEUE SUBCLASS
* ------------------
* Now we'll turn our attention to building a Queue. Remember when we used to cut and paste code and
* logic, then make subtle changes to it to build our new class? Instead, we'll use inheritance here,
* automate the copy-and-paste, and just focus on implementing the few methods that differ in our
* subclass (enqueue(), dequeue()). When you're done with this class, test it using the supplied driver
* software.
*/
// METHODS
/*
* public void enqueue(Object next);
* • inserts to the beginning of the list
* • Consider using your superclass methods here rather than implementing enqueue
*/
//@Override
public void enqueue(Object next)
{
super.insert(next, 0); // 0 for beginning of list
}
/*
* public Object dequue();
* • removes from the end of the list
* • Consider using superclass methods here rather than building dequeue() by hand
*/
//@Override
public Object dequeue()
{
return super.remove(super.size() - 1);
}
/*
* public static void main(String[] args)
* • A driver to test your stack -- see below for more information
* ---------------------------------------------------------------
* • Add a test that demonstrates the FIFO ordering principles of queues
* • Add a test that attempts to dequue elements from an empty dequeue
*/
public static void main(String[] args)
{
Queue empty = new Queue();
Queue Queue();
Queue multiple = new Queue();
// here you tried to append the value to method, append usage is not possible in method
System.out.println("Empty: " + empty);
System.out.println("One: " + one);
System.out.println("Multiple: " + multiple);
one.dequeue();
multiple.dequeue();
System.out.println("One (upon delete): " + one);
System.out.println("Multiple (upon delete): " + multiple);
// one.insert(600, 1);
multiple.insert(400, 2);
System.out.println("One (on insert): " + one);
System.out.println("Multiple (on insert): " + multiple);
}
}
LinkedListException.java
public class LinkedListException extends Exception
{
/*
* THE LinkedListException CLASS
* -----------------------------
* This class is an exercise in inheriting from classes provided to you by the Java API. Your class should
* have only two methods (both constructors - one empty non-parameter constructor and another that
* takes a String as a parameter) and no data items. Throw this exception when an error or exception
* occurs in your code, such as a pop() on an empty stack (determine what these error/exception
* conditions first ahead of time).
*/
// Two Constructors (One empty | One string)
/*
* One empty non-parameter constructor
*/
public LinkedListException()
{
super("Linked List Exception");
}
/*
* Another constructor that takes a String parameter
* • Throw when an error or exception occurs in code, such as pop() on empty stack
* (determine what these error/exception conditions first ahead of time)
*/
public LinkedListException(String message)
{
super(message);
}
}