- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Struggling with JAVA!
Can someone of u guys plz explain what’s happening in this program, Can someone plz explain it step by step, Thx in advance :)
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println(“Stack is full.”);
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println(“Stack underflow.”);
return 0;
}
else
return stck[tos–];
}
}
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println(“Stack in mystack1:”);
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println(“Stack in mystack2:”);
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
OUTPUT OF THIS PROGRAM!
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14
13
12
11
10
Sorry, don’t think you’ll get much help here, this is a JavaScript forum.
You can try this JAVA forum -> javaprogrammingforums.com
Looks to me like potential homework answer seeker….
@kgscott284 I agree.
@samnizamani This website is good at members helping with code :: http://www.dreamincode.net/ :: but like Ben Walker mentioned, you need to show effort otherwise people will not help you as it seems you are trying to get people to do your work for you. Plus, if you read the comments //
its actually telling you what the program is doing.
I suggest moving this to [Solved]
@BenWalker, right on brother…
Marked as Solved