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 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() {
}
}
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);
}
}
Code:
Node.js:
/*
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) {
this.info = info;
this.nextLink = null;
}
public void setInfo(T info) {
this.info = info;
}
public void setNextLink(Node nextNode) {
this.nextLink = nextNode;
}
public T getInfo() {
return this.info;
}
public Node getNextLink() {
return this.nextLink;
}
}
QueueArray.java:
public class QueueArray {
int startIndex;
int endIndex;
int maxSize;
int size;
String[] data;
// Constructor
// Parameter: integer size
// size is used to initialize the String array
public QueueArray(int size)
{
this.startIndex = 0;
this.endIndex = 0;
this.maxSize = size;
this.size = 0;
data = new String[size];
}
// Returns the number of elements in the queue
public int size()
{
return this.size;
}
// adds a string element, s into the queue
public void enqueue(String s)
{
// check if the number of elements
is equal to the
// total size of the String
array
if(this.size == this.maxSize)
{
System.out.println("The Queue is full");
return;
}
// Adding the element in the String
array
// (this.endIndex++)%this.maxSize
will return 0,1,2,....(maxSize-1)
// for example: maxSize = 5
// and the startIndex = 3 and
endIndex = 4
// (endIndex++)%maxSize = (4+1)%5 =
0
// the next element will be added
into data[0]
this.data[(this.endIndex++)%this.maxSize] = s;
this.size++;
this.endIndex %=
this.maxSize;
}
public String dequeue()
{
// Checking if the queue is
empty
if(this.size == 0)
{
System.out.println("The Queue is empty");
return
null;
}
// Deleting the element from
startIndex
String result =
this.data[startIndex];
this.startIndex = (this.startIndex
+ 1) % this.maxSize;
this.size--;
return result;
}
public Boolean isFull()
{
// if the number of elements is
equal to
// the max size of the array
if(this.size == this.maxSize)
return
true;
return false;
}
public Boolean isEmpty()
{
// if the number of elements in
queue is zero
if(this.size == 0)
return
true;
return false;
}
}
QueueLinkedList:
public class QueueLinkedList {
// Two nodes to represent the first and the last
node of the linked list
// because for adding the element we need the first
node
// and removing we need the last node;
Node<String> startNode;
Node<String> endNode;
int Qsize;
public QueueLinkedList()
{
Qsize = 0;
}
public int size()
{
return this.Qsize;
}
public Boolean isEmpty()
{
if(this.Qsize == 0)
return
true;
return false;
}
public void enqueue(String s)
{
// checking if the linked list is
empty and initializing the first node
if(this.startNode == null
&& this.endNode == null)
{
this.startNode =
new Node<String>(s);
this.endNode =
this.startNode;
this.Qsize++;
return;
}
// Adding the new node to the last
of the Linked List because First in First out
Node<String> temp = new
Node<String>(s);
this.endNode.setNextLink(temp);
this.endNode = temp;
this.Qsize++;
}
public String dequeue()
{
if(this.Qsize == 0)
{
System.out.println("The Queue is empty");
return
null;
}
// Removing a node from the head
node of the Linked List.
String result =
this.startNode.getInfo();
if(Qsize == 1) //if the size is
one, both startNode and endNode will be point to the same
node
{
this.startNode = null;
this.endNode = null;
}
else
this.startNode =
this.startNode.getNextLink(); // Changing the startNode to point to
the next Node in the Linked List.
Qsize--;
return result;
}
}
StackLinkedList.java:
public class StackLinkedList {
// stack will be the head node of the Linked
List
Node<String> stack;
int Ssize;
public StackLinkedList()
{
this.Ssize = 0;
}
public void push(String s)
{
// If the stack is empty, the stack
is initialized.
if(this.stack == null)
{
this.stack = new
Node<String>(s);
this.Ssize++;
return;
}
// the new node will be added at
the front because of First In Last Out in a Stack
Node<String> temp = new
Node<String>(s);
this.stack.setNextLink(temp);
this.stack = temp;
this.Ssize++;
}
public String pop()
{
if(this.Ssize == 0)
{
System.out.println("The Stack is empty");
return
null;
}
// remove the node pointed by the
stack pointer
// and point to the next
node.
String result =
this.stack.getInfo();
this.stack =
this.stack.getNextLink();
this.Ssize--;
return result;
}
public int size()
{
return this.Ssize;
}
// if the number of elements in the stack is zero,
return true else false
public Boolean isEmpty()
{
if(this.Ssize == 0)
return
true;
return false;
}
}
Output:
The Queue is empty
The Queue is full
The Queue is empty
The Stack is empty
your score is 60/60
IDE:
Get Answers For Free
Most questions answered within 1 hours.