create a stack class based on an array of type double of fixed size. In the clas
ID: 3806673 • Letter: C
Question
create a stack class based on an array of type double of fixed size. In the class,
The array should have the default size of 5.
The array size should be settable via a constructor.
The following methods must be implemented:
push – inserts a value a the top of the stack
pop – returns the top value, removing it from the stack
isEmpty – returns true if the stack is empty, false otherwise
isFull - – returns true if the stack is full, false otherwise
Please notice you must use an array not an arraylist or any other list. Im having trouble with a couple parts of it. Thanks
Explanation / Answer
PROGRAM:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Stack
{
private double[] stack=new double[5];
private int size=5;
private int top=-1;
public Stack()
{
}
//Parameterized constructor for initializing array size
public Stack(int size)
{
this.stack = new double[size];
this.top = -1;
this.size = size;
}
//inserting values in to stack
public void push(double obj)
{
if (top >= size)
throw new IndexOutOfBoundsException("Stack size = " + size);
stack[++top] = obj;
}
//checking whether stack is empty
public boolean isEmpty()
{
if(this.top==-1)
{
return true;
}
return false;
}
// //checking whether stack is full
public boolean isFull()
{
if(top==size)
{
return true;
}
return false;
}
//poping the values from the stack
public double pop()
{
if (top < 0) throw new IndexOutOfBoundsException();
double obj = stack[top--];
stack[top + 1] = 0;
return obj;
}
//main function
public static void main(String s[]) throws NumberFormatException, IOException
{
//variable type stack
Stack s1= null;
//InputStreamReader & BufferedReader for reading the input
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
int choice=0;
double number=0;
System.out.println("1. user default size of array:");
System.out.println("2. custum size");
choice=Integer.parseInt(br.readLine().toString());
//checking user is using default size of an array or customized size
if(choice==1)
{
s1=new Stack();
}
else if(choice==2)
{
System.out.println(" enter size of array:");
int size=Integer.parseInt(br.readLine());
s1=new Stack(size);
}
loop:do
{
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. is empty");
System.out.println("4. is full");
System.out.println("5. exit");
System.out.println("Enter the Choice:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
//pushing the value to the stack
try
{
System.out.println("Enter the number:");
number=Double.parseDouble(br.readLine());
s1.push(number);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("Array is full");
}
break;
case 2:
//poping the value from the stack
System.out.println(s1.pop());
break;
case 3:
//checking the stack whether it is empty or not
System.out.println(s1.isEmpty());
break;
case 4:
//checking the stack whether it is full or not
System.out.println(s1.isFull());
break;
case 5:
break loop;
default:
System.out.println("Enter a valid choice");
}
}while(true);
}
}
OUTPUT:
1. user default size of array:
2. custum size
1
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
22
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
2
22.0
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
3
true
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
4
false
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
1
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
1
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
1
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
1
1. push
2. pop
3. is empty
4. is full
5. exit
Enter the Choice:
1
Enter the nuber:
1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.