Develop a Stack.java: •a class named Stack •Within the stack class, there are fo
ID: 3797998 • Letter: D
Question
Develop a Stack.java:
•a class named Stack
•Within the stack class, there are four methods:
•a constructor method
•boolean pop();// delete the element on the top of the stack
•boolean push (Character c);// pushes the element c onto the stack
•Character peek();// returns the element on the top of the stack (doesn’t remove it!)
•boolean isEmpty();// check whether the stack is empty
Implementation:
•Array or linked list
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Develop a Test.java:
•Main method which will test the stack:
•Create a stack with 10 Character objects capacity
•Call the methods in the stack class, output the result of each method
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Command Window should look like:
Explanation / Answer
import java.util.*;
class stack
{
int maxsize =0, n=0;
maxsize=n;
private:
int arr[maxsize]
int top;
public:boolean isempty()
{
return top=-1;
}
public:booleanpop()
if(isempty())
system.out.println("underflow exception is thrown");
maxsize=maxsize-1;
return[top--];
}
public:boolean push(int i)
{
if(top+1>=maxsize)
system.out.println("overflow exception is thrown");
if(top+1<maxsize)
arr[++top]=i;
maxsize=maxsize+1;
pulic:character peek()
{
if(Isempty())
system.out.println("underflow exception is thrown");
return arr[top];
}
public Static void main(string args[])
{
scanner scan =new scanner(system.in);
system.out.println("enter the size of the stack");
int n=scan.next int();
stack stack1=newstack(n);
char ch;
do{
system.out.println(1.push);
system.out.println(2.pop);
system.out.println(3.peek);
system.out.println(4.isempty);
int choice =scan.nextint();
switch(choice)
{
case1: System.out.println(stack1.push(scan.next());
break;
case2: System.out.println(stack1.pop(scan.next());
break;
case3: System.out.println(stack1.peek(scan.next());
break;
case4: System.out.println(stack1.Isempty(scan.next());
break;
default: System.out.println("oops you have entered the wrong choice");
break;
system.out.println("do you want to continue(Y orN));
ch =scan.next()
}
while(ch=='Y'|| ch=='y')';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.