Question
I need help trying to get the same output in the picture below but dont know where to start. How do i create new players
I need help trying to get the same output in the picture below but dont know where to start. How do i create new players or get an exception. Please use word for word just like the following output .
Code:
public class ArrayStack
private int top;
private T[] stack;
private final static int DEFAULT_CAP = 3;
public ArrayStack (int size)
{
stack = (T[])(new Object[size]);
top = 0;
}
public ArrayStack()
{
this (DEFAULT_CAP);
}
public void push(T element)
{
if(stack.length == top)
{
expandCapacity();
}
stack[top] = element;
top++;
}
private void expandCapacity()
{
T[] temp = (T[]) new Object[stack.length * 2];
for (int i = 0; i
{
temp[i] = stack[i];
}
stack = temp;
}
@Override
public T pop() throws StackException
{
if (isEmpty())
throw new StackException();
T result;
top--;
result = stack[top];
stack[top] = null;
return result;
}
public T peek() throws StackException
{
if (isEmpty())
throw new StackException();
return stack[top-1];
}
public int size() {return top;}
public boolean isEmpty() {return (top == 0);}
public String toString()
{
String result = "";
if (top > 0)
{
result = result + stack[top-1];
}
for (int i = top - 2; i >= 0; i--)
{
result = result + " " + stack[i];
}
result = result + "";
return result;
}
}
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;
}
public String getName() { return dogName;}
public int getAge() {return age;}
public String getBreed() { return breed;}
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;
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;}
//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()
{
setScore((int)(Math.random() * 100 % 70));
}
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 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 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());
// pop first 2 elements of stack
strStack.pop();
strStack.pop();
// display updated stack elements
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());
// check if stack is empty
System.out.println(" Stack empty (true or false): "+strStack.isEmpty());
// loop to empty the stack
while(!strStack.isEmpty())
strStack.pop();
// display number of elements 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 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();
// display updated stack elements
System.out.println(" Altered stack of players:");
System.out.println(dogStack);
// display number of elements and player at top of stack
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());
// check if stack is empty
System.out.println(" Stack empty (true or false): "+dogStack.isEmpty());
// loop to empty the stack
while(!dogStack.isEmpty())
dogStack.pop();
// display number of elements in stack and if stack is empty
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();
try {
System.out.println("Creating first stack , to track players");
System.out.println("Peeking at top of stack 1");
strStack.peek();
} catch (StackException e) {
System.out.println(e);
}
System.out.println(strStack.isEmpty());
}
}
bin yavaw.exe (Feb 1, 2011 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. 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 Daryl 158 Level 2 Shawn Unranked Aaron Unranked
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