Can someone help me with this assignment; Write a Java class (MyStackAssignment)
ID: 3786432 • Letter: C
Question
Can someone help me with this assignment; Write a Java class (MyStackAssignment) to emulate a stack structure using an array. Provide methods for push(), pop(), isEmpty(), peek(), and size(). Use the postfix expression in the text (given below) to test your logic for evaluating the postfix form of the expression..
infix:: (34 * 42 - (2 + 5) ) * 4 / 2 postfix: 3 4 * 2 5 + - 4 * 2 /
I need to be able to use any numbers (32, 100 or 1349 for example. Not just from 0-9 )Be sure to avoid error situations such as popping an empty stack, and the stack holding more than one item at the completion of the program. (Note: you should pop() he last value on the stack and display it as the final result, This should result in an empty stack).
Explanation / Answer
import java.util.*;
public class myclassAssignment {
static Scanner sc=new Scanner(System.in);
static int[] stack;
static int size;
static int top;
static{
size=0;
top=-1;
}
public static void main(String[] args) {
System.out.println("please enter stack capacity:");
size=sc.nextInt();
stack=new int[size];
System.out.println("please enter your choice:");
System.out.println("1.push 2.pop 3.isempty 4. peep 5.size 6.exit");
while(true){
System.out.println("please enter your choice:");
int ch=sc.nextInt();
switch(ch){
case 1:push(stack);
break;
case 2:pop();
break;
case 3:boolean flag=isempty(stack);
System.out.println("is stack empty :"+flag);
break;
case 4:peep(stack);
break;
case 5:int val=findsize(stack);
System.out.println("size of stack now is"+val);
break;
case 6:System.exit(0);
}
}
}
private static int findsize(int stack[]) {
return top;
}
private static void peep(int[] stack) {
for(int i=0;i<=top;i++)
System.out.println(stack[i]);
}
private static boolean isempty(int[] stack) {
if(top==-1)
return true;
else
return false;
}
private static int pop() {
if(top==-1)
System.out.println("stack is underflow:");
return stack[top--];
}
private static int[] push(int stack[]) {
System.out.println("enter element:");
int element=sc.nextInt();
if(top<size-1){
++top;
stack[top]=element;
}else{
System.out.println("stack is overflow:");
}
return stack;
}
}
output:
please enter stack capacity:
3
please enter your choice:
1.push
2.pop
3.isempty
4. peep
5.size
6.exit
please enter your choice:
1
enter element:
10
please enter your choice:
1
enter element:
20
please enter your choice:
5
size of stack now is1
please enter your choice:
4
10
20
please enter your choice:
3
is stack empty :false
please enter your choice:
2
please enter your choice:
5
size of stack now is0
please enter your choice:
4
10
please enter your choice:
3
is stack empty :false
please enter your choice:
2
please enter your choice:
5
size of stack now is-1
please enter your choice:
4
please enter your choice:
3
is stack empty :true
please enter your choice:
6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.