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

School of Biomedical Engineering Jimma Institute of Technology Jimma University

The document provides instructions for a final laboratory assignment to simulate the operation of a stack data structure using Java. It includes details on creating interface and class files to define a stack and implement it using an array. It also provides sample code and instructions to test the stack implementation. The student is to build a menu-driven program to push and pop items from the stack and display the stack's contents. Screenshots of the interfaces are required to demonstrate the simulation functionality.

Uploaded by

Hanan Kemal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

School of Biomedical Engineering Jimma Institute of Technology Jimma University

The document provides instructions for a final laboratory assignment to simulate the operation of a stack data structure using Java. It includes details on creating interface and class files to define a stack and implement it using an array. It also provides sample code and instructions to test the stack implementation. The student is to build a menu-driven program to push and pop items from the stack and display the stack's contents. Screenshots of the interfaces are required to demonstrate the simulation functionality.

Uploaded by

Hanan Kemal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SCHOOL OF BIOMEDICAL ENGINEERING

JIMMA INSTITUTE OF TECHNOLOGY


JIMMA UNIVERSITY

ECEG4171: DATA STRUCTURES AND ALGORITHMS

FINAL LABORATORY #2
SIMULATE THE OPERATION OF THE STACK ADT

Student Number Student Name Signature

Prepared by:

Engr. Kris Calpotura, MSME, MIT


Asst. Professor, FECE – Computer Stream
Final Laboratory Instructions:
1. Safety procedures must be taken into consideration in each LAB.
2. This LAB contributes to the successful completion of the course requirements.
3. Peer groups will be utilized for the conduct of this LAB.
4. Deadline for the submission of this LAB will be a week before the last week of the semester.

Final Laboratory 2: Simulate the operation of a Stack ADT

Individual Assessment 40%

No Aspect for Evaluation Mark

1 Health & Safety Housekeeping and organization 5%

2 Participation/ Work independently/ Oral questions 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 Calculations /Codes/ Theory. 5%

3 Data collection/ Findings / Observations 5%

4 Diagrams / Charts / Figures and Plots with Captions 10%

4 Analysis & Discussion / (theory vs actual) 15%

5 Conclusions/ Summary /self-reflection 15%

6 Quality of work performed including quality of lab report, 5%


neatness etc.
INTRODUCTION
A Stack is an Abstract Data Type (ADT) that supports the following methods:
push(obj) Add object obj at the top of the stack.
Input: Object; Output: None.
obj pop() Delete an item from the top of the stack and returns object obj; an error
occurs if he stack is empty.
Input: None; Output: Object.
obj peek() Returns the top object obj on the stack , without removing it; an error occurs
if the stack is empty.
Input: None; Output: Object.
boolean isEmpty() Returns a boolean indicating if the stack is empty.
Input: None; Output: boolean (true or false).
int size() Returns the number of items on the stack.
Input: None; Output: integer.
Type Object may be any type that can be stored in the stack. The actual type of the object will be
provided by the user.
MACHINE PROBLEM
Write Java programs to implement the STACK using an array.
Procedures
1. Open your Java IDE (NetBeans). Click on File – New Project. Under Categories, select Java –
Java Application. Click Next and name your project as FinalLaboratory2PeerGroupXX. This
will create you a new java project with your main class. Right click your package name
(finallaboratory2peergroupXX) and create a new java interface. Name your interface class as
Stack.java
Capture your screen after accomplishing this task:
2. Write the following code on the interface class.
public interface Stack
{
public void push(Object ob);
public Object pop();
public Object peek();
public boolean isEmpty();
public int size();
}
Capture your screen after accomplishing this task:

The actions push, pop, peek, empty, and size are simply translated into specifications for
methods called push(), pop(), peek(), isEmpty(), and size(). These are the standard names for
stack operations. Each method is specified by describing its return value as well as any
modifications it makes to the object.
3. The Stack interface may be implemented in a variety of ways. The most basic method is to
utilize an ordinary array. The ArrayStack implementation stores stack components in an array
a[]. Its other data field is the number top, which corresponds to the stack's top member. The
top is also used to tally the number of things currently in the stack. Create a new Java class and
name it ArrayStack to implement this stack interface. java.
4. Write the following code on your java class:
public class ArrayStack implements Stack
{
private Object a[];
private int top;

public ArrayStack(int n)
{
a = new Object[n];
top = -1;
}

public void push(Object item)


{
if(top == a.length-1)
{
System.out.println("Stack is full");
return;
}
top++;
a[top] = item;
}

public Object pop()


{
if( isEmpty() )
{
System.out.println("Stack is empty");
return null;
}
Object item = a[top];
top--;
return item;
}

public Object peek()


{
if( isEmpty() ) return null;
return a[top];
}

public boolean isEmpty()


{
return (top == -1);
}

public int size()


{
return top+1;
}
}
Capture your screen after accomplishing this task:
The constructor generates a new stack of the provided size, n. The index of the item at the top
of the stack is stored in the variable top. The push() function raises top to refer to the space
immediately above the previous top and saves a data item there. It's worth noting that top is
increased before the object is installed. The pop() function decrements top after returning the
value at top. This essentially removes the item off the stack; it is no longer accessible, despite
the fact that the value remains in the array (until another item is pushed into the cell). The
peek() function just returns the item at the top of the stack without altering the stack. The Stack
interface's criteria for the pop() and peek() functions demand that the stack not be empty. If the
stack is empty, the isEmpty() function returns true. If the stack is empty, the top variable is set
to -1.
5. Test your Stack ADT (ArrayStack.java) by invoking the java class to your main class
(FinalLaboratory2PeerGroupXX.java). Write the code below on the main class.
public class FinalLaboratory2PeerGroupXX
{
public static void main(String[] args)
{
ArrayStack stk = new ArrayStack(4);
Object item;
stk.push('A');
stk.push('B');
stk.push('C');
System.out.println("size(): "+ stk.size());
item = stk.pop();
System.out.println(item + " is deleted");
stk.push('D');
stk.push('E');
stk.push('F');
System.out.println(stk.pop() + " is deleted");
stk.push('G');
item = stk.peek();
System.out.println(item + " is on top of stack");
}
}
Capture your screen after accomplishing this task:
6. Save your file.
Questions
1. What are the different operations of the Stack ADT?
2. Run FinalLaboratory2PeerGroupXX.java, what is the output?
Answer to Questions
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 Push Menu


Options:
[1] Enter item to PUSH
[2] Show contents of STACK
[3] Go Back to Main Menu
Choice: _____

The Pop Menu


Options: Message can be “Stack is
[1] Pop an Item of the STACK EMPTY” or “An item on the
[2] Show contents of STACK STACK was popped” if the user
[3] Go Back to Main Menu selects 1.
Choice: _____
Message:

The Push Interface


Message can be “Stack is FULL”
or “An item was pushed on the
Enter item to PUSH: ____
STACK”
Message:
Push an item again? [Y/N] ___

The Contents of the Stack This interface is available on both


Contents: _____________________________ the PUSH and POP Menu
Size: ___________
Go Back to the previous interface? [Y/N] ____
8.3.2 Interface Flow
MM

PuM 3 PoM

3 2 1 1 2 3

SSI PI SSI

MM: Main Menu


PuM: Push Menu
PoM: Pop Menu
SS: Show Stack Interface
PI: Push Interface
Source Code for your Main Class
Screenshots of the Interfaces (explain functionality of each interface)

You might also like