Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

}

}

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

3 import java.util. Random; 4 5 public class Player { 5 // creating variables 7 private String playersName: 8 private int score; 9 private String rank: 10 11 // players methods 12e public player() 13 14 this.playersName = ""; 15 this.score = 0; 16 this.rank = "Level 1"; 17 2 18 19 public Player(String nane) { 20 this.playersName = name; 21 this.score = 0; 22 this.rank - "Level 1"; 23 } 24 25 //accessors 26 public String getName() { return playersName : ) 27 public int getscore() {return score;) 28 public String getRank() { return ranka) 29 30 Imutators 31 public void setName(String nane) { this.playersante w name;) 32 public void setScore(int score) (this.score = score;) 33 public void setRank (String rank) { this.rank = rank:) 34 35 36 public void play 37 38 1 generating a random number for players score 39 setScore(int) (Math.random()-10070)); 41 // deciding rank of player depending on score public String decideRankt) 45 String rank - it getScore() > 58) rank - "Level 1: 48 else if(getScorel) > 35 66 getScore() 20 65 getScorel) { public void pusher element); public T pop() throws Stackexception; public T peek() throws StackException; public int size(); public boolean isEmpty(); public String toString(); 11 12 ) 13 Nm 000 6 7 8 9 10 LOVOU ALON 5 6 2 3 public class Stackexception extends RuntimeException { 40 public StackException() { super("stack is empty"); } 70 public Stackexception(String msg) { 8 super(msg); } 10 11 } 9 12 3 public class stackDriver { 4 5 6e public static void main(String[] args) { 7 StackInterface strStack = new ArrayStack(); 8 StackInterface dogStack = new Arraystack(); 9 Player player = new Player("Rex"); player.setScore(29); 11 strStack.push(player); 12 13 player = new Player("Christy"); 14 player.setScore(13); 15 strStack.push(player); 16 I 17 player = new Player("Zane"); 18 player.setScore (31); 19 strStack.push(player); 20 21 player = new Player("Cali"); 22 player.setScore (39); 23 strStack.push(player); 24 25 Dogs dog = new Dogs ("rooney"); 26 dog.setAge(10); dog.setBreed ("Golden"); 28 dogStack.push(dog); 29 30 dog = new Dogs("Christy"); 31 dog.setAge(7); 32 dog.setBreed ("Golden"); 33 dogStack.push(dog); 34 35 dog = new Dogs("Zane"); 36 dog.setAge(2); 37 dog.setBreed ("Golden"); 38 dogStack.push(dog); 39 40 dog = new Dogs("Call"); 41 dog.setAge(5); dog.setBreed ("Golden"); 43 dogStack.push(dog); 27 45 46 47 48 // display the original stack System.out.println("Original stack of players:"); System.out.println(strStack); Sim 51 Writable P S RE PI SC Ra Cur Cur // 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()); 1/ display current top player of stack System.out.println(" Current player on top of stack!"); System.out.println(strStack.peek()); I // pop first 2 elements of stack strStack.pop(); strStack.pop(); // display updated stack etements System.out.println(" Altered stack of players:"); System.out.println(strStack); 17 display number of elements and player at cop 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(); 1/ check it stack is empty System.out.println(" Stack empty true or false): "testock, isEmpty(); 1 loop to empty the stack whilelistrStack.isEmpty()) strStack.pop(); Pla Sco Rank Alte Play Score Rank: Playe Score Rank: 2 22 23 Curren 28 79 Current Player Score: Rankt 51 82 BB Stack en 8 current W/ display number of etenents in stack and if stack is empty 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 stock System.out.println("Current number of players in the stockt dogStack.size()) 1/ display current top player of Stack Systen.out.println("incurrent player on top of stack"); System.out.print in dogstack peek(): Stack em Original Name: roo Breed: Gold Age: 10 Name: Chri Breed: Gold Woo first 2 o Lonants OT stack 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. P2 Popping the top three players on stack 1 into pi, p2, and p3. Arranging them in an array for easy looping. Using the play method on the player array vie loop, and then pushing them back to the stack. Taking a peek at the top of the stack. Susan 168 Level 2 How many players are in the stack, again? 5 Printing out stack of Players. Susan 168 Level 2 Clara 233 Level 3 Dary1 158 Level 2 Shawn @ Unranked Aaron e Unranked Creating a second stack, to track Characters. 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... 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: 11e 844 Gaav: N. Elf: Warrior: 100 712 Dracon: Human: Paladin: 110 897 Let's see if they can all run Heroics. true false true true Gaav can't? Ok. Let's put him back last. Let's make sure we did that right... Gaav: N. Elf: Warrior: 1ee 712 Ok, reprinting the stack one more time to make sure. Gaav: N. Elf: Warrior: 1ee 712 Butane: B. Elf: Demon Hunter: 110 844 Dracon: Human: Paladin 110 897 I forget, how many Players were in stacki? 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions