Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I wanted to create the output shown in one of the pictures shown but not sure where to start . Im not sure if my

I wanted to create the output shown in one of the pictures shown but not sure where to start . Im not sure if my programming is right to get the output shown . Is there anyway to help compile my code? using the player class

code :

public class ArrayStack implements StackInterface {

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

}

@Override

public void push(T element) {

if (stack.length == top) {

expandCapacity();

}

stack [top] = element;

top++;

}

private void expandCapacity() {

}

@Override

public T pop() throws StackException {

return null;

}

@Override

public T peek() throws StackException {

return null;

}

@Override

public int size() {

return 0;

}

@Override

public boolean isEmpty() {

// TODO Auto-generated method stub

return false;

}

}

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 Player {

// creating variables

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;

this.score = 0;

this.rank = "Level 1";

}

//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("Player: " + getName()

+ " Score: " + getScore() + " Rank: "

+ decideRank());

}

}

public class stackDriver {

public static void main(String[] args) {

StackInterface strStack = new ArrayStack();

}

}

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

6 7 3 import java.util.Random; 4 5 public class Player { 1/ creating variables private String playersName: 8 private int score; 9 private String rank; 10 11 // players methods 129 public player() 13 14 this.playersName = ""; 15 this.score = 0; 16 this.rank = "Level 1"; 17 2 18 190 public player(String nane) { 20 this.playersName = name; 21 this.score = 0; 22 this.rank = "Level 1": 23 } 24 25 //accessors 26 public String getNane() { return playersName:) 27 public int getScore() (return score:) 28 public String getRank() { return ranka) 29 30 //mutators 31 public void setName(String nane) { this.playersante none;) public void setScorelint score) (this.score = score;} public void setRank(String rank) { this.rank = rank;) 35 360 public void play 37 38 1/ generating a randon number for players score 39 setscoret(int) (Math.random()10070)); 40 } 33 34 42 49 1/deciding rank of player depending on score public String decideRank String rank = "" if(getScore() >= 58) rank - "Level 1: else ifigetScorel) > 35 66 getScore() 20 65 getScorel) = 50) rank = "Level 1"; else if(getScore() >= 35 && getScore() = 20 66 getScorel) { 4 public void push(element); 6 public T pop() throws Stackexception; public T peek() throws Stackexception; public int size(); public boolean isEmpty(); 10 public String toString(); 11 12 ) 13 7 8 9 } public class ArrayStack implements Stackinterface ! private int top: private T stack; private final static int DEFAULT_CAP = 3; public ArrayStack (int size) { stack = (TI). (new Object (sizel); top = ; public ArrayStack() { this (DEFAULT_CAP); ) @Override public void push(T element) { it (stack. length - top) { expandCapacity: ) stack (top) - element; top++; 25 private void expand Capacity 3 25 27 28 29 30 31 32 @Override public T pop() throws Stackexception { // TODO Auto-generated method stub return null; } 35 @Override public T peek() throws Stackexception { // TODO Auto-generated nethod stub return null; 3 39 40 244 45 46 Override public int size() { // TODO Auto-generated method stub return; ) Override public boolean IsEmpty() { TODO Auto-generated method stub return false; public class stackDriver { public static void main(String[] args) { StackInterface strStack = new ArrayStack(); 1 2 3 5 StackDriver (1) [Java Application] C:\Program Files\Java'jre 1.8.0_101\bir riginal stack of players: Name: Rex Score: 29 Rank: Level 3 Name: Christy Score: 13 Rank: Level 4 Name: Zane Score: 31 Rank: Level 2 Name: Cali Score: 39 Rank: Level 2 Current number of players in the stack: 4 Current player on top of stack: Name: Cali Score: 39 Rank: Level 2 Altered stack of players: Name: Rex Score: 29 Rank: Level 3 Name: Christy Score: 13 Rank: Level 4 Current number of players in the stack: 2 Current player on top of stack: Name: Christy Score: 13 Rank: Level 4 Stack empty (true or false): false Current number of players in the stack: 0 Stack empty (true or false): true

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

Step: 3

blur-text-image

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

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions

Question

46. GivenMX(t).2.3et.5e3t, ndp(x), E(X), V(X).

Answered: 1 week ago

Question

Distinguish between aptitude and achievement tests.

Answered: 1 week ago