Question

In: Computer Science

Complete all parts of this java program. Use Homework2Driver.java below for testing. /* Note: Do not...

Complete all parts of this java program. Use Homework2Driver.java below for testing.

/*

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

}

}

/*

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

}

}

/*

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

}

}

/*

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 with your written java code.

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

}

}

Just have to fill in written code with the templates given, and then to test that code with the Homework2Driver provides.

Solutions

Expert Solution

//Node.java

public class Node<T> {
  
   private T info;

   private Node nextLink;

   public Node(T info) {
       this.info = info;
       nextLink = null;
   }

   public void setInfo(T info) {
       this.info = info;
   }

   public void setNextLink(Node nextNode) {
       nextLink = nextNode;
   }

   public T getInfo() {
       return info;
   }

   public Node getNextLink() {
       return nextLink;
   }

}
//end of Node.java

//QueueArray.java

/*

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() {
       elements = (T[])new Object[CAPACITY];
       rearIndex = -1;
   }

   public QueueArray(int size) {
       elements = (T[])new Object[size];
       rearIndex = -1;  
   }

   public T dequeue() {
      
       if(isEmpty())
           return null;
       T data = elements[0];
       for(int i=0;i<rearIndex;i++)
           elements[i] = elements[i+1];
       rearIndex--;
       return data;
   }

   public void enqueue(T info) {
       if(!isFull())
       {
           rearIndex++;
           elements[rearIndex] = info;
       }
   }

   public boolean isEmpty() {
       return(rearIndex == -1);
   }

   public boolean isFull() {
       return(size() == elements.length);
   }

   public int size() {
       return rearIndex+1;
   }
  
}
//end of QueueArray.java

// QueueLinkedList.java
/*

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() {
       front = null;
       rear = null;
       size = 0;

   }

   public void enqueue(T info) {
       Node node = new Node(info);
       if(isEmpty())
       {
           front = node;
           rear = node;
       }else
       {
           rear.setNextLink(node);
           rear = node;
       }
       size++;
   }

   public T dequeue() {
       if(!isEmpty())
       {
           T data = (T) front.getInfo();
           front = front.getNextLink();
           if(front == null)
               rear = null;
           size--;
          
           return data;
       }      
      
       return null;
   }

   public boolean isEmpty() {
       return(front == null);
   }

   public int size() {
       return size;
   }
}
//end of QueueLinkedList.java

//StackLinkedList.java

/*

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() {
       topPointer = null;
       size=0;
   }

   public void push(T info) {
       Node node = new Node(info);
       if(isEmpty())
           topPointer = node;
       else
       {
           node.setNextLink(topPointer);
           topPointer = node;
       }
       size++;
   }

   public T pop() {
       if(!isEmpty())
       {
           T data = (T) topPointer.getInfo();
           topPointer = topPointer.getNextLink();
           size--;
           return data;
       }
      
       return null;
   }

   public T top() {
       if(!isEmpty())
           return (T) topPointer.getInfo();
       return null;
   }

   public boolean isEmpty() {
       return(topPointer == null);

   }

   public int size() {
       return size;
   }
}
//end of StackLinkedList.java

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

   System.out.printf("your score is %d/60 \n", score);
   }

}
//end of Homework2Driver.java

Output:


Related Solutions

Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required -...
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for...
Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the...
Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the program to add two more threads: add a class C and a class D, and call them from main.  Does deadlock occur? import java.util.concurrent.locks.*; class A implements Runnable {             private Lock first, second;             public A(Lock first, Lock second) {                         this.first = first;                         this.second = second;             }             public void run() {                         try {                                     first.lock();                                     System.out.println("Thread A got first lock.");                                     // do something                                     try {                                                 Thread.sleep( ((int)(3*Math.random()))*1000);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT