In: Computer Science
import java.util.Stack;
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Stack<Integer> new_stack =
new Stack<>();/* Start with the empty stack */
Scanner scan = new
Scanner(System.in);
int num;
for (int i=0; i<10; i++){//Read
values
num =
scan.nextInt();
new_stack.push(num);
}
System.out.println(""+getAvg(new_stack));
}
public static int getAvg(Stack s) {
//TODO: Find the average of the
elements in the stack
//If the average is a decimal
number, return only the integer part. Example: if average=10.8,
return 10
//Example: if s=[10, 20, 30, 40,
50[, the method should return: 30
}
}
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args) { Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */ Scanner scan = new Scanner(System.in); int num; for (int i=0; i<10; i++){//Read values num = scan.nextInt(); new_stack.push(num); } System.out.println(""+getAvg(new_stack)); } public static int getAvg(Stack s) { //TODO: Find the average of the elements in the stack //If the average is a decimal number, return only the integer part. Example: if average=10.8, return 10 //Example: if s=[10, 20, 30, 40, 50[, the method should return: 30 int sum = 0; int count = 0; while (!s.isEmpty()){ sum += Integer.parseInt(String.valueOf(s.pop())); count += 1; } return sum/count; } }