Java Arena
Step 2 of 7

Constructors and methods in a box

A box has room for more than fields. Under a second line come the class's constructors and methods. Here's the recycling center's bin with them:

The diagram as text
  • Bin, a class
    • private field material: String
    • private field capacity: int
    • private field load: int
    • public constructor Bin(material: String, capacity: int)
    • public method getMaterial(): String
    • public method add(kilos: int): boolean
    • public method empty(): void
    • public method toString(): String
  • A constructor is the class's name with its parameters in parentheses, and nothing after them: a constructor has no return type, in a diagram as in Java.
  • A method is its name, its parameters in parentheses, a colon and its return type: getMaterial(): String returns a String. A method that returns nothing has void.
  • Each parameter is written like a field, name first: add(kilos: int) takes an int called kilos. Parameters are separated by commas, and a method without parameters has empty parentheses.
  • The sign in front is the visibility again. Constructors and methods that other classes call are public, +. A private helper method gets a -.
  • Constructors come first, then methods.

So each line gives you a header:

In the box In Java
+Bin(material: String, capacity: int) public Bin(String material, int capacity)
+getMaterial(): String public String getMaterial()
+add(kilos: int): boolean public boolean add(int kilos)
+empty(): void public void empty()

toString is one of Bin's methods, so it has a line too. @Override isn't shown: a box shows headers, and nothing above them or inside them.

What a box doesn't say

The box gives every header, but not what the methods do. Does add refuse a load that doesn't fit? What does toString return? A diagram leaves that to the code, or, before the code exists, to words. The team that drew this box agreed that a new bin is empty, that add puts the load in and returns true only if the bin's load stays within its capacity (otherwise it changes nothing and returns false), and that empty sets the load back to 0. With those words and the box, the class almost writes itself:

Main.java
public class Main {
    public static void main(String[] args) {
        Bin glass = new Bin("glass", 500);
        System.out.println(glass.add(300));
        System.out.println(glass.add(250));
        System.out.println(glass);
        glass.empty();
        System.out.println(glass.add(250) + " " + glass.getMaterial());
    }
}
Bin.java
public class Bin {
    private String material;
    private int capacity;
    private int load;

    public Bin(String material, int capacity) {
        this.material = material;
        this.capacity = capacity;
        this.load = 0;
    }

    public String getMaterial() {
        return this.material;
    }

    public boolean add(int kilos) {
        if (this.load + kilos > this.capacity) {
            return false;
        }
        this.load += kilos;
        return true;
    }

    public void empty() {
        this.load = 0;
    }

    @Override
    public String toString() {
        return this.material + ": " + this.load + "/" + this.capacity + " kg";
    }
}
Output
true
false
glass: 300/500 kg
true glass

The challenges in this module work the same way: a box, and words that say what the methods do. Follow the box's headers exactly. Other classes call the methods by those names, with those types, and a header that's different can make their calls fail to compile, or quietly change what they compute.

Your task

Challenge 1 of 3

The ward gives each of its beds an object. Write the class Bed in Bed.java, as this box shows:

The diagram as text
  • Bed, a class
    • private field number: int
    • private field patient: String
    • public constructor Bed(number: int)
    • public method isFree(): boolean
    • public method assign(name: String): void
    • public method release(): void
    • public method toString(): String
  • A new bed is free: its patient is null.
  • assign puts the patient's name in the bed, and release makes the bed free again.
  • isFree says whether the bed is free.
  • toString returns Bed 4: free for a free bed and Bed 4: Ines Moreau for a bed with a patient, with the bed's own number and patient.

The check runs:

Bed bed = new Bed(4);
System.out.println(bed + " " + bed.isFree());
bed.assign("Ines Moreau");
System.out.println(bed + " " + bed.isFree());
bed.release();
System.out.println(bed);

and expects:

Bed 4: free true
Bed 4: Ines Moreau false
Bed 4: free

Challenge 2

A garden center's greenhouses open their vents when it gets warm. The team drew this box, and Main.java was written from it:

The diagram as text
  • Greenhouse, a class
    • private field name: String
    • private field degrees: double
    • private field open: boolean
    • public constructor Greenhouse(name: String, degrees: double)
    • public method getName(): String
    • public method getDegrees(): double
    • public method isOpen(): boolean
    • public method warm(by: double): void
    • public method openVents(): void

In Greenhouse.java, three method headers don't match the box, so the program doesn't compile. Fix them (and anything inside those methods that has to change with them), so that the class matches the box. Leave Main.java as it is. The program should print:

Palm house: true, 26.0 degrees

Challenge 3

A board-game café lends games to its guests, and pieces go missing. Write the class GameBox in GameBox.java, as this box shows:

The diagram as text
  • GameBox, a class
    • private field title: String
    • private field pieces: int
    • private field missing: int
    • public constructor GameBox(title: String, pieces: int)
    • public method lose(count: int): void
    • public method find(count: int): void
    • public method isComplete(): boolean
    • public method toString(): String
    • private method present(): int
  • A new box has all its pieces: none are missing.
  • lose adds to the missing pieces. find takes found pieces off them, but never below 0.
  • isComplete says whether no pieces are missing.
  • present is a private helper that returns how many pieces are in the box. toString uses it, and returns Harbor Lights: 80 of 84 pieces, with the box's own title and numbers.

The check runs:

GameBox box = new GameBox("Harbor Lights", 84);
box.lose(5);
box.find(1);
System.out.println(box + " " + box.isComplete());
box.find(10);
System.out.println(box + " " + box.isComplete());

and expects:

Harbor Lights: 80 of 84 pieces false
Harbor Lights: 84 of 84 pieces true