Java code please Write a class encapsulating a sports game, which inherits from
ID: 3826553 • Letter: J
Question
Java code please
Write a class encapsulating a sports game, which inherits from Game. A sports game has the following additional attribute: whether the game is a team or individual game, and whether the game can end in a ti. Code the constructor and the toString method of the new class. You also need to include a client class to test your code.
Problems Javadoc Declaration O Console terminated SportsGameClient Java A cation] C:Program F 111 bin javaw.exe Jan 3, 2017, 2:24:29 P description: Soccer: Is a team game true: Can end in a tie true description 100 yards: Is a team game: false: Can end in a tie: true sgl and sg2 are not equal sgl and sg2 are now equalExplanation / Answer
Game.java
public class Game
{
private String description;
public Game(String description)// argument constructor
{
this.description = description;
}
public void setDescription(String description)
{
this.description = description;
}
public String toString()
{
return " description : "+description;
}
}
SportsGame.java
public class SportsGame extends Game
{
private boolean isTeamGame;
private boolean endsInTie;
public SportsGame(String description,boolean isTeamGame,boolean endsInTie)//constructor
{
super(description);//call base class constructor
this.isTeamGame = isTeamGame;
this.endsInTie = endsInTie;
}
public void setIsTeamGame(boolean isTeamGame)
{
this.isTeamGame = isTeamGame;
}
public void setEndsInTie(boolean endsInTie)
{
this.endsInTie = endsInTie;
}
public String toString()
{
return super.toString()+ " Is a team game : "+isTeamGame +" Can end in a tie : "+endsInTie;
}
public boolean equals(SportsGame sg) // compare two ganes
{
if(this.isTeamGame == sg.isTeamGame && this.endsInTie == sg.endsInTie)
return true;
else
return false;
}
}
TestGame.java
public class TestGame
{
public static void main (String[] args)
{
SportsGame sg1 = new SportsGame("Soccer",true,true);
SportsGame sg2 = new SportsGame("100 yards",false,true);
System.out.println(sg1);
System.out.println(sg2);
if(sg1.equals(sg2))
System.out.println(" sg1 and sg2 are equal");
else
System.out.println(" sg1 and sg2 are not equal");
sg2.setDescription("soccer");
sg2.setIsTeamGame(true);
sg2.setIsEndsInTie(true);
if(sg1.equals(sg2))
System.out.println(" sg1 and sg2 are equal");
else
System.out.println(" sg1 and sg2 are not equal");
}
}
output:
description : Soccer Is a team game : true Can end in a tie : true
description : 100 yards Is a team game : false Can end in a tie : true
sg1 and sg2 are not equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.