Please I`m trying to run this codes from the command line, but it keep telling m
ID: 3662332 • Letter: P
Question
Please I`m trying to run this codes from the command line, but it keep telling me that class Arrarystack is public and should be declare on a file name Arraystack.java.
please can someone take a look and explain how to runs it on netbean. I really need to see the output of the codes.
class MyStack {
private int top;
private int data[];
//constructor
MyStack(int size) {
top = -1;
data = new int[size];
}
//pushing new item to array stack
void push(int val) {
if (top == data.length - 1) {
System.out.println("Stack OverFlow");
} else {
data[++top] = val;
System.out.println("Pushed item :" + data[top]);
}
}
//removing top most element
int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
System.out.println("Popped Item : " + data[top]);
return data[top--];
}
}
}
public class ArrayStack {
public static void main(String args[]){
//new MyStack object created
MyStack stk = new MyStack(3);
//pushing data to stack
stk.push(2);
stk.push(4);
stk.push(6);
stk.push(8);
//popping data from stack
stk.pop();
stk.pop();
stk.pop();
stk.pop();
}
}
Explanation / Answer
You have to save this program as ArrayStack.java
Because the main method is in the class named ArrayStack.
Retry to execute it as ArrayStack.java