In: Computer Science
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.
*/
/*
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() {
}
}
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);
}
}
If you have any doubts, please give me comment...
/*
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;
@SuppressWarnings("unchecked")
public QueueArray() {
elements = (T[]) new Object[CAPACITY];
}
@SuppressWarnings("unchecked")
public QueueArray(int size) {
CAPACITY = size;
elements = (T[]) new Object[size];
}
public T dequeue() {
if(!isEmpty()){
T temp = elements[0];
for (int i = 0; i < rearIndex; i++) {
elements[i] = elements[i + 1];
}
rearIndex--;
return temp;
}
return null;
}
public void enqueue(T info) {
if(!isFull()){
rearIndex++;
elements[rearIndex] = info;
}
}
public boolean isEmpty() {
return rearIndex == -1;
}
public boolean isFull() {
return rearIndex+1 == CAPACITY;
}
public int size() {
return rearIndex+1;
}
}
/*
Tips: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<String> myQueueArray = new QueueArray(2);
myQueueArray.dequeue();
if (myQueueArray.isEmpty() && !myQueueArray.isFull() && myQueueArray.size() == 0){
System.out.println("points-1");
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()){
System.out.println("points-2");
score += 6;
}
if (myQueueArray.dequeue().equals("Orange") && myQueueArray.size() == 1 && !myQueueArray.isEmpty()){
System.out.println("points-3");
score += 6;
}
if (myQueueArray.dequeue().equals("Mango") && myQueueArray.size() == 0 && myQueueArray.isEmpty()){
System.out.println("points-4");
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/24 \n", score);
}
}