3.5k views
4 votes
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:Ravens133 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.Here is class WinningTeam:import java.util.Scanner;public class WinningTeam {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);Team team = new Team();String name = scnr.next();int wins = scnr.nextInt();int losses = scnr.nextInt();team.setTeamName(name);team.setTeamWins(wins);team.setTeamLosses(losses);if (team.getWinPercentage() >= 0.5) {System.out.println("Congratulations, Team " + team.getTeamName() +" has a winning average!");}else {System.out.println("Team " + team.getTeamName() +" has a losing average.");}}}

2 Answers

2 votes

Final answer:

To solve the student's request, we created a Java class called Team with methods to set and get the team's name, wins, and losses, as well as a method to calculate the team's winning percentage, using casting to double to avoid integer division.

Step-by-step explanation:

To complete the student's request for a class definition for Team in Java, which should include a method to calculate the winning percentage, here's a simple implementation:

public class Team {
private String teamName;
private int teamWins;
private int teamLosses;

public void setTeamName(String name) {
this.teamName = name;
}

public void setTeamWins(int wins) {
this.teamWins = wins;
}

public void setTeamLosses(int losses) {
this.teamLosses = losses;
}

public String getTeamName() {
return this.teamName;
}

public double getWinPercentage() {
return (double)teamWins / (teamWins + teamLosses);
}
}

Note that we use a cast to double to avoid integer division when calculating getWinPercentage() to ensure a fractional percentage is returned.

User Mustafa Kemal
by
5.3k points
5 votes

Answer:

Step-by-step explanation:

public class Team {

private String teamName;

private int teamWins;

private int teamLosses;

public String getTeamName() {

return teamName;

}

public void setTeamName(String teamName) {

this.teamName = teamName;

}

public int getTeamWins() {

return teamWins;

}

public void setTeamWins(int teamWins) {

this.teamWins = teamWins;

}

public int getTeamLosses() {

return teamLosses;

}

public void setTeamLosses(int teamLosses) {

this.teamLosses = teamLosses;

}

public double getWinPercentage() {

return teamWins / (double) (teamWins + teamLosses);

}

}

User Tomalak
by
5.3k points