In: Computer Science
Write a java application that implements ArrayStack in chapter 16 of your textbook.
Your application should have two files:
1. ArrayStack.java: This file will have the implementation of your stack.
2. TestStack.java: This file will have the main method where you declare your stack object and test the different stack operations.
In addition to the ArrayStack methods given in the book, you are to write a toString method. This will allow you to print the Stack object from main.
You should be able to find the doubleArray method implementation via google searching.
Demonstrate all stack operations by calling them in your main and using print statements to explain the method calls. A sample output is given below, you should do something similar.
The current stack is empty: [] After 5 push operations the stack has 5 elements [A, B, C, D, E] The current top of stack is E TopAndPop returns E. The top of stack is currently D After 2 pop operations the stack has 5 elements [A, B] Currently the stack is not empty After calling makeEmpty, the stack becomes empty: []
ArrayStack class-
import java.util.Arrays;
//create ArrayStack class
public class ArrayStack {
int size=5;
//creating character Array
char stack[]=new char[size];
//set top to -1
int top=-1;
//push method body
public void push(char v){
if(top==size-1)
{
System.out.println("Stack is full");
}
else{
stack[++top]=v;
System.out.println("Pushed SuccessFully");
}
}
//pop method body
public void pop()
{
char v=stack[top];
--top;
System.out.println("Poped Element
is: "+v);
}
public void show(){
if(top==-1)
{
System.out.println("Stack is empty");
}
else {
System.out.println("Stack Element Are: ");
System.out.print("[");
for(int i=0;i<=top;i++)
{
System.out.print(stack[i]+",");
}
System.out.println("]");
}
}
//empty method
public void makeEmpty()
{
top=-1;
}
//toString method for printing the ArrayStack
object
public String toString() {
return Arrays.toString(stack)
;
}
}
TestStack class-
import java.util.Scanner;
//creating a class TestStack
public class TestStack {
//creating object of scanner class
static Scanner sc=new Scanner(System.in);
//creating object of ArrayStack class
static ArrayStack my=new ArrayStack ();
//main method
public static void main(String[] args) {
while(true)
{
int ch=0;
System.out.println("1:
push");
System.out.println("2: pop");
System.out.println("3:
show");
System.out.println("4: make
Empty");
System.out.println("5:
exit");
System.out.println("Enter your Choice");
ch=sc.nextInt();
switch(ch)
{
case 1:System.out.println("Enter Stack
Element");
//taking character input from user
char
value=sc.next().charAt(0);
//call push method
my.push(value);
break;
case 2://call pop method
my.pop();
break;
case 3: //display MyStack object using toString
method
System.out.println(my);
break;
case 4 :
//call makeEmpty method for clean
the stack
my.makeEmpty();
System.out.println("now stack is empty");
break;
case 5: System.exit(0);
default: System.out.println("Invalid choice!!");
}
}
}
}
note - this is only for character.