Question: Java Question: You are given the NewStackInterface interface with some additional abstract methods. The NewLinkedStack class implements this NewStackInterface, and hence you must implement the
Java Question:
You are given the NewStackInterface interface with some additional abstract methods. The NewLinkedStack class implements this NewStackInterface, and hence you must implement the additional methods in the NewLinkedStack class (indicated by TODO comments). The method descriptions are given below - 1. void popFromBottom() This method removes the element from the bottom of the stack; throws StackUnderflowException if the stack is empty. 2. T bottom() This method returns the element at the bottom of the stack; throws StackUnderflowException if the stack is empty. 3. String toString() This method creates and returns a nicely formatted String representation of the object. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. 4. int size() This method returns a count of how many elements are currently on the stack. 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.
I added a driver code and there are no visible errors or red x on the side telling me something is wrong. But when I try to run the code, it says "select and ant build" and both options for ant builds result in an error: unable to find an ant file to run. Why is it asking for an ant build instead of just running the .java file? I have been told to create a build.xml file and did so with code pasted below all of this, but I'm not sure how to run it or if it's correct. Thank you in advance! BTW: the pathway for the code below is C:\Users ame\Desktop\SPS\NewLinkedStack.java
Additional Directions:
1. You are given SPS folder with some java files in it. Copy the uncompressed SPS package to your eclipse workspace. Add the method implementations to NewLinkedStack class and write the driver program in the same package.
2. DO NOT make any changes to the given method headers, i.e., method return type, method name, and method parameters.
3. DO NOT add any new instance variables in the NewLinkedStack class.
4. Use only one NewLinkedStack object in the driver program. DO NOT create any copy or temporary stack.
5. You are not allowed to use any data structure other than NewLinkedStack. You are not allowed to use arrays, ArrayList, Stack, List, Node, etc.
6. You could generate random values to populate the stack. Make sure you display the (input) stack contents in the output console (see the example above).
```java
package SPS;
import java.util.Random;
public class NewLinkedStack
public NewLinkedStack() { top = null; }
public void push(T element) // Places element at the top of this stack. { LLNode
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; }
```
Driver code added below public int size() {}
```java
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()); }
}
```
Build.xml file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
