In Java: Write a class encapsulating a board game, which inherits from Game. A b
ID: 3567475 • Letter: I
Question
In Java: Write a class encapsulating a board game, which inherits from Game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class to test your code. Please provide the full code in a easily readible format. *NOTE*: I need full code for each class. Don't give me the same answer that is floating around on chegg and yahoo answers that is worthless
Explanation / Answer
public class Game
{
int players;
String tied = "tied";
String notTied = "not tied";
String end;
public Game(int people, String ending)
{
players = people;
if (ending == "tied")
end = tied;
else if (ending == "not tied")
end = notTied;
}
public int getPlayers()
{
return players;
}
public String getEnding()
{
return end;
}
public void setPlayers(int people)
{
players = people;
}
public void setEnding(String tieOrNo)
{
end = tieOrNo;
}
}
class BoardGame extends Game
{
public BoardGame(int people, String ending)
{
super(people, ending);
}
@Override
public String toString()
{
return "Number of players: " + players + " The game was " + end;
}
}
Client Class
public class GameClient
{
public static void main(String [] args)
{
BoardGame game1 = new BoardGame(4, "not tied");
System.out.println(game1.toString());
game1.setPlayers(6);
game1.setEnding("tied");
System.out.println(game1.toString());
BoardGame game2 = new BoardGame(2, "tied");
System.out.println(game2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.