Java Arena
Step 1 of 7

A class as a box

A program of two or three classes fits in your head. A program of ten classes, each with its instance variables, constructors and methods, doesn't, and neither does a program that a team is about to write together. So programmers draw their classes: a box for each class, and lines between the boxes for how the classes are connected. Such a drawing is a class diagram.

Class diagrams have a standard form, part of UML, the Unified Modeling Language, so that a diagram drawn by one programmer can be read by anyone who knows UML. This module teaches you to read them, to turn them into classes, and to draw the classes you write.

A box with fields

A recycling center has a bin for each material: glass, paper, metal and so on. Its program keeps each bin in an object of this class:

public class Bin {
    private String material;
    private int capacity;
    private int load;
}

In a class diagram, the class is a box:

The diagram as text
  • Bin, a class
    • private field material: String
    • private field capacity: int
    • private field load: int
  • The class's name is at the top, in bold.
  • Under a line come the class's instance variables, one per line. Diagrams call them fields (or attributes).
  • Each field is written as its name, a colon and its type: capacity: int. That's the other way around from Java, where the type comes first (int capacity).
  • The sign in front is the field's visibility: - for private and + for public. Since this course keeps instance variables private, nearly every field you'll see starts with -. (You may also meet # for protected, from module 34.)

Under each drawing, "The diagram as text" says the same in words, which is handy when you want to check what a sign means.

A diagram is a plan, not a program: it doesn't run, and it may leave things out. This step's boxes show only fields. The next step adds constructors and methods.

From a box to a class

A box tells you what to write. Here's the box of a pickup: a load that the center's truck collects from an address.

The diagram as text
  • Pickup, a class
    • private field address: String
    • private field kilos: double
    • private field done: boolean

Each line becomes one instance variable, in the same order:

  • -address: String is private String address;
  • -kilos: double is private double kilos;
  • -done: boolean is private boolean done;

With a constructor and a toString, which this box doesn't show, the class can be used:

Main.java
public class Main {
    public static void main(String[] args) {
        Pickup pickup = new Pickup("12 Mill Road", 35.5);
        System.out.println(pickup);
    }
}
Pickup.java
public class Pickup {
    private String address;
    private double kilos;
    private boolean done;

    public Pickup(String address, double kilos) {
        this.address = address;
        this.kilos = kilos;
        this.done = false;
    }

    @Override
    public String toString() {
        return this.address + ": " + this.kilos + " kg";
    }
}
Output
12 Mill Road: 35.5 kg

The types in a box are Java's own: int, double, boolean, String, or ArrayList<String> for a field that holds a list of Strings. Write them exactly as the box says. A class with an int where its box says double, or a public field where the box says -, no longer matches the plan, and code that others wrote from the same plan may not work with it.

Your task

Challenge 1 of 3

A hospital ward keeps track of its patients. Here's the box of a patient:

The diagram as text
  • Patient, a class
    • private field name: String
    • private field room: int
    • private field temperature: double

Fill in the blanks in Patient.java, so that its instance variables match the box. The constructor and toString are ready.

Challenge 2

The ward's nurses have a class too. Nurse.java has a constructor and two methods, but its instance variables are missing, so it doesn't compile. Add them, as this box shows:

The diagram as text
  • Nurse, a class
    • private field name: String
    • private field nightShift: boolean
    • private field patients: int

Leave the constructor and the methods as they are. The program should print:

Tomas Berg (night shift): 2 patients

Challenge 3

A garden center planned its shrubs with this box:

The diagram as text
  • Shrub, a class
    • private field species: String
    • private field height: double
    • private field potLiters: int

In Shrub.java, two instance variables don't match the box, and the program prints Lilac: 19 cm, pot 10 liters: half centimeters get lost along the way. Fix those two lines, and leave the constructor and the methods as they are. The program should print:

Lilac: 20.0 cm, pot 10 liters