Create a class Create a class called SportsGame. Include the following fields (S
ID: 3831512 • Letter: C
Question
Create a class
Create a class called SportsGame. Include the following fields (String league, String team1, String team2, team1Score, team2Score). Include all setters, getters, constructors, toString method. Create a method named Winner that returns a string holding the winning team and the number of points they won by. Be sure that your Winner method can handle a tie. Instantiate the class in a program and call the methods.
Inherit a class
Create a class called Overtime that inherits from SportsGame. Add the following fields (Boolean suddenDeath, int minutes). Override the toString, and constructors. Create the setters and getters. Instantiate and call in a main.
Explanation / Answer
SportsGame.java
public class SportsGame {
// Declaring instance variables
private String league;
private String team1;
private String team2;
private int team1Score;
private int team2Score;
// Parameterized constructor
public SportsGame(String league, String team1, String team2,
int team1Score, int team2Score) {
this.league = league;
this.team1 = team1;
this.team2 = team2;
this.team1Score = team1Score;
this.team2Score = team2Score;
}
// getters and setters
public String getLeague() {
return league;
}
public void setLeague(String league) {
this.league = league;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public int getTeam1Score() {
return team1Score;
}
public void setTeam1Score(int team1Score) {
this.team1Score = team1Score;
}
public int getTeam2Score() {
return team2Score;
}
public void setTeam2Score(int team2Score) {
this.team2Score = team2Score;
}
// This method will finds the winner of the game
public String winner() {
String str = "";
if (team1Score == team2Score)
str = "Tie";
else if (team1Score > team2Score)
str = team1 + " won by " + (team1Score - team2Score);
else if (team2Score > team1Score)
str = team2 + " won by " + (team2Score - team1Score);
return str;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "SportsGame [league=" + league + ", team1=" + team1 + ", team2="
+ team2 + ", team1Score=" + team1Score + ", team2Score="
+ team2Score + "]";
}
}
____________________
Test.java
public class Test {
public static void main(String args[])
{
//Creating an SportsGame Object
SportsGame sg=new SportsGame("Champoins","Essex","Sussex", 10,5);
//Displaying the winner
System.out.println(sg.winner());
}
}
____________________
Output:
Essex won by 5
___________________
Overtime.java
public class Overtime extends SportsGame {
//Declaring instance variables
private boolean suddenDeath;
private int minutes;
//Parameterized constructor
public Overtime(String league, String team1, String team2, int team1Score,
int team2Score, boolean suddenDeath, int minutes) {
super(league, team1, team2, team1Score, team2Score);
this.suddenDeath = suddenDeath;
this.minutes = minutes;
}
//getters and setters
public boolean isSuddenDeath() {
return suddenDeath;
}
public void setSuddenDeath(boolean suddenDeath) {
this.suddenDeath = suddenDeath;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Overtime [suddenDeath=" + suddenDeath + ", minutes=" + minutes
+ "]";
}
______________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.