Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Begin with the following class public class Game{ private String name; private i

ID: 3902408 • Letter: B

Question

Begin with the following class

public class Game{
     private String name;
     private int maxNumPlayers;
     private boolean canEndInTie;
     private int totalPoints;
     static final int winningPoints=1000;

     public boolean canEndGame(){
           if (totalPoints > winningPoints)
            return true;
         else
               return false;
     }
}

Write a complete Java program that accomplishes the following:

Write a constructor for class Game that initializes 3 of the data members with values passed in as parameters.

Write accessor and mutator methods for the variable canEndInTie.

Make a subclass of Game called CardGame. A card game keeps track of the number of cards that remain in the central stack. This subclass should have a constructor that accepts parameters to assign values to the number of cards currently in the stack, and whether it can end in a tie (a data member of the Game class).


CardGame is different than Game in that it is over when the stack of cards has been depleted. When the method canEndGame is called on a CardGame object, it should reflect this difference.

Modify the CardGame class so that it implements the following interface. (It's up to you to determine the functionality it represents.)
interface Playable
{
      boolean canBeginGameNow(int numPlayers);
}

Write a main method that does the following:
Create an instance of either Game or CardGame.
Then call canEndGame on this object.
Display a message of either Game can end now. (if canEndGame returned true) or Game cannot end yet. (if canEndGame returned false).

Explanation / Answer

Below is your code. I have tried implementing everything. Please let me know in comments if I have missed anything. I'll update the code..

Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments

Game.java

public class Game {

private String name;

private int maxNumPlayers;

private boolean canEndInTie;

public Game() {

name = "Default Game";

maxNumPlayers = 0;

canEndInTie = true;

}

public Game(String name, int maxNumPlayers, boolean canEndInTie) {

this.name = name;

this.maxNumPlayers = maxNumPlayers;

this.canEndInTie = canEndInTie;

}

private int totalPoints;

static final int winningPoints = 1000;

public boolean isCanEndInTie() {

return canEndInTie;

}

public void setCanEndInTie(boolean canEndInTie) {

this.canEndInTie = canEndInTie;

}

public boolean canEndGame() {

if (totalPoints > winningPoints)

return true;

else

return false;

}

}

CardGame.java

public class CardGame extends Game implements Playable {

private int numOfCardsInStack;

public CardGame(int numOfCardsInStack, boolean canEndInTie) {

this.numOfCardsInStack = numOfCardsInStack;

this.setCanEndInTie(canEndInTie);

}

@Override

public boolean canEndGame() {

if (numOfCardsInStack <= 0)

return true;

else

return false;

}

@Override

public boolean canBeginGameNow(int numPlayers) {

if (numPlayers > 1) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

Game gm = new Game("PlayMate", 10, true);

if (gm.canEndGame()) {

System.out.println("Game can end now.");

} else {

System.out.println("Game cannot end yet.");

}

CardGame cgm = new CardGame(0, true);

if (cgm.canEndGame()) {

System.out.println("Game can end now.");

} else {

System.out.println("Game cannot end yet.");

}

}

}

Playable.java

interface Playable {

boolean canBeginGameNow(int numPlayers);

}

Output

Game cannot end yet.
Game can end now.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote