0% found this document useful (0 votes)
15 views

Wps Stack Implementation Group 2 Assignment

This document describes a simulation of a stack data structure using Java. It includes: 1) Wireframes of the menus for pushing and popping items from the stack, including options to view the stack contents. 2) Code implementing the stack as an array, with functions for pushing, popping, and displaying the stack. 3) A main method containing a loop that runs the push and pop menus until the user exits. This provides an interactive simulation of a stack in Java.

Uploaded by

Bedaso Endale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Wps Stack Implementation Group 2 Assignment

This document describes a simulation of a stack data structure using Java. It includes: 1) Wireframes of the menus for pushing and popping items from the stack, including options to view the stack contents. 2) Code implementing the stack as an array, with functions for pushing, popping, and displaying the stack. 3) A main method containing a loop that runs the push and pop menus until the user exits. This provides an interactive simulation of a stack in Java.

Uploaded by

Bedaso Endale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JIMMA UNIVERSITY

JIMMA INSTITUTE OF TECHNOLOGY

SCHOOL OF BIOMEDICAL ENGINEERING

ECEG4171: DATA STRUCTURES AND ALGORITHMS

FINAL LABORATORY #2

SIMULATE THE OPERATION OF THE STACK ADT

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

SUBMITTED TO: ENGR.KRIS CALPOTURA , MSME ,MIT

ASST. PROFESSOR , FECE – COMPUTER STREAM

SUBMISSION DATE: APRIL 26, 2023


Final laboratory 2: simulate the operation of stack ADT

Individual Assessment 40%

No Aspect For Evaluation Mark

1 Healthy and safety housekeeping and organization 5%

2 Participation/work independent / oral organization 10%

3 Set up the IDE 10%

4 Troubleshooting and problem solving 10%

5 Time management/late/finish on time 5%

Group Assessment 60%

1 Introduction/objective/procedure 5%

2 Calculation/codes/theory 5%

3 Data collection/findings/observation 5%

4 Diagrams /chairs/figures and plots with captions 10%

5 Analysis and discussion /(theory Vs actual) 15%

6 Conclusions /summary/ self-reflection 15%

7 Quality of work performed including quality of lab report , neatness etc 5%

Page | 1
Screen capture of after creating new java interface which is called Stack.java:

Screen capture of after writing the code in public interface Stack:

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:

Screen capture of output after running FinalLaboratory2PeerGroup02:

Page | 4
ANSWER FOR THE QUESTION

# 1. Run FinalLaboratory2PeerGroup02.java, what is the output?

The output of FinalLaboratory2PeerGroup02.java is:

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

# 2. what are the different operation of the Stack ADT?

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.

Some of the stack operations are given below.

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:

[1] Push an Item

[2] Pop an Item

[3] Exit the Program

Choice: ____

The Pop Menu


The Push Menu
Options:
Options:
[1] Pop an Item of the STACK
[1] Enter item to PUSH
[2] Show contents of STACK
[2] Show contents of STACK
[3] Go Back to Main Menu
[3] Go Back to Main Menu
Choice: _____
Choice: _____
Message:

The Push Interface The Contents of the Stack

Enter item to PUSH: ____ Contents: _____________________________

Message: Size: ___________

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;

public class FinalLaboratory2PeerGroup02 {

private static int[] stack;

private static int top = -1;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the size of the stack : ");

int size=input.nextInt();

stack=new int[size];

while (true) {

System.out.println("\tMain Menu");

System.out.println("Options:");

System.out.println("[1] Push an Item");

System.out.println("[2] Pop an Item");

System.out.println("[3] Exit the Program");

System.out.print("Choice:");

int choice = input.nextInt();

switch (choice) {case 1:

PushMenu(input);

Page | 11
break;

case 2:

PopMenu(input);

break;

case 3:

System.out.println("Exiting program ");

System.exit(0);

break;

default:

System.out.println("Invalid choice,please try again.");

break;

private static void PushMenu(Scanner input) {

while (true) {

System.out.println("\tPush Menu");

System.out.println("Options:");

System.out.println("[1] Enter item to PUSH");

System.out.println("[2] Show contents of STACK");

Page | 12
System.out.println("[3] Go Back to Main Menu");

System.out.print("Choice:");

int choice = input.nextInt();

switch (choice) {

case 1:

if (top == stack.length-1) {

System.out.println("Stack is FULL");

break;

System.out.print("Enter item to PUSH: ");

int item = input.nextInt();

stack[++top]=item;

System.out.println("AN ITEM WAS PUSHED ON THE STACK is " + item);

System.out.print("PUSH AN ITEM AGAIN? [Y/N] ");

input.nextLine();

String ch = input.nextLine().toUpperCase();

if (!ch.equals("Y"))

return;

Page | 13
break;

case 2:

DisplayStack();

break;

case 3:

return;

default:

System.out.println("Invalid choice, please try again.");

break;

private static void PopMenu(Scanner input) {

while (true) {

System.out.println("\tPop Menu");

System.out.println("Options:");

System.out.println("[1] Pop an Item of the STACK");

System.out.println("[2] Show contents of STACK");

System.out.println("[3] Go Back to Main Menu");

System.out.print("Choice:");

Page | 14
int choice = input.nextInt();

switch (choice) {

case 1:

if (top == -1) {

System.out.println("Stack is EMPTY");

break;

int item = stack[top--];

System.out.println("AN item on the STACK was popped is " + item);

System.out.print("Pop an item again? [Y/N] ");

input.nextLine();

String answer;

answer = input.nextLine().toUpperCase();

if (!answer.equals("Y"))

return;

break;

case 2:

DisplayStack();

Page | 15
break;

case 3:

return;

default:

System.out.println("Invalid choice,please try again.");

break;

private static void DisplayStack() {

if (top == -1) {

System.out.println("Stack is EMPTY");

return;

System.out.println("\tThe Contents of the Stack");

System.out.println("Contents");

for (int i = top; i >= 0; i--) {

System.out.println(stack[i]);

System.out.println("Size: " + (top + 1));

Page | 16
Scanner input;

input = new Scanner(System.in);

System.out.print("Go Back to the previous interface? [Y/N] ");

String ch = input.nextLine().toUpperCase();

if (!ch.equals("Y")) {

System.out.println("Exiting stack display");

Page | 17

You might also like