Question

In: Computer Science

- What is the output of the following program?                      StackPT stk1 = new ArrayStackPT( 10...


- What is the output of the following program?                     

StackPT stk1 = new ArrayStackPT( 10 );

StackPT stk2 = new ArrayStackPT( 10 );

StackPT stk3 = new ArrayStackPT( 10 );

StackPT stk4 = new ArrayStackPT( 10 );

QueuePT q1 = new ArrayQueuePT(10);

int n = 12;

while (n > 0){

  stk1.push(n%2);

n = n/2;

}

String result = "";

while (! stk1.isEmpty()){

result += stk1.pop()+ " ";

}


System.out.println("the output of stk1 : "+result); //___________

for(int i =0; i<10; i++){

stk3.push(i);

}  

stk2.push(stk3.pop());

stk2.push(stk3.pop());

stk2.push(stk3.pop());

result = "";

while (! stk2.isEmpty()){

result += stk2.pop()+ " ";          

}

System.out.println("the output of stk2 : "+result); //___________

for(int i = 0; i < 10; i++){

if(i%4==0)

stk4.push( i + stk3.pop() );

}

result = "";

while (! stk4.isEmpty()){

result += stk4.pop()+ " ";  

}

System.out.println("the output of stk4 : "+result); //___________

int limit = 5;

for(int i = 0; i < limit; i++) {

q1.push(Math.pow (i, i));

q2.push((limit - i)*(limit - i));

}

result = "";

while (! q1.isEmpty()){

result += q1.pop()+ " ";  

}

System.out.println("the output of q1 : "+result); //___________


Solutions

Expert Solution

code:

import java.util.LinkedList; // importing header file
import java.util.Queue;
import java.io.*;
import java.util.*;

class Main{
   public static void main(String args[]){
       Stack<Integer> stk1=new Stack<Integer>();    //Object declaration
       Stack<Integer> stk2=new Stack<Integer>();
       Stack<Integer> stk3=new Stack<Integer>();
       Stack<Integer> stk4=new Stack<Integer>();
    
       Queue<Double> q1 = new LinkedList<Double>();
       Queue<Integer> q2 = new LinkedList<Integer>();
    
       Integer n=new Integer(12);
       while(n>0){
           stk1.push(n%2);
           n=n/2;
       }
       String result="";
       while(!stk1.isEmpty()){
           result+=stk1.pop()+" ";
       }
       System.out.println("the output of stk1: "+result);   //printing the Stack 1 result
    
       for(int i=0;i<10;i++){
           stk3.push(i);
       }
       stk2.push(stk3.pop());
       stk2.push(stk3.pop());
       stk2.push(stk3.pop());
    
       result="";
       while(!stk2.isEmpty()){
           result+=stk2.pop()+" ";
       }
       System.out.println("the output of stk2: "+result);   //printing stack 2 result
    
       for(int i=0;i<10;i++){
           if(i%4==0)
               stk4.push(i+stk3.pop());
       }
       result="";
       while(!stk4.isEmpty()){
           result+=stk4.pop()+" ";
       }
       System.out.println("the output of stk4: "+result);       //printing stack 4 result
    
       Integer limit=new Integer(5);
       for(int i=0;i<limit;i++){
           q1.add(Math.pow(i,i));
           q2.add((limit-i)*(limit-i));
       }
       result="";
       while(!q1.isEmpty()){
           result+=q1.remove()+" ";
       }
       System.out.println("the output of q1: "+result);         //printing Queue result
   }
}

output:


Related Solutions

In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
A teacher instituted a new reading program at school. After 10 weeks in the​ program, it...
A teacher instituted a new reading program at school. After 10 weeks in the​ program, it was found that the mean reading speed of a random sample of 20 second grade students was 94.4 wpm. What might you conclude based on this​ result? Select the correct choice below and fill in the answer boxes within your choice. ​(Type integers or decimals rounded to four decimal places as​ needed.) A. A mean reading rate of 94.4 wpm is not unusual since...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output the largest of those numbers, you can assume any length for those numbers. 80x86 assembly language
trace through the program and what the output would be. If there is an error explain...
trace through the program and what the output would be. If there is an error explain what to change. #include <iostream> using namespace std; int fun(int c, int b); int main(){ int a = 0, b= 5, c = 10; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; b=fun(a, c); cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; while(b==21){ int a = 3; b = a; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; } cout<<"a is:...
Question 5 (10 marks) Python Language What is the output of the following code ? (2...
Question 5 Python Language What is the output of the following code ? (2 points) a, b = 0, 1 while b < 10: print b a, b = b, a+b B. Explain List Comprehension (2 points) Given v = [1 3 5] w = [ [2*x, x**2] for x in v] What is the content of w? c. What is tuple ?   What is the difference between tuple and list ? (2 points) D. What is a module ?  ...
What is the expected output from the following program (3 answers) ______­­ ______namespace std; double insurance(int);...
What is the expected output from the following program (3 answers) ______­­ ______namespace std; double insurance(int); void main() { int j; ______ mileage; ______ monthly_rent; for (j=______ j<4; j++) { mileage=1000*j; monthly_rent= 0.3*mileage + insurance(mileage); printf("Monthly rent for %4d.2f is : $ ______ . \n", mileage, monthly_rent); } } double insurance(int miles) { double mileage_charge; if (miles<=1000) { mileage_charge=100.0; }___ if ((miles>1000) && (miles<=2000)) { mileage_charge=150.0; }; ___ (miles>2000) { mileage_charge=200.0; }; return(mileage_charge);
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
7.3 Loops: Output range with increment of 10 IN CORAL LANGUAGE zyBooks Write a program whose...
7.3 Loops: Output range with increment of 10 IN CORAL LANGUAGE zyBooks Write a program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is -15 30, the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in 20 5, the output is: Second integer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT