Question

Java Enter the stack of weights and write a program that determines if the stack is...

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;

     }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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));
    }

}
Add a comment
Know the answer?
Add Answer to:
Java Enter the stack of weights and write a program that determines if the stack is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT