Question

Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...

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 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() {

}

}

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);

}

}

Homework Answers

Answer #1

======================DONE WITH PART 2 ===========

/*

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.

*/

class Node<T> {

public T element;

public Node link;

}

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 n = new Node();

n.element = info;

n.link = null;

if (front == null) {

front = n;

} else {

rear.link = n;

}

rear = n;

size++;

}

public T dequeue() {

if (front == null) {

return null;

} else {

T doomed = (T) front.element;

front = front.link;

if (front == null) {

rear = null;

}

size--;

return doomed;

}

}

public boolean isEmpty() {

return size == 0;

}

public int size() {

return 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);

}

}

============================================================================

SEE OUTPUT, I have Written Node class as well as it was not provided

Thanks, PLEASE COMMENT if there is any concern.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
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...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
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...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT