Question

In: Computer Science

Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you...

Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you will need to complete some codes.

public class TestCorrectness {

   public static void main(String[] args) throws Exception {
       int queueSize = 7;
       QueueUsingStack qViaStack = new QueueUsingStack(queueSize);
       Queue queue = new Queue(queueSize);
       System.out.println("**** Enqueue Test ****");
       System.out.println();
       for (int i = 1; i <= 4; i++) {
           int x = i * 5;
           qViaStack.enqueue(x);
           queue.enqueue(x);
           System.out.println("Enqueue " + x);
           System.out.println("Stack implementation: " + qViaStack.toString());
           System.out.println("Standard implementation: " + queue.toString());
           System.out.println();
       }
       System.out.println("**** Dequeue Test ****");
       System.out.println();
       for (int i = 1; i <= 2; i++) {
           System.out.println("Stack implementation: (Dequeued " + qViaStack.dequeue() + ") " + qViaStack.toString());
           System.out.println("Standard implementation: (Dequeued " + queue.dequeue() + ") " + queue.toString());
           System.out.println();
       }
       System.out.println("**** Enqueue Test ****");
       System.out.println();
       for (int i = 1; i <= 5; i++) {
           int x = i * 7;
           qViaStack.enqueue(x);
           queue.enqueue(x);
           System.out.println("Enqueue " + x);
           System.out.println("Stack implementation: " + qViaStack.toString());
           System.out.println("Standard implementation: " + queue.toString());
           System.out.println();
       }
       System.out.println("**** Dequeue Test ****");
       System.out.println();
       for (int i = 1; i <= 7; i++) {
           System.out.println("Stack implementation: (Dequeued " + qViaStack.dequeue() + ") " + qViaStack.toString());
           System.out.println("Standard implementation: (Dequeued " + queue.dequeue() + ") " + queue.toString());
           System.out.println();
       }
   }
}

public class QueueUsingStack {
  
Stack mainStack;
int maxQueueSize;
  
public QueueUsingStack(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
mainStack = new Stack(maxQueueSize);
}
  
public void enqueue(int val) { // complete this function
}
  
public int dequeue() { // complete this function
}
  
public int getSize() { // complete this function
}
  
public String toString() {
if (getSize() == 0) {
return "[]";
} else {
String output = "[";
int n = getSize();
for (int i = 0; i < n - 1; i++) {
int x = dequeue();
output += x + ", ";
enqueue(x);
}
int x = dequeue();
output += x + "]";
enqueue(x);
return output;
}
}
}


You should get the following output.
**** Enqueue Test ****
Enqueue 5
Stack implementation: [5]
Standard implementation: [5]
Enqueue 10
Stack implementation: [5, 10]
Standard implementation: [5, 10]
Enqueue 15
Stack implementation: [5, 10, 15]
Standard implementation: [5, 10, 15]
Enqueue 20
Stack implementation: [5, 10, 15, 20]
Standard implementation: [5, 10, 15, 20]
**** Dequeue Test ****
Stack implementation: (Dequeued 5) [10, 15, 20]
Standard implementation: (Dequeued 5) [10, 15, 20]
Stack implementation: (Dequeued 10) [15, 20]
Standard implementation: (Dequeued 10) [15, 20]
**** Enqueue Test ****
Enqueue 7
Stack implementation: [15, 20, 7]
Standard implementation: [15, 20, 7]
Enqueue 14
Stack implementation: [15, 20, 7, 14]
Standard implementation: [15, 20, 7, 14]
Enqueue 21
Stack implementation: [15, 20, 7, 14, 21]
Standard implementation: [15, 20, 7, 14, 21]
Enqueue 28
Stack implementation: [15, 20, 7, 14, 21, 28]
Standard implementation: [15, 20, 7, 14, 21, 28]
2
Enqueue 35
Stack implementation: [15, 20, 7, 14, 21, 28, 35]
Standard implementation: [15, 20, 7, 14, 21, 28, 35]
**** Dequeue Test ****
Stack implementation: (Dequeued 15) [20, 7, 14, 21, 28, 35]
Standard implementation: (Dequeued 15) [20, 7, 14, 21, 28, 35]
Stack implementation: (Dequeued 20) [7, 14, 21, 28, 35]
Standard implementation: (Dequeued 20) [7, 14, 21, 28, 35]
Stack implementation: (Dequeued 7) [14, 21, 28, 35]
Standard implementation: (Dequeued 7) [14, 21, 28, 35]
Stack implementation: (Dequeued 14) [21, 28, 35]
Standard implementation: (Dequeued 14) [21, 28, 35]
Stack implementation: (Dequeued 21) [28, 35]
Standard implementation: (Dequeued 21) [28, 35]
Stack implementation: (Dequeued 28) [35]
Standard implementation: (Dequeued 28) [35]
Stack implementation: (Dequeued 35) []
Standard implementation: (Dequeued 35) []

Solutions

Expert Solution

//------------- TestCorrectness.java -------------
//there are some modifications in your code
//Since Queue is abstract class you can't instantiate it.
//so initialize queue with type LinkedList
//there are no enqueue and dequeue functions , add and remove are replaced them.
//and finally output is same for two implementations.
import java.util.*;
public class TestCorrectness
{
public static void main(String[] args) throws Exception
{
int queueSize = 7;
QueueUsingStack qViaStack = new QueueUsingStack(queueSize);
Queue queue = new LinkedList();
System.out.println("**** Enqueue Test ****");
System.out.println();
for (int i = 1; i <= 4; i++) {
int x = i * 5;
qViaStack.enqueue(x);
queue.add(x);
System.out.println("Enqueue " + x);
System.out.println("Stack implementation: " + qViaStack.toString());
System.out.println("Standard implementation: " + queue.toString());
System.out.println();
}
System.out.println("**** Dequeue Test ****");
System.out.println();
for (int i = 1; i <= 2; i++) {
System.out.println("Stack implementation: (Dequeued " + qViaStack.dequeue() + ") " + qViaStack.toString());
System.out.println("Standard implementation: (Dequeued " + queue.remove() + ") " + queue.toString());
System.out.println();
}
System.out.println("**** Enqueue Test ****");
System.out.println();
for (int i = 1; i <= 5; i++) {
int x = i * 7;
qViaStack.enqueue(x);
queue.add(x);
System.out.println("Enqueue " + x);
System.out.println("Stack implementation: " + qViaStack.toString());
System.out.println("Standard implementation: " + queue.toString());
System.out.println();
}
System.out.println("**** Dequeue Test ****");
System.out.println();
for (int i = 1; i <= 7; i++) {
System.out.println("Stack implementation: (Dequeued " + qViaStack.dequeue() + ") " + qViaStack.toString());
System.out.println("Standard implementation: (Dequeued " + queue.remove() + ") " + queue.toString());
System.out.println();
}
}
}

import java.util.*;
public class QueueUsingStack
{
  
   Stack mainStack;
   int maxQueueSize;
  
   public QueueUsingStack(int maxQueueSize) {
       this.maxQueueSize = maxQueueSize;
       //there is no constructor for Stack that takes size
       //controlling elements insertion more than the specified size
       //is taken care of enqueue method.
       mainStack = new Stack();
   }
   //enqueue(val) method
   public void enqueue(int val)
   {
       //get size of the queue
       //if there is space in queue insert.
       if(getSize()< maxQueueSize)
       {
           //first pop each value in main stack into temperory stack.
           Stack temp = new Stack();
           while(!mainStack.isEmpty())
           {
               temp.push(mainStack.pop());
           }
           //after mainStack is empty push the val passed.
           mainStack.push(val);
           //now push all the values in temp Stack to the mainStack.
           while(!temp.isEmpty())
           {
               mainStack.push(temp.pop());
           }
       }
      
   }
  
   public int dequeue()
   {
       //if mainStack is empty return 0;
       if(mainStack.isEmpty())
       {
           return 0;
       }
       //get the top value from mainStack
       int val = (int)mainStack.peek();
       //pop the value.
       mainStack.pop();
       return val;
   }
  
   public int getSize()
   {
       //use clone() method to copy values in mainStack to temp Stack
       Stack temp = (Stack)mainStack.clone();
       //count the values in temp stack by poopping each element
       int count = 0;
       while(!temp.isEmpty())
       {
           temp.pop();
           count++;
       }
       return count;
   }
  
   public String toString()
   {
       if (getSize() == 0)
       {
           return "[]";
       }
       else
       {
           String output = "[";
           int n = getSize();
           for (int i = 0; i < n - 1; i++)
           {
               int x = dequeue();
               output += x + ", ";
               enqueue(x);
           }
           int x = dequeue();
           output += x + "]";
           enqueue(x);
           return output;
       }
   }
}
/*

------------ OUTPUT ----------------

**** Enqueue Test ****

Enqueue 5
Stack implementation: [5]
Standard implementation: [5]

Enqueue 10
Stack implementation: [5, 10]
Standard implementation: [5, 10]

Enqueue 15
Stack implementation: [5, 10, 15]
Standard implementation: [5, 10, 15]

Enqueue 20
Stack implementation: [5, 10, 15, 20]
Standard implementation: [5, 10, 15, 20]

**** Dequeue Test ****

Stack implementation: (Dequeued 5) [10, 15, 20]
Standard implementation: (Dequeued 5) [10, 15, 20]

Stack implementation: (Dequeued 10) [15, 20]
Standard implementation: (Dequeued 10) [15, 20]

**** Enqueue Test ****

Enqueue 7
Stack implementation: [15, 20, 7]
Standard implementation: [15, 20, 7]

Enqueue 14
Stack implementation: [15, 20, 7, 14]
Standard implementation: [15, 20, 7, 14]

Enqueue 21
Stack implementation: [15, 20, 7, 14, 21]
Standard implementation: [15, 20, 7, 14, 21]

Enqueue 28
Stack implementation: [15, 20, 7, 14, 21, 28]
Standard implementation: [15, 20, 7, 14, 21, 28]

Enqueue 35
Stack implementation: [15, 20, 7, 14, 21, 28, 35]
Standard implementation: [15, 20, 7, 14, 21, 28, 35]

**** Dequeue Test ****

Stack implementation: (Dequeued 15) [20, 7, 14, 21, 28, 35]
Standard implementation: (Dequeued 15) [20, 7, 14, 21, 28, 35]

Stack implementation: (Dequeued 20) [7, 14, 21, 28, 35]
Standard implementation: (Dequeued 20) [7, 14, 21, 28, 35]

Stack implementation: (Dequeued 7) [14, 21, 28, 35]
Standard implementation: (Dequeued 7) [14, 21, 28, 35]

Stack implementation: (Dequeued 14) [21, 28, 35]
Standard implementation: (Dequeued 14) [21, 28, 35]

Stack implementation: (Dequeued 21) [28, 35]
Standard implementation: (Dequeued 21) [28, 35]

Stack implementation: (Dequeued 28) [35]
Standard implementation: (Dequeued 28) [35]

Stack implementation: (Dequeued 35) []
Standard implementation: (Dequeued 35) []

------------------
(program exited with code: 0)

Press any key to continue . . .

*/


Related Solutions

Find and fix the compile time bugs in the code at the end of this section....
Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here. This assignment is meant to test your attention to detail and strengthen your debugging skills. Here is the code. // Week 4 Assignment-1 // Description: Compile time errors //---------------------------------- //**begin #include files************ #include <iostream> //...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
please fix the code at the bottom to also report the percentages as well as the...
please fix the code at the bottom to also report the percentages as well as the counts. the person who did it forgot this part . the code is bellow the instructions: We will generate random values, but they should be limited to 0, 1, 2, or 3. To do this, think of a way to map the random value to a small value; there are many ways to do this, however, the way you choose must be reproducible. That...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
C++ Bank Account Error Fix, full code. I am using Dev-C++ to Compile and Execute. The...
C++ Bank Account Error Fix, full code. I am using Dev-C++ to Compile and Execute. The project is below, I have supplied the code and I'm getting an error in SavingsAccount.h file. 17   5   C:\Users\adam.brunell\Documents\Classes\C++\Week4\SavingsAccount.h   [Error] 'SavingsAccount::SavingsAccount(std::string, double, double)' is protected A.Assume i.SavingsAccount: Assume an Interest Rate of 0.03 ii.HighInterestSavings: Assume an Interest Rate of 0.05, Minimum Balance = $2500 iii.NoServiceChargeChecking: Assume an Interest Rate = 0.02, Minimum of Balance = $1000 iv.ServiceChargeChecking – Assume account service charge = $10,...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
/* What's wrong with this code? (nothing actually - but let's fix it to use a...
/* What's wrong with this code? (nothing actually - but let's fix it to use a repetition structure) Copy and paste this .txt file into jGrasp, then save it, compile it, run it. Modify it! Bring your code to class at the start of Week 8, with at least 2 output runs with different data. What the program should do is take orders(input) for three (3) octoberfest guests, using a "for" loop. (+1 pt.) Each guest will choose 1 or...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } c) Compile the code using the ARM gcc 8.2 compiler. Add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level). What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT