In: Computer Science
Complete the following for full credit in this assignment:
Complete the coding requirement for the Class Method Bodies:
Write the constructor: This should initialize all of your class fields and dynamically allocate memory for your array that will hold your stack data. You should use the constant “n” as the size for your fixed-size stack.
Write the push method: This should add the item in the formal parameter to your stack. Caveat: You need to check and make sure you aren’t going out of bounds with your array. If so, you need to ignore requests to add to the stack as it is full. After that, you can simply add the item and update your cursor (“top” field).
Write the toString method: If the stack is empty, simple return empty brackets “[]”. Otherwise, build a string that consists of every item in the stack inside the brackets and separated by commas (e.g. [1,2,3]). You must use a loop to do this, but you will likely have an extra comma at the end. You can use the substring method to remove the ending comma.
Write the isEmpty method: This can be done in one line if you are savvy. Simply return whether the size of your stack is zero or not.
Write the size method: This can also be done in one line depending on how you programmed the rest of the class. Simply return how many items are currently in the stack.
Write the peek method: This should return the value at the top of the stack. If the stack is empty, return -1. Caveat: Make sure you check whether the stack is empty BEFORE you try accessing array elements otherwise you could crash the program.
Write the pop method: This should return the value at the top of the stack and then remove that item from the stack. We can use “lazy evaluation” which means we don’t actually remove the item but only remove access to it. Simply decrement your cursor variable to remove access to it. If the stack is empty, return -1. Caveat: Make sure you check whether the stack is empty BEFORE you try accessing array elements otherwise you could crash the program.
Write a clear method. This should set the stack to the empty state. In other words, it “resets” the stack to how it was when you first created the stack object. You should use “Lazy Evaluation” for efficiency.
public class Lab05{
public static void main(String[] args){
StackInt st = new StackInt();
p(st.isEmpty());
// This should be true
p(st.peek());
// This
should be -1
p(st.size());
// This
should be 0
for(int i = 0; i < 10;
i++)
st.push(i);
// This adds 10
items to the stack
p(st.toString());
// This should be
[0,1,2,3,4,5,6,7,8,9]
p(st.isEmpty());
// This should be false
p(st.peek());
// This
should be 9
p(st.size());
// This
should be 10
p("Popped " + st.pop() + " from
stack..."); // This should be "Popped 9 from
stack..."
p(st.toString());
// This should be
[0,1,2,3,4,5,6,7,8]
st.clear();
// This
should clear the stack...
p(st.isEmpty());
// This should be true
}
public static <E> void p(E s){
System.out.println(s);
}
}
GIVE THUMPS UP
here's, your java code for the above question.
I HAVE COMMENTED DOWN THE WORK OF EACH OF THE STACK'S METHOD. I HAVE DONE THIS CODE IN NOTEPAD++ EDITOR AND RUN THROUGH CMD.
SOURCE CODE:
/* JAVA CODE */
import java.util.*;
class Stack{
int stack[];
int top;
int max;
//constructor for initializing stack
Stack(int maxSize){
stack = new int[maxSize];
max = maxSize;
top = -1;
}
//this fuction is used to add an elelments in
stack
public void push(int data){
if(top == (max - 1)){
System.out.println("STACK OVERFLOW!!!");
System.exit(1);
}
stack[++top] = data;
}
//Thsi fuction is used to check, stack is empty or
not.
public boolean isEmpty(){
if(top == -1){
return
true;
}
return false;
}
//toString fuvtion is used to print stack
public String toString(){
String result = "";
if(isEmpty()){
return
"[]";
}
else{
for(int
i=top;i>=1;i--){
result = "," + stack[i] + result;
}
result = "[" +
stack[0] + result + "]";
}
return result;
}
//Returning size of the stack
public int size(){
return top+1;
}
//returning peek/top element of the stack
public int peek(){
if(isEmpty()){
return -1;
}
return stack[top];
}
//returning pop elelment of the stack.
public int pop(){
if(isEmpty()){
System.out.println("STACK UNDERFLOW");
System.exit(1);
}
return stack[top--];
}
//this method is used to reset the stack or clear all
the stack
public void clear(){
top = -1;
}
public static void main(String[] args){
Stack st = new Stack(20);
System.out.println(st.isEmpty());
//this should be true
System.out.println(st.peek());
//this should be -1
System.out.println(st.size());
//this should be 0
for(int i=0;i<10;i++){
st.push(i);
//this adds 10 items to the stack.
}
System.out.println(st.toString());
//this should be [0,1,2,3,4,5,6,7,8,9]
System.out.println(st.isEmpty());
//this should be false
System.out.println(st.peek());
//this should be 9
System.out.println(st.size());
//this should be 10
System.out.println("Popped " +
st.pop() + " from Stack..."); //this should be " Popped from 9 from
the stack..."
System.out.println(st.toString());
//this should be[0,1,2,3,4,5,6,7,8]
st.clear(); //this should be clear
the stack
System.out.println(st.isEmpty());
//this should be true.
}
}
OUTPUT :
true
-1
0
[0,1,2,3,4,5,6,7,8,9]
false
9
10
Popped 9 from Stack...
[0,1,2,3,4,5,6,7,8]
true
SCREENSHOT :
IF YOU HAVE ANY DOUBTS, DO COMMENT IN COMMENT BOX. GIVE THUMPS UP...