In: Computer Science
- 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); //___________
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: