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 How can i print out the output below when using this code?

public class ArrayStack implements StackInterface {

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 strStack = new ArrayStack();

StackInterface dogStack = new ArrayStack();

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);

}

}

terminated Testing Driver Java Application Program Files Javje 1.3.0 10binjava. (Feb 15, 2017, 10:53:06 PM) Creating first stack, to track Players. Peeking at top of stacki. exceptions. EmptyStackexception: Stack is empty. No element to return Looks empty. Let's double check, just in case... true Yep, empty. Let's add some Players to it. Creating five Players. Pushing the five players on the stack. Creating generic Players p1, P2, P3. Popping the top three Players on Stack 1 into p1, p2, and p3. Arranging them in an array for easy looping. Using the play method on the player array via a loop, and then pushing them back to the stack. Taking a peek at the top of the stack. 168 Level 2 How many players are in the stack, again? Printing out stack of Players. Susan 168 Level 2 Clara 233 Level 3 Daryl 158 Level 2 Shawn Unranked Aaron Unranked Susan Creating a second stack, to track Characters. Peeking at top of stack2. exceptions. EmptyStackException: Stack is empty. No element to return Looks empty. Let's double check, just in case... true Yep, empty. Let's add some Characters to it. Creating 3 Characters. Pushing the three characters on the stack. Creating a second stack, to track Characters. Peeking at top of stack2. exceptions. EmptyStackexception: Stack is empty. No element to return Looks empty. Let's double check, just in case... true Yep, empty. Let's add some Characters to it. Creating 3 Characters. Pushing the three characters on the stack. Printing out the stack real fast. Butane: B. Elf: Demon Hunter: 110 844 Gaav: N. Elf: Warrior: 100 712 Dracon: Human: Paladin: 110 897 Let's see if they can all run Heroics. true false true Gaav can't? Ok. Let's put him back last. Let's make sure we did that right... Gaav: N. Elf: Warrior: 100 712 Ok, reprinting the stack one more time, to make sure. Gaav: N. Elf: Warrior: 100 712 Butane: B. Elf: Demon Hunter: 110 844 Dracon: Human: Paladin: 110 897 I forget, how many Players were in stack1? 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!