From the course: Level Up: Java

Determine the winner - Java Tutorial

From the course: Level Up: Java

Determine the winner

- [Instructor] It's the holidays. Your family's in town and you just played a few rounds of a guessing game. The game had a series of rounds and during each round a given player and their partner got a series of points. Given the team's results from each round, your challenge is to write a program that automatically determines the overall winner and prints out the final score results in descending order. We start you off with a bit of sample code. We have a team class for representing each team as well as an app class for where the main code will be run. We also have a utility function in the TeamUtil class. This generates and sets some sample round scores from each team. Your challenge is to implement the reveal results function. It should print out the winner as well as the rest of the game results in descending order. Good luck. (soft music) Let's dive in to the reveal results implementation. We begin by validating the input. If there are no teams playing or no rounds have been played yet, then the game must not have started. So we print a message and exit the function. If there are results to calculate, we organize them in a tree map. A tree map allows us to organize our teams by their final score, where the key in the map is the score and the value is the list of teams with that score. This helps us keep track if there's a tie. We also use a tree map because we can access the keys in a descending order and we can easily iterate through the results starting with the winner. To calculate each team's final score, we use the sumTotal score function from the team class. In this function, we add up all the positive scores in the score list. The reduced function reduces our list into an integer by summing each item in the list. If the list is empty or does not contain positive numbers, the or else clause allows us to return zero. Back in reveal results, we create an iterator out of the maps' keys. The iterator will allow us to iterate through the scores with .next and we can announce each result. Let's look at the implementation. Here we access the list of teams with the given score from our map, and we announce the results with the players' names and the score. If there are multiple teams with the same score, we also announce that it's a tie. After announcing the winner, we've reused the announced result functionality to announce subsequent players in descending order. Let's run this. All of the scores are positive, and this looks correct but we should verify the functionality with automated tests. In our team test, we verify the accessibility of our attributes as well as our sum score function. We ensure that when there are negative scores in our score list, we don't count them. Another way to do this is to provide a setter and ensure no negative scores are ever added. It's up to you whether you want to filter out invalid data when you set it or when you retrieve it. In our teamUtils test, we test our reveal results function by creating cases with ties, with no teams and more. Notice we don't test our private functions here because they're tested through our public methods. I encourage you to add on to this program. What if the user could input their team names or input each round score? You could also add different games that reuse the reveal results functionality by adding interfaces and leveraging polymorphism. There's always a way to enhance every program you make.

Contents