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 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.

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

//SINCE THE QUESTION IS TO IMPLEMENT StackLinkedList AND THERE ARE ONLY 3 TESTS //ON myStack THE RESULT OF SCORE
//WILL BE 18.
/*

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.

*/
//Node class , question have no implementaion for Node class
//that's why i have created one.
class Node<T>
{
   public T val;
   public Node next;
   Node(T val)
   {
       this.val = val;
       next = null;
   }
}
public class StackLinkedList<T>
{

   private Node topPointer;

   private int size;

   public StackLinkedList()
   {
       topPointer = null;
       size = 0;
   }

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

   public T pop()
   {
       if(topPointer == null)
       {
           return null;
       }
       else
       {
           Node temp = topPointer;
           topPointer = topPointer.next;
           size--;
           return (T)temp.val;
       }
   }

   public T top(){
      
       if(topPointer == null)
       {
           return null;
       }
       else
       {
           return (T)topPointer.val;
       }

   }

   public boolean isEmpty()
   {
       return size == 0?true:false;
   }

   public int size()
   {
       return size;
   }

}

//---------------- 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;
          
       }
       //SINCE THE QUESTION IS TO IMPLEMENT StackLinkedList AND THERE ARE ONLY 3 //TESTS ON myStack THE RESULT OF SCORE
       //WILL BE 18.
       System.out.printf("your score is %d/60 \n", score);
      

   }

}

//----------- OUTPUT ------------

//SINCE THE QUESTION IS TO IMPLEMENT StackLinkedList AND THERE ARE ONLY 3 TESTS //ON myStack THE RESULT OF SCORE
//WILL BE 18.

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 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 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:...
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...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
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 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; } /*...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT