Java Arena
Step 5 of 5

The end of Java Programming I

This step ends the material of the MOOC's seventh part, and with it Java Programming I, the first half of the course. First, this module in one list. When a program is bigger than a screen:

  1. Start from a description, and find its parts: a class for the data, a class for the logic, and the part that reads input and prints.
  2. Write down the rules for input before writing code: what ends it, what's valid, and what to print when nothing is.
  3. Build the data class first, then the logic, and the input part last. Try each piece with a test drive, and use placeholders for methods that aren't written yet.
  4. Let the logic answer with return values, null and false included, and let the input part turn those answers into messages.
  5. Read every line of a command before checking any of it, and answer bad input instead of crashing.

What Java Programming I covered

Every program in this module was built from the tools of the earlier ones. Here they are, part by part:

  • Part 1: printing, reading input with a Scanner, variables and their types, calculating, and choosing with if, else if and conditions joined with &&, || and !.
  • Part 2: repeating with while and for, break and continue, and dividing a program into methods with parameters and return values.
  • Part 3: finding bugs, lists and arrays, and taking text apart with the String methods and split.
  • Part 4: classes and objects, with constructors, getters and toString, lists of objects, and reading files.
  • Part 5: designing classes, overloading, primitive and reference variables, null, objects that use other objects, and equals.
  • Part 6: lists inside objects, a user interface kept apart from the program's logic, and unit tests with JUnit.
  • Part 7: procedural and object-oriented programs side by side, sorting and searching, and this module's larger programs.

A final program

This step's challenges put many of those tools into one program. The board-game club of step 1 runs a league: after every game, each player gets 0 to 10 league points, and the club writes them into the file league.csv, one line per player and game, like Mia,8. The program reads the file (part 4), keeps the players in a list inside a logic object and each player's points in a list inside the player (part 6), finds players by name and picks the leader (parts 3 and 5), sorts their names (part 7), and answers commands, bad input included (part 6 and this module). The plan:

Part Class Its job
The data Player a player's name and their points from each game
The logic League the players: find one, add a game's points, the leader, the names in order
Input and output Main and UserInterface read the file, then answer the commands

Here's Player, ready for both challenges:

import java.util.ArrayList;

public class Player {
    private String name;
    private ArrayList<Integer> games;

    public Player(String name) {
        this.name = name;
        this.games = new ArrayList<>();
    }

    public String getName() {
        return this.name;
    }

    public void addGame(int points) {
        this.games.add(points);
    }

    public int total() {
        int total = 0;
        for (int points : this.games) {
            total += points;
        }
        return total;
    }

    public String toString() {
        return this.name + ", total " + this.total() + ", games " + this.games;
    }
}

What comes next

Java Programming II continues from part 8. It opens with a short recap of parts 1 to 7, and then brings the hash map: a collection that finds a value by its key, such as a phone number by a person's name. After that, its parts go on to inheritance and interfaces, streams, exceptions, writing files and graphical user interfaces, among other things. This site will add those modules later. Part 7, and with it Java Programming I, closes with the MOOC's conclusion page on its own site.

Your task

Challenge 1 of 3

The league's logic. Player.java is the class above, and Main.java is ready: it reads league.csv and gives each line to add, and then it tries the other methods out. In League.java, replace the placeholders:

  • find(String name) returns the player with that name, or null.
  • add(String playerName, int points) adds a game's points to the player. When there's no player by that name yet, it first creates one and adds it to the list.
  • leader() returns the player with the most points in all, or null when there are no players. If two players have the same total, the one added first.
  • playerNames() returns a new list of the players' names in alphabetical order. Sort it with Collections.sort.

League doesn't print anything. For the file shown below, the program prints:

Players: [Jonas, Mia, Priya]
Leader: Mia, total 23, games [8, 7, 8]
Priya, total 22, games [6, 9, 7]
null

Challenge 2

The league's user interface. League.java (with the methods of the previous challenge), Player.java and Main.java are ready: main reads league.csv into a League and starts the user interface. In UserInterface.java, the loop, quit, leader, the answer to unknown commands and the lesson's toNumber are ready too. Write the two empty branches:

  • add, then a player's name and a line with the points: the points must be a whole number from 0 to 10, and otherwise print Points must be a whole number from 0 to 10. With valid points, add the game's points (a name that's new makes a new player) and print the player.
  • compare, then two lines, each with a player's name. Read both lines first. If the first player isn't in the league, print No player called and that name, and otherwise the same for the second. When both are there, print who has more points in all, the one with more points first, like Priya is ahead of Mia: 24 to 15. When their totals are the same, print the two names in the order they were typed, like Mia and Jonas are tied at 15.

For the input shown and the file shown below, the program prints:

> Points must be a whole number from 0 to 10
> Priya, total 24, games [6, 9, 9]
> Priya is ahead of Mia: 24 to 15
> No player called Pia
> Jonas, total 15, games [9, 5, 1]
> Mia and Jonas are tied at 15
> Leader: Priya, total 24, games [6, 9, 9]
> Unknown command: score
> Good night

Challenge 3

One last trace through the tools of Java Programming I: a class of your own, a list of objects, split, loops inside loops, references and sorting. What does this program print? Look closely at what tallies holds.