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

}

}

Homework Answers

Answer #1

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:

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 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...
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...
can you please explain how to complete all methods in java ? thanks /* Note:   Do...
can you please explain how to complete all methods in java ? thanks /* 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>...
(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;    ...
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,...
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...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
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...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
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....