Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java; need help with the below //push into the stack when push thing on stack, t

ID: 3682918 • Letter: J

Question

Java; need help with the below

//push into the stack when push thing on stack, the stack has to reorder //lower values have higher priority //pop higher priority first //create OrderStack class with methods: //push a value onto the stack //pop a value off the stack //and method to find out if the stack is empty //the user can enter as many items as desired //when enter a dot the it prints the stack in sorted order, example 1, 3, 5, 6 //or for string A, a, b, c //stack if integer, doubles, and strings -- always in order //The items list have to be comparable, means that class will be //OrderStack> import java.util.*; //Scanner; import java.io.*; public class OrderStack> { public static void main(String args[]) throws IOException { Scanner scanner = new Scanner( System.in ); String inputVal = scanner.nextLine(); System.out.println(); choice(); //begin do -- while loop until user inputs exit do { switch(inputVal.split(" ")[0]) {case "1": { System.out.println("Integers "); String inputValI = ""; int inputValIn = 0; StackintegerStack = new Stack<>(); ArrayList integerList = new ArrayList<>(); while (inputValI.equals(".")) //don't work because is not integer butneed to work {System.out.print("Enter integer ("." when done): "); inputValIn = scanner.nextInt(); integerStack.push(inputValIn); System.out.println("pushed: " + inputValIn); // need to check if integer } //end while } break; case "2": { //Option 2 -- Doubles System.out.println("Doubles "); //need to work on it } break; case "3": { //Option 3 -- Strings System.out.println("Strings "); //need to work on it } break; case "4": { //if(inputVal.equals("exit")) break; System.out.println("DONE"); System.exit(0); //exits program } break; default: //present user with error if input is not in the menu System.out.println("That is not right."); } // end switch statement System.out.println(); choice(); System.out.println(); } while (!inputVal.equals("4")); System.out.println("Done!"); scanner.close(); } public static void choice() { System.out.printf("choice%n"); System.out.printf("1. Integers%n"); System.out.printf("2. Doubles%n"); System.out.printf("3. Strings%n"); System.out.printf("4. Exit%n%n"); } } class Stack { private final ArrayList elements; // ArrayList stores stack elements public Stack() { this(10); // default stack size } public Stack(int capacity) {int initCapacity = capacity > 0 ? capacity : 10; elements = new ArrayList(initCapacity); } public void push(T pushValue) {elements.add(pushValue); } // return the top element if not empty; else throw EmptyStackException public T pop() { if (elements.isEmpty()) // if stack is empty throw new EmptyStackException("Stack is empty, cannot pop"); return elements.remove(elements.size() - 1); } } // end class Stack class EmptyStackException extends RuntimeException { public EmptyStackException() {this("Stack is empty"); } public EmptyStackException(String exception) {super(exception);}} class FullStackException extends RuntimeException {public FullStackException() { this("Stack is full");} public FullStackException(String exception) {super(exception);}}

Explanation / Answer

import java.util.ArrayList;

public class Stack {
private ArrayList<Integer> stack = null;
    int arr[] = new int[ArrayList<Integer>];
    int top = -1;
  
public Stack(){
stack = new ArrayList<Integer>();
}
  
public void push(int pushedElement){
   if (top<ArrayList<Integer>-1)
   top++ ;
   arr[top]=pushedElement;
   System.out.println("Inserted " + pushedElement + " in your stack");
stack.add(array_value);
}else
   System.out.println("Stack Overflow !")
  
public void pop() throws ExceptionForStackUnderflow{
if(!isEmpty()){
System.out.println("Poped " + stack.get(stack.size() - 1) + " from your stack");
stack.remove(stack.size() - 1);
}else{
throw new ExceptionForStackUnderflow("Stack is empty !");
}
}
  
public boolean isEmpty(){
if(stack.size() == 0) return true;
else return false;
}
  
public int top() throws ExceptionForStackUnderflow{
if(!isEmpty()){
return stack.get(stack.size() - 1);
}else{
throw new ExceptionForStackUnderflow("Stack is empty !");
}
}
  
public static void main(String[] args) {
Stack st = new Stack();
st.push(1);
st.push(2);
st.push(3);
   st.push(4);
   st.push(5);
   st.push(6);
   st.push(7);
   st.push(8);
   st.push(9);
   st.push(10);
  
try{
System.out.println("Top entry in stack :" + st.top());
}
for (i=0; i<ArrayList; i++)
{
System.out.println("sorted list is" + ArrayList[i])
}


catch(ExceptionForStackUnderflow e){
System.out.println(e.getResponse());
}
  
try{
st.pop();
st.pop();
st.pop();
st.pop();
   st.pop();
   st.pop();
   st.pop();
   st.pop();
   st.pop();
   st.pop();
}catch(ExceptionForStackUnderflow e){
System.out.println(e.getResponse());
}
}
}


//ExceptionForStackUnderflow class

public class ExceptionForStackUnderflow extends Exception{
private static final long serialVersionUID = 1L;

public ExceptionForStackUnderflow(){
super();
}

public ExceptionForStackUnderflow(String respponse){
super(respponse);
}

public ExceptionForStackUnderflow(Throwable th){
super(th);
}

public ExceptionForStackUnderflow(String respponse, Throwable th){
super(response, th);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote