Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Summary: Create a stack of random numbers numElements long. Display the largest integer in the stack. This is what I have so far, but am

Summary: Create a stack of random numbers numElements long. Display the largest integer in the stack. This is what I have so far, but am not able to run the code. When I try to run, it says 'select an ant operation to run'. It gives two options (default and configurable ant run) but neither works and I'm not sure why. Please help, thanks!

Long version: using the NewLinkedStack class, create an application that finds the largest integer value in the stack. Use the following approach: Prompt the user for the number of elements in a stack. Fill the stack with randomly generated integers. The topmost element is compared with the bottommost element of the stack. If the top element is greater than the bottom element, then the bottom element is removed from the stack. If the bottom element is greater than the top element, then the top element is removed from the stack. If both the elements are of same value, then either top or bottom element (your choice) is removed from the stack. The process is continued until we are left with the largest element in the stack. Display the result.

``` import java.util.Random; import java.util.Scanner;

public class NewLinkedStack implements NewStackInterface { protected LLNode top; // reference to the top of this stack

public NewLinkedStack() { top = null; }

public void push(T element) // Places element at the top of this stack. { LLNode newNode = new LLNode(element); newNode.setLink(top); top = newNode; }

public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else top = top.getLink(); }

public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. { if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else return top.getInfo(); }

public boolean isEmpty() // Returns true if this stack is empty, otherwise returns false. { return (top == null); }

public boolean isFull() // Returns false - a linked stack is never full { return false; } @Override public void popFromBottom() throws StackUnderflowException { // TODO Auto-generated method stub

} @Override public T bottom() throws StackUnderflowException { // TODO Auto-generated method stub return null; } @Override public String toString() { // TODO Auto-generated method stub return null; } @Override public int size() { // TODO Auto-generated method stub return 0; } public static void main(String[] args) { // prompt user for stack size Scanner s = new Scanner(System.in); System.out.print("How many elements do you want in the stack? "); int numElements = s.nextInt(); s.close(); // randomly generate numbers 0-999 equal to stack size for(int i = 0; i < numElementsInt; i++) { Random random = new Random(); int number = random.nextInt(999); number += 1; stack.push(number) } // print stack Symstem.out.print("Top --> " + stack); // assertThat(number).isPositive().isLessThan(999); // compare top and bot, pop smaller number while(stack.size() > 1) { Integer top = stack.top(); Integer bottom = stack.bottom(); if(top > bottom) { stack.popFromBottom(); } else if (bottom > top) { stack.pop(); } else { stack.pop(); } } // if stack is size of 1, print Symstem.out.print("Largest number: " + stack.top()); } }

```

The output should look something like this...

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions