Instance variables: a) Declare instance variables named homeScore and visScore t
ID: 3715525 • Letter: I
Question
Instance variables:
a) Declare instance variables named homeScore and visScore that are ints
b) Declare instance variables named homeTeam and visTeam that are Strings
Instance methods:
c) Write a constructor that will receive four parameters – two Strings and two ints corresponding to the home and visiting teams, then the home and visiting scores. The constructor will assign them to appropriate instance variables.
d) Write accessor methods for the score variables (one for each.)
e) Write mutator methods for the score variables (one for each.).
f) Write a toString() method that will display the winning team’s (higher score
first).name and score, followed by the other team. If it’s a tie, show the home team first
} // end class
Explanation / Answer
public class Game { // declare instance variables here private int homeScore; private int visScore; private String homeTeam; private String visTeam; // write the answer to “c” here public Game(int homeScore, int visScore, String homeTeam, String visTeam) { this.homeScore = homeScore; this.visScore = visScore; this.homeTeam = homeTeam; this.visTeam = visTeam; } // write the answer to “d” here public int getHomeScore() { return homeScore; } public int getVisScore() { return visScore; } public String getHomeTeam() { return homeTeam; } public String getVisTeam() { return visTeam; } // write the answer to “e” here public void setHomeScore(int homeScore) { this.homeScore = homeScore; } public void setVisScore(int visScore) { this.visScore = visScore; } public void setHomeTeam(String homeTeam) { this.homeTeam = homeTeam; } public void setVisTeam(String visTeam) { this.visTeam = visTeam; } // write the answer to “f” here public String toString() { if (homeScore >= visScore) return homeTeam + " " + getHomeScore() + " - " + visTeam + " " + getVisScore(); else return visTeam + " " + getVisScore() + " - " + homeTeam + " " + getHomeScore(); } }