In this assignment, you are to create two solutions in Java for a stack. Data En
ID: 3678568 • Letter: I
Question
In this assignment, you are to create two solutions in Java for a stack.
Data Encapsulation
In this solution, you will need 3 classes. Place all classes in the StackTest1.java file. This means that StackData and StackOps are not public classes.
1. StackData:
a. public data for a stack
b. you can use any data structure you wish, but not the Java stack class. You must write your own implementation.
2. StackOps:
a. containing operations for the stack object.
Note: all methods for this class are static and there are no class variables.
b. Operations to include are
i. Push
ii. Pop
iii. Peek
iv. Count
v. toString
c. Include exceptions for overflow and underflow
3. StackTest1
a. Test the different operations of the stack class
Information Hiding
In this solution, you will need 2 classes. Place both classes in the StackTest2.java file. This means that Stack is a private class.
1. Stack :
a. Including private data
b. Included public methods for
i. Push
ii. Pop
iii. Peek
iv. Count
v. toString
c. Include exceptions for overflow and underflow
d. You can use any data structure you wish, but not the Java stack class. You must write your own implementation.
2. StackTest2
a. Test the different operations of the stack class
Be sure to follow all course coding and documentation standards.
Submit your 2 files (StackTest1.java and StackTest2.java) for grading.
Explanation / Answer
//StackTest1.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MDR
*/
import java.io.*;
import java.util.Scanner;
public class StackTest1 {
public static int ssize,data,selection=1,selc;
public static void main(String []args)throws IOException
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Stack Size");
ssize=scan.nextInt();
Stack s=new Stack(ssize);
while(selection==1)
{
System.out.println("enter 1.Push 2.Pop 3.Peek 4.count 5.Exit");
selc=scan.nextInt();
switch(selc)
{
case 1:System.out.print("Enter data:");
data=scan.nextInt();
System.out.println();
StackOp.push(s, data);
break;
case 2:System.out.println("deleted element is:"+StackOp.pop(s));
break;
case 3:System.out.println("top element is:"+StackOp.peek(s));
break;
case 4:System.out.println("total elements in stack is:"+StackOp.count(s));
break;
case 5:System.exit(1);
break;
default:System.out.println("Wrong OPTION");
}
}
}
}
class Stack
{
public int stack[];
public int top;
public Stack(int size)
{
stack=new int[size];
top=-1;
}
}
class StackOp
{
public static void push(Stack s,int data)
{
if(s.top==s.stack.length-1)
System.out.println("Stack overflow");
else
{
s.top++;
s.stack[s.top]=data;
}
}
public static int pop(Stack s)
{
int data=0;
if(s.top==-1)
System.out.println("UnderFlow");
else
{
data=s.stack[s.top];
s.top--;
}
return data;
}
public static int peek(Stack s)
{
int data=0;
if(s.top==-1)
System.out.println("UnderFlow");
else
{
data=s.stack[s.top];
}
return data;
}
public static int count(Stack s)
{
return s.top+1;
}
}
//StackTest1.java output:
run:
Enter Stack Size
4
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
2
UnderFlow
deleted element is:0
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:2
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:4
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
3
top element is:4
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
4
total elements in stack is:2
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:6
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:8
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:10
Stack overflow
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
5
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 4 seconds)
//StackTest2.java:
import java.util.Scanner;
import java.io.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MDR
*/
public class StackTest2 {
public static int ssize,data,selection=1,selc;
public static void main(String []args)throws IOException
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Stack Size");
ssize=scan.nextInt();
Stack s=new Stack(ssize);
while(selection==1)
{
System.out.println("enter 1.Push 2.Pop 3.Peek 4.count 5.Exit");
selc=scan.nextInt();
switch(selc)
{
case 1:System.out.print("Enter data:");
data=scan.nextInt();
System.out.println();
s.push(data);
break;
case 2:System.out.println("deleted element is:"+s.pop());
break;
case 3:System.out.println("top element is:"+s.peek());
break;
case 4:System.out.println("total elements in stack is:"+s.count());
break;
case 5:System.exit(1);
break;
default:System.out.println("Wrong OPTION");
}
}
}
}
class Stack
{
private int stack[];
private int top;
public Stack(int size)
{
stack=new int[size];
top=-1;
}
public void push(int data)
{
if(top==stack.length-1)
System.out.println("Stack overflow");
else
{
top++;
stack[top]=data;
}
}
public int pop()
{
int data=0;
if(top==-1)
System.out.println("UnderFlow");
else
{
data=stack[top];
top--;
}
return data;
}
public int peek()
{
int data=0;
if(top==-1)
System.out.println("UnderFlow");
else
{
data=stack[top];
}
return data;
}
public int count()
{
return top+1;
}
}
//Stacktest2.java output:
run:
Enter Stack Size
4
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
2
UnderFlow
deleted element is:0
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:5
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:10
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
3
top element is:10
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
4
total elements in stack is:2
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:56
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:42
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
1
Enter data:36
Stack overflow
enter 1.Push
2.Pop
3.Peek
4.count
5.Exit
5
Java Result: 1
BUILD SUCCESSFUL (total time: 41 seconds)