I just need help on this last step! Write a method called havePlayedTogether(Str
ID: 3797786 • Letter: I
Question
I just need help on this last step! Write a method called havePlayedTogether(String teamA, String teamB) in the League class that returns a boolean indicating whether or not the teams with the given names have played together or not. You should make use of the gamesPlayed array that you just filled in. Once completed, append the following code to the LeagueTestProgram and compile/run it.
Here's some of my code:
public class League
{
private String name;
private int teamCount;
private Team[] teams;
private int [] [] gamesPlayed = new int[8][8];
public String getName() { return(name); }
public Team[] getTeams() { return(teams); }
public void setName(String aName) { name = aName; }
public League(String aName) {
name = aName;
teamCount = 0;
teams = new Team[8]; //This does not actually make any Team objects
}
//This is the toString method that specifies the appearance of the League
public String toString() {
return("The " + name + " league with " + teamCount + " teams.");
}
//This method adds a new team to the league
public int addTeam(Team aTeam) {
if (teamCount < 8) {
teams[teamCount++] = aTeam;
return teamCount;
}
return -1;
}
//This method prints out all of the teams
public void showTeams() {
for(int i=0; i<teamCount; i++)
System.out.println(teams[i].toString());
}
/****************** For LeagueTester1 *******************************/
//Record Win and Loss and updates
public void recordWinAndLoss(Team winner, Team loser)
{
winner.setWins(winner.getWins() + 1);
loser.setLosses(loser.getLosses() + 1);
int x = indexOf(winner);
int y = indexOf(loser);
gamesPlayed[x][y] += 1;
gamesPlayed[y][x] += 1;
}
//Record a Tie game and updates
public void recordTie(Team teamA, Team teamB)
{
teamA.setTies(teamA.getTies() + 1);
teamB.setTies(teamB.getTies() + 1);
int x = indexOf(teamA);
int y = indexOf(teamB);
gamesPlayed[x][y] += 1;
gamesPlayed[y][x] += 1;
}
//Find the team with the given name. If none exists, return null
public Team teamWithName(String aName) {
for(int i=0; i<teamCount; i++) {
if (teams[i].getName().equals(aName))
return teams[i];
}
return null;
}
//Record Win and Loss
public void recordWinAndLoss(String winner, String loser) {
Team winningTeam, losingTeam;
if ((winningTeam = this.teamWithName(winner)) == null) {
System.out.println("Invalid Team name: " + winner);
return;
}
if ((losingTeam = this.teamWithName(loser)) == null) {
System.out.println("Invalid Team name: " + loser);
return;
}
recordWinAndLoss(winningTeam, losingTeam);
}
//Record a Tie game
public void recordTie(String teamAName, String teamBName) {
Team teamA, teamB;
if ((teamA = this.teamWithName(teamAName)) == null) {
System.out.println("Invalid Team name: " + teamAName);
return;
}
if ((teamB = this.teamWithName(teamBName)) == null) {
System.out.println("Invalid Team name: " + teamBName);
return;
}
recordTie(teamA, teamB);
}
//Return the total number of games played in the League
public int totalGamesPlayed() {
int total=0;
for (int i=0; i<teamCount; i++)
total += teams[i].gamesPlayed();
return total/2;
}
//Return the team with the most points
public Team firstPlaceTeam() {
Team best;
if (teamCount == 0)
return null;//If there are no teams, then no team is in first place
best = teams[0];
for (int i=1; i<teamCount; i++) {
if (best.totalPoints() < teams[i].totalPoints())
best = teams[i];
}
return best;
}
//Return the team with the least points
public Team lastPlaceTeam() {
Team worst;
if (teamCount == 0)
return null;//If there are no teams, then no team is in last place
worst = teams[0];
for (int i=1; i<teamCount; i++) {
if (worst.totalPoints() > teams[i].totalPoints())
worst = teams[i];
}
return worst;
}
//Returns the team who played the most
public Team teamMostPlayed()
{
Team most;
if (teamCount == 0)
return null;
most = teams[0];
for (int i=1; i<teamCount; i++) {
if (most.gamesPlayed() < teams[i].gamesPlayed())
most = teams[i];
}
return most;
}
//Returns undefeated teams
public Team[] undefeated()
{
int count= 0;
for(int i = 0; i < teams.length; i++)
{
if(teams[i].getLosses() == 0)
{
count = count + 1;
}
}
Team[] undefeated = new Team[count];
count = 0;
for(int i = 0; i < teams.length; i++)
{
if(teams[i].getLosses() == 0)
{
undefeated[count] = teams[i];
count = count + 1;
}
}
return undefeated;
}
//returns index of specific team of a league
public int indexOf(Team t)
{
for(int i=0; i<teams.length; i++)
{
if(teams[i] == t)
return i;
}
return -1;
}
//This method prints out a table showing which teams played which other teams
public void showTable()
{
System.out.println(" 0 1 2 3 4 5 6 7 ");
System.out.println(" -------------------------------");
for(int row=0; row<8; row++)
{
System.out.print(row +" | ");
for(int col=0; col<8; col++)
{
System.out.print(gamesPlayed[row][col] + " | ");
}
System.out.println(" -------------------------------");
}
for(int i=0; i<8; i++)
System.out.println(i + " = " + teams[i].getName());
}
public class League
{
private String name;
private int teamCount;
private Team[] teams;
private int [] [] gamesPlayed = new int[8][8];
public String getName() { return(name); }
public Team[] getTeams() { return(teams); }
public void setName(String aName) { name = aName; }
public League(String aName) {
name = aName;
teamCount = 0;
teams = new Team[8]; //This does not actually make any Team objects
}
//This is the toString method that specifies the appearance of the League
public String toString() {
return("The " + name + " league with " + teamCount + " teams.");
}
//This method adds a new team to the league
public int addTeam(Team aTeam) {
if (teamCount < 8) {
teams[teamCount++] = aTeam;
return teamCount;
}
return -1;
}
//This method prints out all of the teams
public void showTeams() {
for(int i=0; i<teamCount; i++)
System.out.println(teams[i].toString());
}
/****************** For LeagueTester1 *******************************/
//Record Win and Loss and updates
public void recordWinAndLoss(Team winner, Team loser)
{
winner.setWins(winner.getWins() + 1);
loser.setLosses(loser.getLosses() + 1);
int x = indexOf(winner);
int y = indexOf(loser);
gamesPlayed[x][y] += 1;
gamesPlayed[y][x] += 1;
}
//Record a Tie game and updates
public void recordTie(Team teamA, Team teamB)
{
teamA.setTies(teamA.getTies() + 1);
teamB.setTies(teamB.getTies() + 1);
int x = indexOf(teamA);
int y = indexOf(teamB);
gamesPlayed[x][y] += 1;
gamesPlayed[y][x] += 1;
}
//Find the team with the given name. If none exists, return null
public Team teamWithName(String aName) {
for(int i=0; i<teamCount; i++) {
if (teams[i].getName().equals(aName))
return teams[i];
}
return null;
}
//Record Win and Loss
public void recordWinAndLoss(String winner, String loser) {
Team winningTeam, losingTeam;
if ((winningTeam = this.teamWithName(winner)) == null) {
System.out.println("Invalid Team name: " + winner);
return;
}
if ((losingTeam = this.teamWithName(loser)) == null) {
System.out.println("Invalid Team name: " + loser);
return;
}
recordWinAndLoss(winningTeam, losingTeam);
}
//Record a Tie game
public void recordTie(String teamAName, String teamBName) {
Team teamA, teamB;
if ((teamA = this.teamWithName(teamAName)) == null) {
System.out.println("Invalid Team name: " + teamAName);
return;
}
if ((teamB = this.teamWithName(teamBName)) == null) {
System.out.println("Invalid Team name: " + teamBName);
return;
}
recordTie(teamA, teamB);
}
//Return the total number of games played in the League
public int totalGamesPlayed() {
int total=0;
for (int i=0; i<teamCount; i++)
total += teams[i].gamesPlayed();
return total/2;
}
//Return the team with the most points
public Team firstPlaceTeam() {
Team best;
if (teamCount == 0)
return null;//If there are no teams, then no team is in first place
best = teams[0];
for (int i=1; i<teamCount; i++) {
if (best.totalPoints() < teams[i].totalPoints())
best = teams[i];
}
return best;
}
//Return the team with the least points
public Team lastPlaceTeam() {
Team worst;
if (teamCount == 0)
return null;//If there are no teams, then no team is in last place
worst = teams[0];
for (int i=1; i<teamCount; i++) {
if (worst.totalPoints() > teams[i].totalPoints())
worst = teams[i];
}
return worst;
}
//Returns the team who played the most
public Team teamMostPlayed()
{
Team most;
if (teamCount == 0)
return null;
most = teams[0];
for (int i=1; i<teamCount; i++) {
if (most.gamesPlayed() < teams[i].gamesPlayed())
most = teams[i];
}
return most;
}
//Returns undefeated teams
public Team[] undefeated()
{
int count= 0;
for(int i = 0; i < teams.length; i++)
{
if(teams[i].getLosses() == 0)
{
count = count + 1;
}
}
Team[] undefeated = new Team[count];
count = 0;
for(int i = 0; i < teams.length; i++)
{
if(teams[i].getLosses() == 0)
{
undefeated[count] = teams[i];
count = count + 1;
}
}
return undefeated;
}
//returns index of specific team of a league
public int indexOf(Team t)
{
for(int i=0; i<teams.length; i++)
{
if(teams[i] == t)
return i;
}
return -1;
}
//This method prints out a table showing which teams played which other teams
public void showTable()
{
System.out.println(" 0 1 2 3 4 5 6 7 ");
System.out.println(" -------------------------------");
for(int row=0; row<8; row++)
{
System.out.print(row +" | ");
for(int col=0; col<8; col++)
{
System.out.print(gamesPlayed[row][col] + " | ");
}
System.out.println(" -------------------------------");
}
for(int i=0; i<8; i++)
System.out.println(i + " = " + teams[i].getName());
}
System.out.println (aLeague. have PlayedTogether ("Chicago B ull Boston Celtics")) System.out.println (aLeague. have PlayedTogether ("New York Knicks "Detroit Pistons") System.out.println (a League have Played Together Detroit Pistons", "Atlanta Hawks") System.out.println (aLeague. have PlayedTogether ("Chicago B ull "New York Knicks") System.out.println (aLeague. have PlayedTogether ("Atlanta Hawks "New York Knicks'')); System.out.println (aLeague have PlayedTogether ("Indiana Pacers "Boston Celtics")) System.out.println (a League have Played Together ("Toronto Raptors "New Jersey Nets")) Nets", "New System.out.println (aLeague. have PlayedTogether ("New Jerse York Knicks'')); System.out.println (a League have PlayedTogether ("Boston Celtics" "Detroit Pistons")); System.out.println (a League have PlayedTogether Detroit Pistons "New Jersey Nets")); System.out.println (a League have playedTogether I Love 2017 Frank") System.out.println (a League have PlayedTogether ("Chicago B ull "Chicago Bulls") The resulting output should be true and false values as following: false false false true false true true false false false false falseExplanation / Answer
/// Checks whether two teams have already played together.