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

Identify the directives and statements in the following program. What is the output of the program?...
Identify the directives and statements in the following program. What is the output of the program? [2 points] #include <stdio.h> int main (void) { printf(“Parkinson’s Law: \n Work expands so as to \t”); If i and j are positive integers, does (-i)/j always have the same value as –(i/j)? Justify your answer. [2 points] printf(“fill the time \n”); Supply parenthesis to show how a C compiler would interpret the following expressions: [2 points] a * b – c *d +...
1. What does the following program do? 2. What output does the program produce? 3. Examine...
1. What does the following program do? 2. What output does the program produce? 3. Examine the program code carefully. Is the program functioning correctly? 4. If you do not think the program is working correctly, describe why? 5. Include one screenshot of the program's output. C++ PROGRAM: #include <iostream> #include <pthread.h> #include <stdlib.h> int count; void* myFunction(void* arg) { int actual_arg = *((int*) arg);    for(unsigned int i = 0; i < 10; ++i) { count++; std::cout << "Thread...
Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
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.
(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;...
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...
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
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 ?  ...
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:...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT