Complete the java program.
/*
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.
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);
}
}
//SINCE THE QUESTION IS TO IMPLEMENT StackLinkedList AND THERE
ARE ONLY 3 TESTS //ON myStack THE RESULT OF SCORE
//WILL BE 18.
/*
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.
*/
//Node class , question have no implementaion for Node class
//that's why i have created one.
class Node<T>
{
public T val;
public Node next;
Node(T val)
{
this.val = val;
next = null;
}
}
public class StackLinkedList<T>
{
private Node topPointer;
private int size;
public StackLinkedList()
{
topPointer = null;
size = 0;
}
public void push(T info)
{
if(topPointer == null)
{
Node node = new
Node(info);
topPointer =
node;
size++;
}
else
{
Node node = new
Node(info);
node.next =
topPointer;
topPointer =
node;
size++;
}
}
public T pop()
{
if(topPointer == null)
{
return
null;
}
else
{
Node temp =
topPointer;
topPointer =
topPointer.next;
size--;
return
(T)temp.val;
}
}
public T top(){
if(topPointer == null)
{
return
null;
}
else
{
return
(T)topPointer.val;
}
}
public boolean isEmpty()
{
return size == 0?true:false;
}
public int size()
{
return size;
}
}
//---------------- 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;
}
//SINCE THE QUESTION IS TO
IMPLEMENT StackLinkedList AND THERE ARE ONLY 3 //TESTS ON myStack
THE RESULT OF SCORE
//WILL BE 18.
System.out.printf("your score is
%d/60 \n", score);
}
}
//----------- OUTPUT ------------
//SINCE THE QUESTION IS TO IMPLEMENT StackLinkedList AND THERE
ARE ONLY 3 TESTS //ON myStack THE RESULT OF SCORE
//WILL BE 18.
Get Answers For Free
Most questions answered within 1 hours.