Java
Enter the stack of weights and write a program that determines if the stack is study. If weights maintain an increasing order from top to bottom, stack is defined as studdy.
Stack<Integer> s = new Stack<Integer>();
s.push(100); s.push(75); s.push(50); s.push(25); s.push(5);
…
s = new Stack<Integer>();
s.push(100); s.push(50); s.push(75); s.push(25); s.push(5);
…
public boolean isSturdy(Stack<Integer> stack) {
…
return false;
}
import java.util.Stack;
public class SturdyStack {
public boolean isSturdy(Stack<Integer> stack) {
if(stack.isEmpty()) {
return true;
}
int num = stack.pop();
while (!stack.isEmpty()) {
if(stack.peek() < num) {
return false;
}
num = stack.pop();
}
return true;
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<Integer>();
s.push(100); s.push(75); s.push(50); s.push(25); s.push(5);
System.out.println(new SturdyStack().isSturdy(s));
s = new Stack<Integer>();
s.push(100); s.push(50); s.push(75); s.push(25); s.push(5);
System.out.println(new SturdyStack().isSturdy(s));
}
}
Java Enter the stack of weights and write a program that determines if the stack is...