Question
How can i add this exception into my program?? Using the output sample. Java private int top; private T [] stack; private final static int
How can i add this exception into my program?? Using the output sample. Java
private int top;
private T [] stack;
private final static int DEFAULT_CAP = 3;
// constructor to create an empty stack of given capacity
public ArrayStack (int size) {
stack = (T[]) (new Object [size]);
top = 0;
}
// default constructor to create an empty stack of default capacity
public ArrayStack() {
this (DEFAULT_CAP);
}
// method to insert element at top of stack
@Override
public void push(T element) {
if (stack.length == top) // stack is full, expand the stack
{
expandCapacity();
}
// insert element at top
stack[top] = element;
top++;
}
// helper method to expand the capacity of the array
private void expandCapacity() {
// create a new array of twice the original capacity
T[] temp = (T[]) new Object[2*stack.length];
// loop to insert elements from stack to temp
for(int i=0;i temp[i] = stack[i]; stack = temp; // update stack to temp } // method to remove and return the top element of stack @Override public T pop() throws StackException { if(isEmpty()) // empty stack, throw exception throw new StackException("Cannot pop from an empty stack."); // get the top data T data = stack[top-1]; top--; // decrement top return data; } // method to return the top element of stack @Override public T peek() throws StackException { if(isEmpty()) // empty stack, throw exception throw new StackException("Cannot peek from an empty stack."); return stack[top-1]; // return the top element } // method to return the number of elements in the stack @Override public int size() { return top; } // method to return true if stack is empty else return false @Override public boolean isEmpty() { return top == 0; } // method to return string representation of stack from bottom to top public String toString() { String s = ""; for(int i=0;i s += stack[i].toString()+" "; return s; } } public interface StackInterface public void push(T element); public T pop() throws StackException; public T peek() throws StackException; public int size(); public boolean isEmpty(); public String toString(); } public class StackException extends RuntimeException { public StackException() { super("stack is empty"); } public StackException(String msg) { super(msg); } } public class Player { private String playersName; private int score; private String rank; // players methods public Player() { this.playersName = ""; this.score = 0; this.rank = "Level 1"; } public Player(String name) { this.playersName = name; } public String getName() { return playersName;} public int getScore() {return score;} public String getRank() { return rank;} public void setName(String name) { this.playersName = name;} public void setScore(int score) { this.score = score;} public void setRank(String rank) { this.rank = rank;} public void play() { setScore((int)(Math.random() * 100 % 70)); } //deciding rank of player depending on score public String decideRank() { String rank = ""; if(getScore() >= 50) rank = "Level 1"; else if(getScore() >= 35 && getScore() rank = "Level 2"; else if(getScore() >= 20 && getScore() rank = "Level 3"; else if(getScore() rank = "Level 4"; setRank(rank); return rank; } // creating a toString method to print players information @Override public String toString() { return("Name: " + getName() + " Score: " + getScore() + " Rank: " + decideRank()); } public class stackDriver { public static void main(String[] args) { StackInterface StackInterface Player player = new Player("Rex"); player.setScore(29); strStack.push(player); player = new Player("Christy"); player.setScore(13); strStack.push(player); player = new Player("Zane"); player.setScore(31); strStack.push(player); player = new Player("Cali"); player.setScore(39); strStack.push(player); Dogs dog = new Dogs("rooney"); dog.setAge(10); dog.setBreed("Golden"); dogStack.push(dog); dog = new Dogs("Christy"); dog.setAge(7); dog.setBreed("Golden"); dogStack.push(dog); dog = new Dogs("Zane"); dog.setAge(2); dog.setBreed("Golden"); dogStack.push(dog); dog = new Dogs("Cali"); dog.setAge(5); dog.setBreed("Golden"); dogStack.push(dog); // display the original stack System.out.println("Original stack of players:"); System.out.println(strStack); // display number of elements in stack System.out.println("Current number of players in the stack: "+strStack.size()); // display current top player of stack System.out.println(" Current player on top of stack:"); System.out.println(strStack.peek()); strStack.pop(); strStack.pop(); System.out.println(" Altered stack of players:"); System.out.println(strStack); // display number of elements and player at top of stack System.out.println(" Current number of players in the stack: "+strStack.size()); System.out.println(" Current player on top of stack:"); System.out.println(strStack.peek()); System.out.println(" Stack empty (true or false): "+strStack.isEmpty()); while(!strStack.isEmpty()) strStack.pop(); System.out.println(" Current number of players in the stack: "+strStack.size()); System.out.println(" Stack empty (true or false): "+strStack.isEmpty()); System.out.println(); System.out.println("---------------------------------------------------"); System.out.println("Original stack of players:"); System.out.println(dogStack); // display number of elements in stack System.out.println("Current number of players in the stack: "+dogStack.size()); // display current top player of stack System.out.println(" Current player on top of stack:"); System.out.println(dogStack.peek()); // pop first 2 elements of stack dogStack.pop(); dogStack.pop(); System.out.println(" Altered stack of players:"); System.out.println(dogStack); System.out.println(" Current number of players in the stack: "+dogStack.size()); System.out.println(" Current player on top of stack:"); System.out.println(dogStack.peek()); System.out.println(" Stack empty (true or false): "+dogStack.isEmpty()); while(!dogStack.isEmpty()) dogStack.pop(); System.out.println(" Current number of players in the stack: "+dogStack.size()); System.out.println(" Stack empty (true or false): "+dogStack.isEmpty()); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started