In: Computer Science
Complete all parts of this java program. Use Homework2Driver.java below for testing.
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
public class Node<T> {
private T info;
private Node nextLink;
public Node(T info) {
}
public void setInfo(T info) {
}
public void setNextLink(Node nextNode) {
}
public T getInfo() {
}
public Node getNextLink() {
}
}
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
/*
Hint: This Queue implementation will always dequeue from the first element of
the array i.e, elements[0]. Therefore, remember to shift all elements
toward front of the queue after each dequeue.
*/
public class QueueArray<T> {
public static int CAPACITY = 100;
private final T[] elements;
private int rearIndex = -1;
public QueueArray() {
}
public QueueArray(int size) {
}
public T dequeue() {
}
public void enqueue(T info) {
}
public boolean isEmpty() {
}
public boolean isFull() {
}
public int size() {
}
}
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
public class QueueLinkedList<T> {
private Node front;
private Node rear;
private int size;
public QueueLinkedList() {
}
public void enqueue(T info) {
}
public T dequeue() {
}
public boolean isEmpty()
}
public int size() {
}
}
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
public class StackLinkedList<T> {
private Node topPointer;
private int size;
public StackLinkedList() {
}
public void push(T info) {
}
public T pop() {
}
public T top() {
}
public boolean isEmpty() {
}
public int size() {
}
}
Run this program ( Homework2Driver.java ) to test with your written java code.
Comment out sections that you have not finished, so it does not
interfere your troubleshooting.
For example, commenting out parts 2 and 3 while testing part 1.
public class Homework2Driver {
public static void main(String [] args) {
int score = 0;
// Part 1: Array based Queue - QueueArray.java
QueueArray myQueueArray = new QueueArray(2);
myQueueArray.dequeue();
if (myQueueArray.isEmpty() && !myQueueArray.isFull() && myQueueArray.size()
== 0)
score += 6;
myQueueArray.enqueue("Orange");
myQueueArray.enqueue("Mango");
myQueueArray.enqueue("Guava"); // Note: with Queue size 2, this won't get
into the queue.
if (myQueueArray.isFull())
score += 6;
if (myQueueArray.dequeue().equals("Orange") && myQueueArray.size() == 1
&& !myQueueArray.isEmpty())
score += 6;
if (myQueueArray.dequeue().equals("Mango") && myQueueArray.size() == 0 &&
myQueueArray.isEmpty())
score += 6;
// Part 2: Linked List based Queue - QueueLinkedList.java
QueueLinkedList myQueueList = new QueueLinkedList();
myQueueList.dequeue();
if (myQueueList.isEmpty() && myQueueList.size() == 0)
score += 6;
myQueueList.enqueue("Apple");
myQueueList.dequeue();
myQueueList.enqueue("Orange");
myQueueList.enqueue("Lemon");
if (myQueueList.dequeue().equals("Orange") && myQueueList.size() == 1 && !
myQueueList.isEmpty())
score += 6;
if (myQueueList.dequeue().equals("Lemon") && myQueueList.size() == 0 &&
myQueueList.isEmpty())
score += 6;
// Part 3: Linked List based Stack - StackLinkedList.java
StackLinkedList myStack = new StackLinkedList();
myStack.pop();
if (myStack.isEmpty() && myStack.size() == 0)
score += 6;
myStack.push("Peach");
if (!myStack.isEmpty() && myStack.size() == 1)
score += 6;
myStack.pop();
myStack.push("Pineapple");
if (myStack.pop().equals("Pineapple") && myStack.isEmpty() &&
myStack.size() == 0)
score += 6;
System.out.printf("your score is %d/60 \n", score);
}
}
Just have to fill in written code with the templates given, and then to test that code with the Homework2Driver provides.
//Node.java
public class Node<T> {
  
   private T info;
private Node nextLink;
   public Node(T info) {
       this.info = info;
       nextLink = null;
   }
   public void setInfo(T info) {
       this.info = info;
   }
   public void setNextLink(Node nextNode) {
       nextLink = nextNode;
   }
   public T getInfo() {
       return info;
   }
   public Node getNextLink() {
       return nextLink;
   }
}
//end of Node.java
//QueueArray.java
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
/*
Hint: This Queue implementation will always dequeue from the first element of
the array i.e, elements[0]. Therefore, remember to shift all elements
toward front of the queue after each dequeue.
*/
public class QueueArray<T> {
  
   public static int CAPACITY = 100;
private final T[] elements;
private int rearIndex = -1;
   public QueueArray() {
       elements = (T[])new
Object[CAPACITY];
       rearIndex = -1;
   }
   public QueueArray(int size) {
       elements = (T[])new
Object[size];
       rearIndex = -1;  
   }
   public T dequeue() {
      
       if(isEmpty())
           return
null;
       T data = elements[0];
       for(int
i=0;i<rearIndex;i++)
           elements[i] =
elements[i+1];
       rearIndex--;
       return data;
   }
   public void enqueue(T info) {
       if(!isFull())
       {
          
rearIndex++;
          
elements[rearIndex] = info;
       }
   }
   public boolean isEmpty() {
       return(rearIndex == -1);
   }
   public boolean isFull() {
       return(size() ==
elements.length);
   }
   public int size() {
       return rearIndex+1;
   }
  
}
//end of QueueArray.java
// QueueLinkedList.java
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
public class QueueLinkedList<T> {
private Node front;
private Node rear;
private int size;
   public QueueLinkedList() {
       front = null;
       rear = null;
       size = 0;
}
   public void enqueue(T info) {
       Node node = new Node(info);
       if(isEmpty())
       {
           front =
node;
           rear =
node;
       }else
       {
          
rear.setNextLink(node);
           rear =
node;
       }
       size++;
   }
   public T dequeue() {
       if(!isEmpty())
       {
           T data = (T)
front.getInfo();
           front =
front.getNextLink();
           if(front ==
null)
          
    rear = null;
           size--;
          
           return
data;
       }  
   
      
       return null;
   }
   public boolean isEmpty() {
       return(front == null);
   }
   public int size() {
       return size;
   }
}
//end of QueueLinkedList.java
//StackLinkedList.java
/*
Note: Do not add any additional methods, attributes.
Do not modify the given part of the program.
Run your program against the provided Homework2Driver.java for
requirements.
*/
public class StackLinkedList<T> {
private Node topPointer;
private int size;
   public StackLinkedList() {
       topPointer = null;
       size=0;
   }
   public void push(T info) {
       Node node = new Node(info);
       if(isEmpty())
           topPointer =
node;
       else
       {
          
node.setNextLink(topPointer);
           topPointer =
node;
       }
       size++;
   }
   public T pop() {
       if(!isEmpty())
       {
           T data = (T)
topPointer.getInfo();
           topPointer =
topPointer.getNextLink();
           size--;
           return
data;
       }
      
       return null;
   }
   public T top() {
       if(!isEmpty())
           return (T)
topPointer.getInfo();
       return null;
   }
   public boolean isEmpty() {
       return(topPointer == null);
}
   public int size() {
       return size;
   }
}
//end of StackLinkedList.java
//Homework2Driver.java
public class Homework2Driver {
  
   public static void main(String [] args) {
  
   int score = 0;
// Part 1: Array based Queue - QueueArray.java
QueueArray myQueueArray = new QueueArray<>(2);
myQueueArray.dequeue();
   if (myQueueArray.isEmpty() &&
!myQueueArray.isFull() && myQueueArray.size()== 0)
       score += 6;
myQueueArray.enqueue("Orange");
myQueueArray.enqueue("Mango");
myQueueArray.enqueue("Guava"); // Note: with Queue size 2, this won't get into the queue.
if (myQueueArray.isFull())
score += 6;
if (myQueueArray.dequeue().equals("Orange") && myQueueArray.size() == 1
&& !myQueueArray.isEmpty())
score += 6;
if (myQueueArray.dequeue().equals("Mango") && myQueueArray.size() == 0 &&
myQueueArray.isEmpty())
score += 6;
// Part 2: Linked List based Queue - QueueLinkedList.java
QueueLinkedList myQueueList = new QueueLinkedList();
myQueueList.dequeue();
if (myQueueList.isEmpty() && myQueueList.size() == 0)
score += 6;
myQueueList.enqueue("Apple");
myQueueList.dequeue();
myQueueList.enqueue("Orange");
myQueueList.enqueue("Lemon");
if (myQueueList.dequeue().equals("Orange") && myQueueList.size() == 1 && !
myQueueList.isEmpty())
score += 6;
if (myQueueList.dequeue().equals("Lemon") && myQueueList.size() == 0 &&
myQueueList.isEmpty())
score += 6;
// Part 3: Linked List based Stack - StackLinkedList.java
StackLinkedList myStack = new StackLinkedList();
myStack.pop();
if (myStack.isEmpty() && myStack.size() == 0)
score += 6;
myStack.push("Peach");
if (!myStack.isEmpty() && myStack.size() == 1)
score += 6;
myStack.pop();
myStack.push("Pineapple");
if (myStack.pop().equals("Pineapple") && myStack.isEmpty() &&
myStack.size() == 0)
score += 6;
   System.out.printf("your score is %d/60 \n",
score);
   }
}
//end of Homework2Driver.java
Output:
