Wps Stack Implementation Group 2 Assignment
Wps Stack Implementation Group 2 Assignment
FINAL LABORATORY #2
GROUP 2
STUDENT NUMBER STUDENT NAME ID NUMBER SIGNATURE
1 BEDASO ENDALE RU2426/13
2 TABITA MESELE RU3706/13
3 DEJENE TECHAN RU2432/13
4 TREFE HAILE RU3064/13
5 ABDIRASHID OMAR RU3493/13
6 MISBAH ALI RU1915/13
1 Introduction/objective/procedure 5%
2 Calculation/codes/theory 5%
3 Data collection/findings/observation 5%
Page | 1
Screen capture of after creating new java interface which is called Stack.java:
Page | 2
Screens capture of after creating new java class which is called ArrayStack and the code
written in this class:
Page | 3
Screen capture after writing the code in public class FinalLaboratory2PeerGroup02:
Page | 4
ANSWER FOR THE QUESTION
Size (): 3
C is deleted
Stack is full
E is deleted
G is on top of stack
Note: The output may vary depending on the implementation and size of the stack
A stack is considered to be a linear data structure because it is based on the principle of Last In
First Out (LIFO). In a stack , elements are added and removed from the same end , called the
top of the stack. The last element added to the stack is the first one to be removed, creating a
linear order of element.
There are various stack operations that are applicable on a stack. Stack operations are generally
used to extract information and data from a stack data structure.
1. push()
Push is a function in stack definition which is used to insert data at the stack's top.
Page | 5
2. pop()
Pop is a function in the stack definition which is used to remove data from the stack's
top.
3. peek()
Peek is a function in the stack which is used to extract the element present at the stack
top.
get the top data element of the stack, without removing it.
4. isEmpty()
isEmpty is a boolean function in stack definition which is used to check whether the
stack is empty or not. It returns true if the stack is empty. Otherwise, it returns false.
5. isFull()
isFull is a function which is used to check whether the stack has reached its maximum
limit of insertion of data or not i.e. if 'maxLimit' is the maximum number of elements
that can be stored in the stack and if there are exactly maxLimit number of elements
present in the stack currently, then the function isFull() returns true. Otherwise, if the
number of elements present in the stack currently are less than 'maxLimit',
then isFull() returns false.
6. size()
Size is a function in stack definition which is used to find out the number of elements
that are present inside the stack.
returns the number of items in the stack.
Page | 6
CASE STUDY
Using all the java classes, simulate the operation of the Stack ADT.
Wireframes
Simulation of Stack
Main Menu
Options:
Choice: ____
Push an item again? [Y/N] ___ Go Back to the previous interface? [Y/N] ____
Page | 7
Message can be “Stack is EMPTY” or “An item on the STACK was popped” if the user
selects 1 from The Pop Menu.
Message can be “Stack is FULL” or “An item was pushed on the STACK” from The push
Interface.
DisplayStack interface is available in both push menu and pop menu.
This is a Java program that implements a stack data structure. The stack is a data structure that
works on the principle of "last in, first out" (LIFO). In the beginning, the program asks the user
for the size of the stack.
The program contains a loop, which displays a menu of options for the user. The user can
choose to push an item onto the stack, pop an item from the stack, or exit the program.
The pushMenu function provides options for adding an item to the stack. The popMenu
function allows the user to remove an item from the stack. Both pushMenu and popMenu have
options for displaying the contents of the stack.
The displayStack function displays the current contents of the stack, its size, and offers the user
the option to go back to the previous interface.
Finally, the main function contains a loop that keeps the program running until the user
chooses to exit. Overall, this program provides a basic implementation of the stack data
structure in Java.
Here, we are going to implement stack using arrays, which makes it a dynamic size stack
implementation.
Page | 8
Screen capture of the code
Page | 9
Page | 10
package finallaboratory2peergroup02;
import java.util.Scanner;
int size=input.nextInt();
stack=new int[size];
while (true) {
System.out.println("\tMain Menu");
System.out.println("Options:");
System.out.print("Choice:");
PushMenu(input);
Page | 11
break;
case 2:
PopMenu(input);
break;
case 3:
System.exit(0);
break;
default:
break;
while (true) {
System.out.println("\tPush Menu");
System.out.println("Options:");
Page | 12
System.out.println("[3] Go Back to Main Menu");
System.out.print("Choice:");
switch (choice) {
case 1:
if (top == stack.length-1) {
System.out.println("Stack is FULL");
break;
stack[++top]=item;
input.nextLine();
String ch = input.nextLine().toUpperCase();
if (!ch.equals("Y"))
return;
Page | 13
break;
case 2:
DisplayStack();
break;
case 3:
return;
default:
break;
while (true) {
System.out.println("\tPop Menu");
System.out.println("Options:");
System.out.print("Choice:");
Page | 14
int choice = input.nextInt();
switch (choice) {
case 1:
if (top == -1) {
System.out.println("Stack is EMPTY");
break;
input.nextLine();
String answer;
answer = input.nextLine().toUpperCase();
if (!answer.equals("Y"))
return;
break;
case 2:
DisplayStack();
Page | 15
break;
case 3:
return;
default:
break;
if (top == -1) {
System.out.println("Stack is EMPTY");
return;
System.out.println("Contents");
System.out.println(stack[i]);
Page | 16
Scanner input;
String ch = input.nextLine().toUpperCase();
if (!ch.equals("Y")) {
Page | 17