Write a function, game, that takes two Team objects as parameters and returns the name of the winning team. Function specifications: • The function name: game • The function takes two parameters, both of the type Team. • The function returns the name of the winning team, as a string • To determine the winning team, add up the points associated with each team’s first 4 players. The team with more points is the winner. • If one or both of the teams do not have 4 or more players, your function should return “forfeit” • If the teams have the same total points, your function should return “draw” • The output of “forfeit” takes higher priority than “draw” in the sense that if a team doesn’t have enough players, a game cannot be played, so there could be no draw (for example, if both teams have no players, this would constitute a “forfeit”, not a draw).
Answers
Answer:
State affects behavior, behavior affects state. We know that objects have state and behavior, represented by instance variables and methods. But until now, we haven’t looked at how state and behavior are related. We already know that each instance of a class (each object of a particular type) can have its own unique values for its instance variables. Dog A can have a name “Fido” and a weight of 70 pounds. Dog B is “Killer” and weighs 9 pounds. And if the Dog class has a method makeNoise(), well, don’t you think a 70-pound dog barks a bit deeper than the little 9-pounder? (Assuming that annoying yippy sound can be considered a bark.) Fortunately, that’s the whole point of an object—it has behavior that acts on its state. In other words, methods use instance variable values. Like, “if dog is less than 14 pounds, make yippy sound, else...” or “increase weight by 5”. Let’s go change some state.