{"id":"class-diagrams-connections","slug":"connections","title":"Arrows between classes","text":"So far each box stood alone. In a program, objects hold references to other objects, and a class diagram shows that with lines between the boxes.\n\n### A field that refers to another class\n\nA theater stages plays, and each play gets many performances. The program has a class for each, and a Performance holds a reference to its Play:\n\n```java\npublic class Performance {\n    private Play play;\n    private String date;\n    private int seatsSold;\n\n    // the constructor and methods\n}\n```\n\nIn a diagram, a field whose type is another class of the diagram isn't written in the box. It's drawn as an arrow, from the box of the class that has the field to the box of the class it refers to:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Play and Performance\">\n<svg class=\"uml-svg\" viewBox=\"0 0 302 296\" width=\"302\" height=\"296\" style=\"width:18.88rem;min-width:17.31rem;aspect-ratio:302/296\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M151 170L151 126\"/><path class=\"u-edge\" fill=\"none\" d=\"M156.5 136L151 126L145.5 136\"/><rect class=\"u-bg\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"24\"/><text x=\"151\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Play</text><text x=\"28.5\" y=\"46\">-title: String</text><text x=\"28.5\" y=\"62\">-minutes: int</text><text x=\"28.5\" y=\"86\">+Play(title: String, minutes: int)</text><text x=\"28.5\" y=\"102\">+getTitle(): String</text><text x=\"28.5\" y=\"118\">+getMinutes(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M20.5 30h261M20.5 70h261\"/><rect class=\"u-frame\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"170\" width=\"290\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"170\" width=\"290\" height=\"24\"/><text x=\"151\" y=\"186\" text-anchor=\"middle\" class=\"u-name\">Performance</text><text x=\"14\" y=\"210\">-date: String</text><text x=\"14\" y=\"226\">-seatsSold: int</text><text x=\"14\" y=\"250\">+Performance(play: Play, date: String)</text><text x=\"14\" y=\"266\">+sell(seats: int): void</text><text x=\"14\" y=\"282\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 194h290M6 234h290\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"170\" width=\"290\" height=\"120\"/><text x=\"159\" y=\"139\" class=\"u-mult\">1</text><text x=\"145\" y=\"152\" text-anchor=\"end\" class=\"u-role\">play</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Play</strong>, a class<ul><li>private field <code>title: String</code></li><li>private field <code>minutes: int</code></li><li>public constructor <code>Play(title: String, minutes: int)</code></li><li>public method <code>getTitle(): String</code></li><li>public method <code>getMinutes(): int</code></li></ul></li><li><strong>Performance</strong>, a class<ul><li>private field <code>date: String</code></li><li>private field <code>seatsSold: int</code></li><li>public constructor <code>Performance(play: Play, date: String)</code></li><li>public method <code>sell(seats: int): void</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Performance has one Play object (play).</li></ul></details></figure>\n\n\n- The arrow goes **from** Performance **to** Play: a Performance knows its Play. It has a field of type Play, so its methods can call the play's methods.\n- A Play doesn't know its performances. It has no field for them, and the arrow doesn't point back: given a Play object, there's no way to reach its performances.\n- The `1` at the arrowhead is the **multiplicity**: each Performance has exactly one Play. An arrow without a number at its head means one, too.\n- The name by the arrow, `play`, is the field's name. It's optional, and handy when a class has two fields of the same type.\n- `play: Play` in the constructor is a parameter, so it stays in the box. Only the field turns into an arrow.\n- `date: String` stays in the box, though String is a class too. Arrows connect the program's own classes, the ones drawn in the diagram. Java's classes, such as String and ArrayList, are written as types.\n\nHere's the code:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Play hamlet = new Play(\"Hamlet\", 190);\n        Performance friday = new Performance(hamlet, \"May 9\");\n        Performance saturday = new Performance(hamlet, \"May 10\");\n        friday.sell(120);\n        saturday.sell(95);\n        friday.sell(30);\n        System.out.println(friday);\n        System.out.println(saturday);\n    }\n}\n// ==== Play.java ====\npublic class Play {\n    private String title;\n    private int minutes;\n\n    public Play(String title, int minutes) {\n        this.title = title;\n        this.minutes = minutes;\n    }\n\n    public String getTitle() {\n        return this.title;\n    }\n\n    public int getMinutes() {\n        return this.minutes;\n    }\n}\n// ==== Performance.java ====\npublic class Performance {\n    private Play play;\n    private String date;\n    private int seatsSold;\n\n    public Performance(Play play, String date) {\n        this.play = play;\n        this.date = date;\n        this.seatsSold = 0;\n    }\n\n    public void sell(int seats) {\n        this.seatsSold += seats;\n    }\n\n    @Override\n    public String toString() {\n        return this.play.getTitle() + \" (\" + this.play.getMinutes() + \" min), \" + this.date + \": \" + this.seatsSold + \" seats sold\";\n    }\n}\n```\n\n```output\nHamlet (190 min), May 9: 150 seats sold\nHamlet (190 min), May 10: 95 seats sold\n```\n\nBoth performances refer to the same Play object. \"Each Performance has one Play\" says how many plays a performance has, not that their plays are different objects: many performances can share one play, as `friday` and `saturday` do.\n\n### One, or none\n\nA reference can also be `null`. A multiplicity of `0..1` at the arrowhead means at most one: zero or one. Say a critic may review a performance, or not:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Performance and Review\">\n<svg class=\"uml-svg\" viewBox=\"0 0 320 84\" width=\"320\" height=\"84\" style=\"width:20rem;min-width:18.34rem;aspect-ratio:320/84\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M130 42L189.2 42\"/><path class=\"u-edge\" fill=\"none\" d=\"M179.2 47.5L189.2 42L179.2 36.5\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"124\" height=\"72\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"124\" height=\"24\"/><text x=\"68\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Performance</text><text x=\"14\" y=\"46\">-date: String</text><text x=\"14\" y=\"62\">-seatsSold: int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 30h124M6 70h124\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"124\" height=\"72\"/><rect class=\"u-bg\" fill=\"none\" x=\"189.2\" y=\"6\" width=\"124\" height=\"72\"/><rect class=\"u-head\" fill=\"none\" x=\"189.2\" y=\"6\" width=\"124\" height=\"24\"/><text x=\"251.2\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Review</text><text x=\"197.2\" y=\"46\">-critic: String</text><text x=\"197.2\" y=\"62\">-stars: int</text><path class=\"u-sep\" fill=\"none\" d=\"M189.2 30h124M189.2 70h124\"/><rect class=\"u-frame\" fill=\"none\" x=\"189.2\" y=\"6\" width=\"124\" height=\"72\"/><text x=\"183.2\" y=\"35\" text-anchor=\"end\" class=\"u-mult\">0..1</text><text x=\"159.6\" y=\"57\" text-anchor=\"middle\" class=\"u-role\">review</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Performance</strong>, a class<ul><li>private field <code>date: String</code></li><li>private field <code>seatsSold: int</code></li></ul></li><li><strong>Review</strong>, a class<ul><li>private field <code>critic: String</code></li><li>private field <code>stars: int</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Performance has at most one Review object (review).</li></ul></details></figure>\n\n\nPerformance's field `review` is `null` until a review comes in, so code that uses it has to check for `null` first. With `1`, the code can count on the field always referring to an object.\n","task":"The theater's costume department sews each costume for one actor, who wears it as one character. `Actor.java` is ready. Write the class `Costume` in `Costume.java`, as the diagram shows:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Actor and Costume\">\n<svg class=\"uml-svg\" viewBox=\"0 0 324 280\" width=\"324\" height=\"280\" style=\"width:20.25rem;min-width:18.57rem;aspect-ratio:324/280\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M162 170L162 126\"/><path class=\"u-edge\" fill=\"none\" d=\"M167.5 136L162 126L156.5 136\"/><rect class=\"u-bg\" fill=\"none\" x=\"35\" y=\"6\" width=\"254\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"35\" y=\"6\" width=\"254\" height=\"24\"/><text x=\"162\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Actor</text><text x=\"43\" y=\"46\">-name: String</text><text x=\"43\" y=\"62\">-height: int</text><text x=\"43\" y=\"86\">+Actor(name: String, height: int)</text><text x=\"43\" y=\"102\">+getName(): String</text><text x=\"43\" y=\"118\">+getHeight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M35 30h254M35 70h254\"/><rect class=\"u-frame\" fill=\"none\" x=\"35\" y=\"6\" width=\"254\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"170\" width=\"312\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"170\" width=\"312\" height=\"24\"/><text x=\"162\" y=\"186\" text-anchor=\"middle\" class=\"u-name\">Costume</text><text x=\"14\" y=\"210\">-character: String</text><text x=\"14\" y=\"234\">+Costume(character: String, actor: Actor)</text><text x=\"14\" y=\"250\">+getActor(): Actor</text><text x=\"14\" y=\"266\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 194h312M6 218h312\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"170\" width=\"312\" height=\"104\"/><text x=\"170\" y=\"139\" class=\"u-mult\">1</text><text x=\"156\" y=\"152\" text-anchor=\"end\" class=\"u-role\">actor</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Actor</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>height: int</code></li><li>public constructor <code>Actor(name: String, height: int)</code></li><li>public method <code>getName(): String</code></li><li>public method <code>getHeight(): int</code></li></ul></li><li><strong>Costume</strong>, a class<ul><li>private field <code>character: String</code></li><li>public constructor <code>Costume(character: String, actor: Actor)</code></li><li>public method <code>getActor(): Actor</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Costume has one Actor object (actor).</li></ul></details></figure>\n\n\n`getActor` returns the costume's actor, and `toString` returns `Puck, for Noor Haddad (172 cm)`, with the costume's own character and its actor's name and height. The check runs:\n\n```java\nActor noor = new Actor(\"Noor Haddad\", 172);\nCostume puck = new Costume(\"Puck\", noor);\nCostume moth = new Costume(\"Moth\", noor);\nSystem.out.println(puck);\nSystem.out.println(moth.getActor().getName());\n```\n\nand expects:\n\n```\nPuck, for Noor Haddad (172 cm)\nNoor Haddad\n```\n","kind":"code","seed":"// ==== Costume.java ====\npublic class Costume {\n\n}\n\n// ==== Actor.java ====\npublic class Actor {\n    private String name;\n    private int height;\n\n    public Actor(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public int getHeight() {\n        return this.height;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Actor noor = new Actor(\"Noor Haddad\", 172);\n        Costume puck = new Costume(\"Puck\", noor);\n        Costume moth = new Costume(\"Moth\", noor);\n        System.out.println(puck);\n        System.out.println(moth.getActor().getName());\n    }\n}\n","solution":"// ==== Costume.java ====\npublic class Costume {\n    private String character;\n    private Actor actor;\n\n    public Costume(String character, Actor actor) {\n        this.character = character;\n        this.actor = actor;\n    }\n\n    public Actor getActor() {\n        return this.actor;\n    }\n\n    @Override\n    public String toString() {\n        return this.character + \", for \" + this.actor.getName() + \" (\" + this.actor.getHeight() + \" cm)\";\n    }\n}\n\n// ==== Actor.java ====\npublic class Actor {\n    private String name;\n    private int height;\n\n    public Actor(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public int getHeight() {\n        return this.height;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Actor noor = new Actor(\"Noor Haddad\", 172);\n        Costume puck = new Costume(\"Puck\", noor);\n        Costume moth = new Costume(\"Moth\", noor);\n        System.out.println(puck);\n        System.out.println(moth.getActor().getName());\n    }\n}\n","hints":["Costume has two instance variables: `character` from its box, and `actor` from the arrow, of type Actor. The constructor stores both.","`getActor` returns the actor. `toString` asks the actor for its name and height: `this.actor.getName()` and `this.actor.getHeight()`."],"tests":[{"name":"Test 1","stdin":"","call":"Actor noor = new Actor(\"Noor Haddad\", 172);\nCostume puck = new Costume(\"Puck\", noor);\nCostume moth = new Costume(\"Moth\", noor);\nSystem.out.println(puck);\nSystem.out.println(moth.getActor().getName());\n","expect":"Puck, for Noor Haddad (172 cm)\nNoor Haddad","hidden":false},{"name":"Test 2","stdin":"","call":"Actor sam = new Actor(\"Sam Okafor\", 185);\nCostume oberon = new Costume(\"Oberon\", sam);\nSystem.out.println(oberon);\nSystem.out.println(oberon.getActor() == sam);\n","expect":"Oberon, for Sam Okafor (185 cm)\ntrue","hidden":true}],"require":[{"pattern":"\\bprivate\\s+Actor\\s+actor\\s*;","message":"The arrow from Costume to Actor, named `actor`, is a field: `private Actor actor;`."},{"pattern":"\\bpublic\\s+Actor\\s+getActor\\s*\\(\\s*\\)","message":"The box's line `+getActor(): Actor` is the header `public Actor getActor()`."}],"forbid":[],"more":[{"task":"The theater's director cuts some scenes, and the play gets shorter. Here are the Play and Performance of the lesson, with one more method in Play:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Play and Performance\">\n<svg class=\"uml-svg\" viewBox=\"0 0 302 312\" width=\"302\" height=\"312\" style=\"width:18.88rem;min-width:17.31rem;aspect-ratio:302/312\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M151 186L151 142\"/><path class=\"u-edge\" fill=\"none\" d=\"M156.5 152L151 142L145.5 152\"/><rect class=\"u-bg\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"24\"/><text x=\"151\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Play</text><text x=\"28.5\" y=\"46\">-title: String</text><text x=\"28.5\" y=\"62\">-minutes: int</text><text x=\"28.5\" y=\"86\">+Play(title: String, minutes: int)</text><text x=\"28.5\" y=\"102\">+getTitle(): String</text><text x=\"28.5\" y=\"118\">+getMinutes(): int</text><text x=\"28.5\" y=\"134\">+cut(minutes: int): void</text><path class=\"u-sep\" fill=\"none\" d=\"M20.5 30h261M20.5 70h261\"/><rect class=\"u-frame\" fill=\"none\" x=\"20.5\" y=\"6\" width=\"261\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"186\" width=\"290\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"186\" width=\"290\" height=\"24\"/><text x=\"151\" y=\"202\" text-anchor=\"middle\" class=\"u-name\">Performance</text><text x=\"14\" y=\"226\">-date: String</text><text x=\"14\" y=\"242\">-seatsSold: int</text><text x=\"14\" y=\"266\">+Performance(play: Play, date: String)</text><text x=\"14\" y=\"282\">+sell(seats: int): void</text><text x=\"14\" y=\"298\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 210h290M6 250h290\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"186\" width=\"290\" height=\"120\"/><text x=\"159\" y=\"155\" class=\"u-mult\">1</text><text x=\"145\" y=\"168\" text-anchor=\"end\" class=\"u-role\">play</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Play</strong>, a class<ul><li>private field <code>title: String</code></li><li>private field <code>minutes: int</code></li><li>public constructor <code>Play(title: String, minutes: int)</code></li><li>public method <code>getTitle(): String</code></li><li>public method <code>getMinutes(): int</code></li><li>public method <code>cut(minutes: int): void</code></li></ul></li><li><strong>Performance</strong>, a class<ul><li>private field <code>date: String</code></li><li>private field <code>seatsSold: int</code></li><li>public constructor <code>Performance(play: Play, date: String)</code></li><li>public method <code>sell(seats: int): void</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Performance has one Play object (play).</li></ul></details></figure>\n\n\nWhat does this program print?\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        Play macbeth = new Play(\"Macbeth\", 150);\n        Performance opening = new Performance(macbeth, \"June 1\");\n        Performance second = new Performance(macbeth, \"June 2\");\n        Performance tour = new Performance(new Play(\"Macbeth\", 150), \"June 3\");\n        opening.sell(80);\n        macbeth.cut(15);\n        second.sell(200);\n        System.out.println(opening);\n        System.out.println(second);\n        System.out.println(tour);\n    }\n}\n// ==== Play.java ====\npublic class Play {\n    private String title;\n    private int minutes;\n\n    public Play(String title, int minutes) {\n        this.title = title;\n        this.minutes = minutes;\n    }\n\n    public String getTitle() {\n        return this.title;\n    }\n\n    public int getMinutes() {\n        return this.minutes;\n    }\n\n    public void cut(int minutes) {\n        this.minutes -= minutes;\n    }\n}\n// ==== Performance.java ====\npublic class Performance {\n    private Play play;\n    private String date;\n    private int seatsSold;\n\n    public Performance(Play play, String date) {\n        this.play = play;\n        this.date = date;\n        this.seatsSold = 0;\n    }\n\n    public void sell(int seats) {\n        this.seatsSold += seats;\n    }\n\n    @Override\n    public String toString() {\n        return this.play.getTitle() + \" (\" + this.play.getMinutes() + \" min), \" + this.date + \": \" + this.seatsSold + \" seats sold\";\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        Play macbeth = new Play(\"Macbeth\", 150);\n        Performance opening = new Performance(macbeth, \"June 1\");\n        Performance second = new Performance(macbeth, \"June 2\");\n        Performance tour = new Performance(new Play(\"Macbeth\", 150), \"June 3\");\n        opening.sell(80);\n        macbeth.cut(15);\n        second.sell(200);\n        System.out.println(opening);\n        System.out.println(second);\n        System.out.println(tour);\n    }\n}\n// ==== Play.java ====\npublic class Play {\n    private String title;\n    private int minutes;\n\n    public Play(String title, int minutes) {\n        this.title = title;\n        this.minutes = minutes;\n    }\n\n    public String getTitle() {\n        return this.title;\n    }\n\n    public int getMinutes() {\n        return this.minutes;\n    }\n\n    public void cut(int minutes) {\n        this.minutes -= minutes;\n    }\n}\n// ==== Performance.java ====\npublic class Performance {\n    private Play play;\n    private String date;\n    private int seatsSold;\n\n    public Performance(Play play, String date) {\n        this.play = play;\n        this.date = date;\n        this.seatsSold = 0;\n    }\n\n    public void sell(int seats) {\n        this.seatsSold += seats;\n    }\n\n    @Override\n    public String toString() {\n        return this.play.getTitle() + \" (\" + this.play.getMinutes() + \" min), \" + this.date + \": \" + this.seatsSold + \" seats sold\";\n    }\n}\n","hints":["`opening` and `second` get the same Play object, `macbeth`: the arrow is a reference, not a copy. `cut` changes that one object, and both performances see it, whenever they print.","`tour` has a Play object of its own, made with `new`, which `cut` never touches."],"tests":[{"name":"Output","stdin":"","expect":"Macbeth (135 min), June 1: 80 seats sold\nMacbeth (135 min), June 2: 200 seats sold\nMacbeth (150 min), June 3: 0 seats sold","hidden":false}],"require":[],"forbid":[],"lines":["Macbeth (135 min), June 1: 80 seats sold","Macbeth (135 min), June 2: 200 seats sold","Macbeth (150 min), June 3: 0 seats sold"]},{"task":"The ward's program has grown: a bed now holds a `Patient` object instead of a name, and a free bed holds none.\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Patient and Bed\">\n<svg class=\"uml-svg\" viewBox=\"0 0 417 328\" width=\"417\" height=\"328\" style=\"width:26.07rem;min-width:23.9rem;aspect-ratio:417/328\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M208.5 186L208.5 142\"/><path class=\"u-edge\" fill=\"none\" d=\"M214 152L208.5 142L203 152\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"405\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"405\" height=\"24\"/><text x=\"208.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Patient</text><text x=\"14\" y=\"46\">-name: String</text><text x=\"14\" y=\"62\">-room: int</text><text x=\"14\" y=\"78\">-temperature: double</text><text x=\"14\" y=\"102\">+Patient(name: String, room: int, temperature: double)</text><text x=\"14\" y=\"118\">+getName(): String</text><text x=\"14\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 30h405M6 86h405\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"405\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"78\" y=\"186\" width=\"261\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"78\" y=\"186\" width=\"261\" height=\"24\"/><text x=\"208.5\" y=\"202\" text-anchor=\"middle\" class=\"u-name\">Bed</text><text x=\"86\" y=\"226\">-number: int</text><text x=\"86\" y=\"250\">+Bed(number: int)</text><text x=\"86\" y=\"266\">+getPatient(): Patient</text><text x=\"86\" y=\"282\">+assign(patient: Patient): boolean</text><text x=\"86\" y=\"298\">+release(): void</text><text x=\"86\" y=\"314\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M78 210h261M78 234h261\"/><rect class=\"u-frame\" fill=\"none\" x=\"78\" y=\"186\" width=\"261\" height=\"136\"/><text x=\"216.5\" y=\"155\" class=\"u-mult\">0..1</text><text x=\"202.5\" y=\"168\" text-anchor=\"end\" class=\"u-role\">patient</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Patient</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>room: int</code></li><li>private field <code>temperature: double</code></li><li>public constructor <code>Patient(name: String, room: int, temperature: double)</code></li><li>public method <code>getName(): String</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Bed</strong>, a class<ul><li>private field <code>number: int</code></li><li>public constructor <code>Bed(number: int)</code></li><li>public method <code>getPatient(): Patient</code></li><li>public method <code>assign(patient: Patient): boolean</code></li><li>public method <code>release(): void</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Bed has at most one Patient object (patient).</li></ul></details></figure>\n\n\n`Patient.java` is ready. Write the class `Bed` in `Bed.java`:\n\n- A new bed is free: its patient is `null`.\n- `assign` puts the patient in the bed and returns `true`, but only if the bed is free. A bed that has a patient keeps them, and `assign` returns `false`.\n- `release` makes the bed free, and `getPatient` returns the bed's patient, or `null` for a free bed.\n- `toString` returns `Bed 3: free`, or `Bed 3: Ines Moreau` with the patient's name.\n\nThe check runs:\n\n```java\nBed bed = new Bed(3);\nPatient ines = new Patient(\"Ines Moreau\", 12, 38.4);\nPatient oskar = new Patient(\"Oskar Lund\", 12, 36.9);\nSystem.out.println(bed);\nSystem.out.println(bed.assign(ines) + \" \" + bed.assign(oskar));\nSystem.out.println(bed);\nbed.release();\nSystem.out.println(bed.getPatient() == null);\n```\n\nand expects:\n\n```\nBed 3: free\ntrue false\nBed 3: Ines Moreau\ntrue\n```\n","kind":"code","seed":"// ==== Bed.java ====\npublic class Bed {\n\n}\n\n// ==== Patient.java ====\npublic class Patient {\n    private String name;\n    private int room;\n    private double temperature;\n\n    public Patient(String name, int room, double temperature) {\n        this.name = name;\n        this.room = room;\n        this.temperature = temperature;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \", room \" + this.room + \", \" + this.temperature + \" degrees\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Bed bed = new Bed(3);\n        Patient ines = new Patient(\"Ines Moreau\", 12, 38.4);\n        Patient oskar = new Patient(\"Oskar Lund\", 12, 36.9);\n        System.out.println(bed);\n        System.out.println(bed.assign(ines) + \" \" + bed.assign(oskar));\n        System.out.println(bed);\n        bed.release();\n        System.out.println(bed.getPatient() == null);\n    }\n}\n","solution":"// ==== Bed.java ====\npublic class Bed {\n    private int number;\n    private Patient patient;\n\n    public Bed(int number) {\n        this.number = number;\n        this.patient = null;\n    }\n\n    public Patient getPatient() {\n        return this.patient;\n    }\n\n    public boolean assign(Patient patient) {\n        if (this.patient != null) {\n            return false;\n        }\n        this.patient = patient;\n        return true;\n    }\n\n    public void release() {\n        this.patient = null;\n    }\n\n    @Override\n    public String toString() {\n        if (this.patient == null) {\n            return \"Bed \" + this.number + \": free\";\n        }\n        return \"Bed \" + this.number + \": \" + this.patient.getName();\n    }\n}\n\n// ==== Patient.java ====\npublic class Patient {\n    private String name;\n    private int room;\n    private double temperature;\n\n    public Patient(String name, int room, double temperature) {\n        this.name = name;\n        this.room = room;\n        this.temperature = temperature;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \", room \" + this.room + \", \" + this.temperature + \" degrees\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Bed bed = new Bed(3);\n        Patient ines = new Patient(\"Ines Moreau\", 12, 38.4);\n        Patient oskar = new Patient(\"Oskar Lund\", 12, 36.9);\n        System.out.println(bed);\n        System.out.println(bed.assign(ines) + \" \" + bed.assign(oskar));\n        System.out.println(bed);\n        bed.release();\n        System.out.println(bed.getPatient() == null);\n    }\n}\n","hints":["The arrow with `0..1` is a field that may be `null`: `private Patient patient;`, set to `null` in the constructor.","`assign` checks `this.patient != null` first and returns `false` if the bed is taken. `toString` must check for `null` too, before it calls `this.patient.getName()`."],"tests":[{"name":"Test 1","stdin":"","call":"Bed bed = new Bed(3);\nPatient ines = new Patient(\"Ines Moreau\", 12, 38.4);\nPatient oskar = new Patient(\"Oskar Lund\", 12, 36.9);\nSystem.out.println(bed);\nSystem.out.println(bed.assign(ines) + \" \" + bed.assign(oskar));\nSystem.out.println(bed);\nbed.release();\nSystem.out.println(bed.getPatient() == null);\n","expect":"Bed 3: free\ntrue false\nBed 3: Ines Moreau\ntrue","hidden":false},{"name":"Test 2","stdin":"","call":"Bed a = new Bed(1);\nBed b = new Bed(2);\nPatient mei = new Patient(\"Mei Tan\", 4, 37.2);\nPatient leo = new Patient(\"Leo Park\", 5, 36.5);\nSystem.out.println(b.assign(leo));\na.release();\nSystem.out.println(a + \" / \" + b);\nSystem.out.println(a.assign(mei) + \" \" + a.getPatient().getName() + \" \" + b.getPatient().getName());\n","expect":"true\nBed 1: free / Bed 2: Leo Park\ntrue Mei Tan Leo Park","hidden":true}],"require":[{"pattern":"\\bprivate\\s+Patient\\s+patient\\s*;","message":"The arrow from Bed to Patient, named `patient`, is a field: `private Patient patient;`."}],"forbid":[]}]}