119k views
1 vote
18.7 [Contest 7- 07/17] Winning team Given main(), define the Team class (in file Team.java). For class method getWinPercentage(), the formula is: teamWins / (teamWins teamLosses) Note: Use casting to prevent integer division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is Angels 80 82, the output is: Team Angels has a losing average.

User Freda
by
4.1k points

1 Answer

6 votes

Answer:

Here is the Team class.

public class Team { //class name

// private type data members of Team class

private String team_name; //name of the team

private int no_of_wins; //number of team wins

private int no_of_losses; //number of team losses

public String getTeamName() { //accessor to get the name of the team from data field team_name

return team_name;} /return the name of the team

public void setTeamName(String team_name) {//mutator to set the name of the team using data field team_name

this.team_name= team_name;}

/*this keyword is used here because formal parameters and instance variables are same.So it is used to differentiate between local and instance variable. */

public int getTeamWins() {//accessor to get the number of wins from data field no_of_wins

return no_of_wins;}

public void setTeamWins(int no_of_wins) {//mutator to set the team wins using data field no_of_wins

this.no_of_wins = no_of_wins;}

public int getTeamLosses() {//accessor to get the team losses from data field no_of_loses

return no_of_losses;}

public void setTeamLosses(int no_of_losses) {//mutator to set the team losses using data field no_of_loses

this.no_of_losses = no_of_losses;}

public double getWinPercentage() { //formula to compute win percentage

double win_percent; //to hold the value of winning average of team

win_percent =(double)no_of_wins/(no_of_wins+no_of_losses); //computes winning average

return win_percent; }} //returns value of winning percentage

Step-by-step explanation:

Team class has three private data members team_name for the name of the team, no_of_wins to hold the value of the number of wins by the team and no_of_losses hold the value of the number of losses by the team. accessore and mutators are used to access and modify/set the values for each data field. The getWinPercentage() computes the winning percentage by dividing the number of wins to the total number of team wins and losses in order to get the winning average of the team. The result is stored in win_percent. The method then returns the value of win_percent.

The class, main method and output screenshots are attached.

18.7 [Contest 7- 07/17] Winning team Given main(), define the Team class (in file-example-1
18.7 [Contest 7- 07/17] Winning team Given main(), define the Team class (in file-example-2
User Obed Amoasi
by
3.3k points