Question: What is the output when this java code is run: import java.util. * ; public class LabQuiz 1 { @SuppressWarnings ( unchecked )

What is the output when this java code is run:
import java.util.*;
public class LabQuiz1{
@SuppressWarnings("unchecked")
public static void main(String[] args){
// A stack is given with the following inputs
Stack stack = new Stack<>();
Random r = new Random();
int max =100;
int min =1;
for (int i =0; i <30; i++){
stack.push(r.nextInt((max - min)+1)+ min);
}
System.out.println("your origin stack");
System.out.println(stack +" size is: "+ stack.capacity());
Stack mystack1=(Stack) stack.clone();
Stack mystack2=(Stack) stack.clone();
Stack mystack3=(Stack) stack.clone();
System.out.println("Sorted Stack");
System.out.println(sortedStack(mystack1));
// write a program to calculate the frequency of your stack with hashmap.
System.out.println("print out the results for your hashmap");
count(mystack2).forEach((key, value)-> System.out.println(key +""+ value));
// write a program to put your stack into a hashset.
System.out.println("Print out the results for your hashset");
HashSet hset = stackTohashSet(mystack3);
System.out.println(hset +" size is: "+ hset.size());
}
public static Stack sortedStack(Stack stack){
Stack sortedStack = new Stack<>();
while (!stack.isEmpty()){
int temp = stack.pop();
while (!sortedStack.isEmpty() && sortedStack.peek()> temp){
stack.push(sortedStack.pop());
}
sortedStack.push(temp);
}
return sortedStack;
}
public static HashMap count(Stack stack){
HashMap hmap = new HashMap<>();
for (int num : stack){
hmap.put(num, hmap.getOrDefault(num,0)+1);
}
return hmap;
}
public static HashSet stackTohashSet(Stack stack){
return new HashSet<>(stack);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!