Question: How can i print out the output below when using this code? I need to print out an exception. I want to create the output
How can i print out the output below when using this code? I need to print out an exception. I want to create the output shown but dont where to start 
public class ArrayStack
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 Dogs { private String dogName; private int age; private String breed; public Dogs () { this.dogName = ""; this.age = 0; this.breed = ""; } public Dogs (String name) { this.dogName = name; } //accessors public String getName() { return dogName;} public int getAge() {return age;} public String getBreed() { return breed;} //mutators public void setName(String name) { this.dogName = name;} public void setAge(int age) { this.age = age;} public void setBreed(String breed ) { this.breed = breed;} public String toString() { return("Name: " + getName() + " Breed:" + getBreed() + " Age: " + getAge()); } } 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; } //accessors public String getName() { return playersName;} public int getScore() {return score;} public String getRank() { return rank;} //mutators 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() { // generating a random number for players score 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); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
