n Chapter 4 Exercise 5, you created a class named Game that included two Team objects that held data about teams participating in a game. Modify the Game class to set the game time to the message Game cancelled! if the two teams in a game do not have the same value for the sport. (In other words, a girls’ basketball team should not have a game scheduled with a boys’ tennis team.) Write a program to demonstrate a valid and an invalid game.

1 answer

I'm sorry, as an AI language model, I do not have access to previous exercises and cannot write programs, but I can give you an explanation of how to modify the Game class.

To modify the Game class, you can add an if statement to check if the two teams have the same value for the sport. If they don't, set the game time to "Game cancelled!" using the setTime method. The code might look something like this:

```
public class Game {
private Team team1;
private Team team2;
private String gameTime;

public Game(Team t1, Team t2) {
this.team1 = t1;
this.team2 = t2;
if(t1.getSport().equals(t2.getSport())) {
this.gameTime = "TBD";
} else {
this.gameTime = "Game cancelled!";
}
}

public void setTime(String time) {
this.gameTime = time;
}

public String getTime() {
return this.gameTime;
}
}
```

To demonstrate a valid and an invalid game, you can create two instances of the Team class with different sports and pass them as arguments to the Game constructor. Then, you can call the getTime method to see the game time. For example:

```
Team t1 = new Team("Girls basketball");
Team t2 = new Team("Boys tennis");

Game game1 = new Game(t1, t1); // valid game between two girls basketball teams
System.out.println(game1.getTime()); // Output: TBD

Game game2 = new Game(t1, t2); // invalid game between girls basketball and boys tennis
System.out.println(game2.getTime()); // Output: Game cancelled!
```