{"id":"class-diagrams-many","slug":"many","title":"Many objects, and arrows both ways","text":"### A star for a list\n\nAt a music festival, each stage has a lineup of acts. `Stage` keeps them in a list:\n\n```java\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts;\n\n    // the constructor and methods\n}\n```\n\nThe list holds Act objects, so it's drawn as an arrow to Act, like a field of type Act would be. A star at the arrowhead says that a Stage has any number of them:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Act and Stage\">\n<svg class=\"uml-svg\" viewBox=\"0 0 259 296\" width=\"259\" height=\"296\" style=\"width:16.19rem;min-width:14.84rem;aspect-ratio:259/296\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M129.5 170L129.5 126\"/><path class=\"u-edge\" fill=\"none\" d=\"M135 136L129.5 126L124 136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"247\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"247\" height=\"24\"/><text x=\"129.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Act</text><text x=\"14\" y=\"46\">-name: String</text><text x=\"14\" y=\"62\">-minutes: int</text><text x=\"14\" y=\"86\">+Act(name: String, minutes: int)</text><text x=\"14\" y=\"102\">+getMinutes(): int</text><text x=\"14\" y=\"118\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 30h247M6 70h247\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"247\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"27.5\" y=\"170\" width=\"204\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"27.5\" y=\"170\" width=\"204\" height=\"24\"/><text x=\"129.5\" y=\"186\" text-anchor=\"middle\" class=\"u-name\">Stage</text><text x=\"35.5\" y=\"210\">-name: String</text><text x=\"35.5\" y=\"234\">+Stage(name: String)</text><text x=\"35.5\" y=\"250\">+add(act: Act): void</text><text x=\"35.5\" y=\"266\">+totalMinutes(): int</text><text x=\"35.5\" y=\"282\">+getActs(): ArrayList&lt;Act&gt;</text><path class=\"u-sep\" fill=\"none\" d=\"M27.5 194h204M27.5 218h204\"/><rect class=\"u-frame\" fill=\"none\" x=\"27.5\" y=\"170\" width=\"204\" height=\"120\"/><text x=\"137.5\" y=\"139\" class=\"u-mult\">*</text><text x=\"123.5\" y=\"152\" text-anchor=\"end\" class=\"u-role\">acts</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Act</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>minutes: int</code></li><li>public constructor <code>Act(name: String, minutes: int)</code></li><li>public method <code>getMinutes(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Stage</strong>, a class<ul><li>private field <code>name: String</code></li><li>public constructor <code>Stage(name: String)</code></li><li>public method <code>add(act: Act): void</code></li><li>public method <code>totalMinutes(): int</code></li><li>public method <code>getActs(): ArrayList&lt;Act&gt;</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Stage has any number of Act objects (acts).</li></ul></details></figure>\n\n\n- `*` means any number, zero included: a new stage has an empty lineup.\n- The list isn't in Stage's box, just as a single Act field wouldn't be. The arrow and its star stand for `private ArrayList<Act> acts;`, which the constructor creates.\n- The methods that work with the list are in the box as usual. A list type is written as in Java: `getActs(): ArrayList<Act>`.\n- Other multiplicities you may meet: `1..*` for one or more, and a range such as `2..4`.\n- A list of Strings or of numbers stays in the box as a field, such as `-tags: ArrayList<String>`. Arrows are for the diagram's own classes.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Stage tent = new Stage(\"Blue Tent\");\n        tent.add(new Act(\"The Low Tides\", 45));\n        tent.add(new Act(\"Mira Solen\", 30));\n        System.out.println(tent.getActs());\n        System.out.println(tent.totalMinutes());\n    }\n}\n// ==== Act.java ====\npublic class Act {\n    private String name;\n    private int minutes;\n\n    public Act(String name, int minutes) {\n        this.name = name;\n        this.minutes = minutes;\n    }\n\n    public int getMinutes() {\n        return this.minutes;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \" (\" + this.minutes + \" min)\";\n    }\n}\n// ==== Stage.java ====\nimport java.util.ArrayList;\n\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts;\n\n    public Stage(String name) {\n        this.name = name;\n        this.acts = new ArrayList<>();\n    }\n\n    public void add(Act act) {\n        this.acts.add(act);\n    }\n\n    public int totalMinutes() {\n        int total = 0;\n        for (Act act : this.acts) {\n            total += act.getMinutes();\n        }\n        return total;\n    }\n\n    public ArrayList<Act> getActs() {\n        return this.acts;\n    }\n}\n```\n\n```output\n[The Low Tides (45 min), Mira Solen (30 min)]\n75\n```\n\nYou'll also see the list created right where the field is declared, instead of in the constructor:\n\n```java\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts = new ArrayList<>();\n\n    public Stage(String name) {\n        this.name = name;\n    }\n\n    // the methods as before\n}\n```\n\nIt does the same thing: each new Stage gets an empty list of its own, and the diagram is the same. Any instance variable can get its first value this way, for example an array: `private int[] scores = new int[5];`.\n\nA multiplicity can also sit at the start of an arrow. It then says how many objects of the first class can share one object at the head. Step 4's performances, drawn with both numbers:\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 287 68\" width=\"287\" height=\"68\" style=\"width:17.94rem;min-width:16.45rem;aspect-ratio:287/68\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M171 34L123 34\"/><path class=\"u-edge\" fill=\"none\" d=\"M133 28.5L123 34L133 39.5\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"56\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"24\"/><text x=\"64.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Play</text><text x=\"14\" y=\"46\">-title: String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 30h117M6 54h117\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"56\"/><rect class=\"u-bg\" fill=\"none\" x=\"171\" y=\"6\" width=\"110\" height=\"56\"/><rect class=\"u-head\" fill=\"none\" x=\"171\" y=\"6\" width=\"110\" height=\"24\"/><text x=\"226\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Performance</text><text x=\"179\" y=\"46\">-date: String</text><path class=\"u-sep\" fill=\"none\" d=\"M171 30h110M171 54h110\"/><rect class=\"u-frame\" fill=\"none\" x=\"171\" y=\"6\" width=\"110\" height=\"56\"/><text x=\"165\" y=\"27\" text-anchor=\"end\" class=\"u-mult\">*</text><text x=\"129\" y=\"27\" class=\"u-mult\">1</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></ul></li><li><strong>Performance</strong>, a class<ul><li>private field <code>date: String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Performance has one Play object; each Play belongs to any number of Performance objects.</li></ul></details></figure>\n\n\nEach Performance has one Play, and a Play can have any number of performances, even though a Play doesn't know them: the arrow still points one way.\n\n### Both ways\n\nThe acts above don't know which stage they're on. If the program needs that, say to print each act with its stage, Act gets a field of type Stage, and then the two classes know each other. The diagram draws that as a line **without an arrowhead**, with a multiplicity at each end:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Stage and Act\">\n<svg class=\"uml-svg\" viewBox=\"0 0 259 280\" width=\"259\" height=\"280\" style=\"width:16.19rem;min-width:14.84rem;aspect-ratio:259/280\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M129.5 110L129.5 154\"/><rect class=\"u-bg\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"24\"/><text x=\"129.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Stage</text><text x=\"57.5\" y=\"46\">-name: String</text><text x=\"57.5\" y=\"70\">+Stage(name: String)</text><text x=\"57.5\" y=\"86\">+add(act: Act): void</text><text x=\"57.5\" y=\"102\">+getName(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M49.5 30h160M49.5 54h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"104\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"154\" width=\"247\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"154\" width=\"247\" height=\"24\"/><text x=\"129.5\" y=\"170\" text-anchor=\"middle\" class=\"u-name\">Act</text><text x=\"14\" y=\"194\">-name: String</text><text x=\"14\" y=\"210\">-minutes: int</text><text x=\"14\" y=\"234\">+Act(name: String, minutes: int)</text><text x=\"14\" y=\"250\">+setStage(stage: Stage): void</text><text x=\"14\" y=\"266\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 178h247M6 218h247\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"154\" width=\"247\" height=\"120\"/><text x=\"137.5\" y=\"123\" class=\"u-mult\">0..1</text><text x=\"137.5\" y=\"149\" class=\"u-mult\">*</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Stage</strong>, a class<ul><li>private field <code>name: String</code></li><li>public constructor <code>Stage(name: String)</code></li><li>public method <code>add(act: Act): void</code></li><li>public method <code>getName(): String</code></li></ul></li><li><strong>Act</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>minutes: int</code></li><li>public constructor <code>Act(name: String, minutes: int)</code></li><li>public method <code>setStage(stage: Stage): void</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Stage and Act know each other: each Stage has any number of Act objects, and each Act has at most one Stage object.</li></ul></details></figure>\n\n\nRead each end's number as \"how many of these\": each Stage has any number of Acts (the `*` at Act's end), and each Act is on at most one Stage (the `0..1` at Stage's end: none until it's added).\n\nIn code, a connection both ways is two fields: Stage's list of acts, and Act's `stage`. They have to agree: when an act goes on a stage, the act must learn its stage too. The usual way is to let one method do both. Inside a method, `this` is the object the method was called on, so `act.setStage(this)` gives the act this very stage:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Stage tent = new Stage(\"Blue Tent\");\n        Act tides = new Act(\"The Low Tides\", 45);\n        System.out.println(tides);\n        tent.add(tides);\n        System.out.println(tides);\n    }\n}\n// ==== Stage.java ====\nimport java.util.ArrayList;\n\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts;\n\n    public Stage(String name) {\n        this.name = name;\n        this.acts = new ArrayList<>();\n    }\n\n    public void add(Act act) {\n        this.acts.add(act);\n        act.setStage(this);\n    }\n\n    public String getName() {\n        return this.name;\n    }\n}\n// ==== Act.java ====\npublic class Act {\n    private String name;\n    private int minutes;\n    private Stage stage;\n\n    public Act(String name, int minutes) {\n        this.name = name;\n        this.minutes = minutes;\n        this.stage = null;\n    }\n\n    public void setStage(Stage stage) {\n        this.stage = stage;\n    }\n\n    @Override\n    public String toString() {\n        if (this.stage == null) {\n            return this.name + \", \" + this.minutes + \" min, no stage yet\";\n        }\n        return this.name + \", \" + this.minutes + \" min, \" + this.stage.getName();\n    }\n}\n```\n\n```output\nThe Low Tides, 45 min, no stage yet\nThe Low Tides, 45 min, Blue Tent\n```\n\nA connection both ways costs more than an arrow: two fields to keep in step, and a change that updates one side and forgets the other leaves them disagreeing. So draw, and write, a one-way arrow unless the program really needs both directions.\n","task":"The ward keeps its patients in a list. `Patient.java` is ready. Write the class `Ward` in `Ward.java`, as the diagram shows:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Patient and Ward\">\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 202L208.5 158\"/><path class=\"u-edge\" fill=\"none\" d=\"M214 168L208.5 158L203 168\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"405\" height=\"152\"/><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\">+getTemperature(): double</text><text x=\"14\" y=\"150\">+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=\"152\"/><rect class=\"u-bg\" fill=\"none\" x=\"85\" y=\"202\" width=\"247\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"85\" y=\"202\" width=\"247\" height=\"24\"/><text x=\"208.5\" y=\"218\" text-anchor=\"middle\" class=\"u-name\">Ward</text><text x=\"93\" y=\"242\">-name: String</text><text x=\"93\" y=\"266\">+Ward(name: String)</text><text x=\"93\" y=\"282\">+admit(patient: Patient): void</text><text x=\"93\" y=\"298\">+withFever(): ArrayList&lt;Patient&gt;</text><text x=\"93\" y=\"314\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M85 226h247M85 250h247\"/><rect class=\"u-frame\" fill=\"none\" x=\"85\" y=\"202\" width=\"247\" height=\"120\"/><text x=\"216.5\" y=\"171\" class=\"u-mult\">*</text><text x=\"202.5\" y=\"184\" text-anchor=\"end\" class=\"u-role\">patients</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>getTemperature(): double</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Ward</strong>, a class<ul><li>private field <code>name: String</code></li><li>public constructor <code>Ward(name: String)</code></li><li>public method <code>admit(patient: Patient): void</code></li><li>public method <code>withFever(): ArrayList&lt;Patient&gt;</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Ward has any number of Patient objects (patients).</li></ul></details></figure>\n\n\n- A new ward has no patients. `admit` adds a patient to the ward.\n- `withFever` returns a new list of the patients whose temperature is 38.0 or more, in the order they were admitted.\n- `toString` returns `Ward 5B: 3 patients`, with the ward's own name and number of patients.\n\nThe check runs:\n\n```java\nWard ward = new Ward(\"Ward 5B\");\nward.admit(new Patient(\"Ines Moreau\", 12, 38.4));\nward.admit(new Patient(\"Oskar Lund\", 12, 36.9));\nward.admit(new Patient(\"Mei Tan\", 14, 38.0));\nSystem.out.println(ward);\nfor (Patient patient : ward.withFever()) {\n    System.out.println(patient.getName());\n}\n```\n\nand expects:\n\n```\nWard 5B: 3 patients\nInes Moreau\nMei Tan\n```\n","kind":"code","seed":"// ==== Ward.java ====\npublic class Ward {\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    public double getTemperature() {\n        return this.temperature;\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        Ward ward = new Ward(\"Ward 5B\");\n        ward.admit(new Patient(\"Ines Moreau\", 12, 38.4));\n        ward.admit(new Patient(\"Oskar Lund\", 12, 36.9));\n        ward.admit(new Patient(\"Mei Tan\", 14, 38.0));\n        System.out.println(ward);\n        for (Patient patient : ward.withFever()) {\n            System.out.println(patient.getName());\n        }\n    }\n}\n","solution":"// ==== Ward.java ====\nimport java.util.ArrayList;\n\npublic class Ward {\n    private String name;\n    private ArrayList<Patient> patients;\n\n    public Ward(String name) {\n        this.name = name;\n        this.patients = new ArrayList<>();\n    }\n\n    public void admit(Patient patient) {\n        this.patients.add(patient);\n    }\n\n    public ArrayList<Patient> withFever() {\n        ArrayList<Patient> fever = new ArrayList<>();\n        for (Patient patient : this.patients) {\n            if (patient.getTemperature() >= 38.0) {\n                fever.add(patient);\n            }\n        }\n        return fever;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \": \" + this.patients.size() + \" patients\";\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    public double getTemperature() {\n        return this.temperature;\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        Ward ward = new Ward(\"Ward 5B\");\n        ward.admit(new Patient(\"Ines Moreau\", 12, 38.4));\n        ward.admit(new Patient(\"Oskar Lund\", 12, 36.9));\n        ward.admit(new Patient(\"Mei Tan\", 14, 38.0));\n        System.out.println(ward);\n        for (Patient patient : ward.withFever()) {\n            System.out.println(patient.getName());\n        }\n    }\n}\n","hints":["Ward's instance variables are its `name` and, for the arrow with the star, `private ArrayList<Patient> patients;`, created in the constructor with `new ArrayList<>()`. `Ward.java` needs `import java.util.ArrayList;` at the top.","`withFever` makes a new list, adds each patient whose `getTemperature()` is at least 38.0, and returns it. `toString` uses the size of the ward's list."],"tests":[{"name":"Test 1","stdin":"","call":"Ward ward = new Ward(\"Ward 5B\");\nward.admit(new Patient(\"Ines Moreau\", 12, 38.4));\nward.admit(new Patient(\"Oskar Lund\", 12, 36.9));\nward.admit(new Patient(\"Mei Tan\", 14, 38.0));\nSystem.out.println(ward);\nfor (Patient patient : ward.withFever()) {\n    System.out.println(patient.getName());\n}\n","expect":"Ward 5B: 3 patients\nInes Moreau\nMei Tan","hidden":false},{"name":"Test 2","stdin":"","call":"Ward ward = new Ward(\"Ward 2A\");\nSystem.out.println(ward + \" \" + ward.withFever());\nward.admit(new Patient(\"Leo Park\", 3, 39.1));\nward.admit(new Patient(\"Ana Ruiz\", 3, 37.9));\nArrayList<Patient> fever = ward.withFever();\nSystem.out.println(fever.size() + \" \" + fever.get(0).getName());\nfever.clear();\nSystem.out.println(ward);\n","expect":"Ward 2A: 0 patients []\n1 Leo Park\nWard 2A: 2 patients","hidden":true}],"require":[{"pattern":"\\bprivate\\s+ArrayList<Patient>\\s+\\w+\\s*[;=]","message":"The arrow with the star is a list: `private ArrayList<Patient> patients;`."},{"pattern":"\\bpublic\\s+ArrayList<Patient>\\s+withFever\\s*\\(\\s*\\)","message":"The box's line `+withFever(): ArrayList<Patient>` is the header `public ArrayList<Patient> withFever()`."}],"forbid":[],"more":[{"task":"The festival moves The Low Tides from the Blue Tent to the Main Field. Stage and Act are the classes of the lesson's connection both ways, and Stage has one more method, `count()`, which returns the number of acts in its list:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Stage and Act\">\n<svg class=\"uml-svg\" viewBox=\"0 0 259 296\" width=\"259\" height=\"296\" style=\"width:16.19rem;min-width:14.84rem;aspect-ratio:259/296\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M129.5 126L129.5 170\"/><rect class=\"u-bg\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"24\"/><text x=\"129.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Stage</text><text x=\"57.5\" y=\"46\">-name: String</text><text x=\"57.5\" y=\"70\">+Stage(name: String)</text><text x=\"57.5\" y=\"86\">+add(act: Act): void</text><text x=\"57.5\" y=\"102\">+getName(): String</text><text x=\"57.5\" y=\"118\">+count(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M49.5 30h160M49.5 54h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"49.5\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"170\" width=\"247\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"170\" width=\"247\" height=\"24\"/><text x=\"129.5\" y=\"186\" text-anchor=\"middle\" class=\"u-name\">Act</text><text x=\"14\" y=\"210\">-name: String</text><text x=\"14\" y=\"226\">-minutes: int</text><text x=\"14\" y=\"250\">+Act(name: String, minutes: int)</text><text x=\"14\" y=\"266\">+setStage(stage: Stage): void</text><text x=\"14\" y=\"282\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 194h247M6 234h247\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"170\" width=\"247\" height=\"120\"/><text x=\"137.5\" y=\"139\" class=\"u-mult\">0..1</text><text x=\"137.5\" y=\"165\" class=\"u-mult\">*</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Stage</strong>, a class<ul><li>private field <code>name: String</code></li><li>public constructor <code>Stage(name: String)</code></li><li>public method <code>add(act: Act): void</code></li><li>public method <code>getName(): String</code></li><li>public method <code>count(): int</code></li></ul></li><li><strong>Act</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>minutes: int</code></li><li>public constructor <code>Act(name: String, minutes: int)</code></li><li>public method <code>setStage(stage: Stage): void</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Stage and Act know each other: each Stage has any number of Act objects, and each Act has at most one Stage object.</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        Stage tent = new Stage(\"Blue Tent\");\n        Stage field = new Stage(\"Main Field\");\n        Act tides = new Act(\"The Low Tides\", 45);\n        tent.add(tides);\n        field.add(tides);\n        System.out.println(tent.count() + \" \" + field.count());\n        System.out.println(tides);\n    }\n}\n// ==== Stage.java ====\nimport java.util.ArrayList;\n\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts;\n\n    public Stage(String name) {\n        this.name = name;\n        this.acts = new ArrayList<>();\n    }\n\n    public void add(Act act) {\n        this.acts.add(act);\n        act.setStage(this);\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public int count() {\n        return this.acts.size();\n    }\n}\n// ==== Act.java ====\npublic class Act {\n    private String name;\n    private int minutes;\n    private Stage stage;\n\n    public Act(String name, int minutes) {\n        this.name = name;\n        this.minutes = minutes;\n        this.stage = null;\n    }\n\n    public void setStage(Stage stage) {\n        this.stage = stage;\n    }\n\n    @Override\n    public String toString() {\n        if (this.stage == null) {\n            return this.name + \", \" + this.minutes + \" min, no stage yet\";\n        }\n        return this.name + \", \" + this.minutes + \" min, \" + this.stage.getName();\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        Stage tent = new Stage(\"Blue Tent\");\n        Stage field = new Stage(\"Main Field\");\n        Act tides = new Act(\"The Low Tides\", 45);\n        tent.add(tides);\n        field.add(tides);\n        System.out.println(tent.count() + \" \" + field.count());\n        System.out.println(tides);\n    }\n}\n// ==== Stage.java ====\nimport java.util.ArrayList;\n\npublic class Stage {\n    private String name;\n    private ArrayList<Act> acts;\n\n    public Stage(String name) {\n        this.name = name;\n        this.acts = new ArrayList<>();\n    }\n\n    public void add(Act act) {\n        this.acts.add(act);\n        act.setStage(this);\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public int count() {\n        return this.acts.size();\n    }\n}\n// ==== Act.java ====\npublic class Act {\n    private String name;\n    private int minutes;\n    private Stage stage;\n\n    public Act(String name, int minutes) {\n        this.name = name;\n        this.minutes = minutes;\n        this.stage = null;\n    }\n\n    public void setStage(Stage stage) {\n        this.stage = stage;\n    }\n\n    @Override\n    public String toString() {\n        if (this.stage == null) {\n            return this.name + \", \" + this.minutes + \" min, no stage yet\";\n        }\n        return this.name + \", \" + this.minutes + \" min, \" + this.stage.getName();\n    }\n}\n","hints":["`add` does two things: it puts the act in the stage's list, and it tells the act its stage with `setStage(this)`.","The second `add` sets the act's stage to the Main Field, but nothing takes the act out of the Blue Tent's list. Both lists have it, while the act knows only its last stage: the two sides disagree."],"tests":[{"name":"Output","stdin":"","expect":"1 1\nThe Low Tides, 45 min, Main Field","hidden":false}],"require":[],"forbid":[],"lines":["1 1","The Low Tides, 45 min, Main Field"]},{"task":"A garden center sells seedlings in trays, and a tray has room for at most 6 of them. Write both classes of this diagram, `Seedling` in `Seedling.java` and `Tray` in `Tray.java`:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Seedling and Tray\">\n<svg class=\"uml-svg\" viewBox=\"0 0 280 280\" width=\"280\" height=\"280\" style=\"width:17.5rem;min-width:16.05rem;aspect-ratio:280/280\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M140 170L140 126\"/><path class=\"u-edge\" fill=\"none\" d=\"M145.5 136L140 126L134.5 136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"268\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"268\" height=\"24\"/><text x=\"140\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Seedling</text><text x=\"14\" y=\"46\">-name: String</text><text x=\"14\" y=\"62\">-price: int</text><text x=\"14\" y=\"86\">+Seedling(name: String, price: int)</text><text x=\"14\" y=\"102\">+getPrice(): int</text><text x=\"14\" y=\"118\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 30h268M6 70h268\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"268\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"13\" y=\"170\" width=\"254\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"13\" y=\"170\" width=\"254\" height=\"24\"/><text x=\"140\" y=\"186\" text-anchor=\"middle\" class=\"u-name\">Tray</text><text x=\"21\" y=\"218\">+Tray()</text><text x=\"21\" y=\"234\">+add(seedling: Seedling): boolean</text><text x=\"21\" y=\"250\">+total(): int</text><text x=\"21\" y=\"266\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M13 194h254M13 202h254\"/><rect class=\"u-frame\" fill=\"none\" x=\"13\" y=\"170\" width=\"254\" height=\"104\"/><text x=\"148\" y=\"139\" class=\"u-mult\">0..6</text><text x=\"134\" y=\"152\" text-anchor=\"end\" class=\"u-role\">seedlings</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Seedling</strong>, a class<ul><li>private field <code>name: String</code></li><li>private field <code>price: int</code></li><li>public constructor <code>Seedling(name: String, price: int)</code></li><li>public method <code>getPrice(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Tray</strong>, a class<ul><li>public constructor <code>Tray()</code></li><li>public method <code>add(seedling: Seedling): boolean</code></li><li>public method <code>total(): int</code></li><li>public method <code>toString(): String</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Tray has 0 to 6 Seedling objects (seedlings).</li></ul></details></figure>\n\n\n- A seedling's `toString` returns `Basil (3 dollars)`, with its own name and price.\n- A new tray is empty. `add` puts the seedling in the tray and returns `true`, unless the tray already has 6 seedlings: then it returns `false` and adds nothing.\n- `total` returns the prices of the tray's seedlings added up.\n- A tray's `toString` returns `3 seedlings, 11 dollars`.\n\nThe check runs:\n\n```java\nTray tray = new Tray();\ntray.add(new Seedling(\"Basil\", 3));\ntray.add(new Seedling(\"Tomato\", 4));\ntray.add(new Seedling(\"Mint\", 4));\nSystem.out.println(tray);\nSystem.out.println(new Seedling(\"Thyme\", 2));\n```\n\nand expects:\n\n```\n3 seedlings, 11 dollars\nThyme (2 dollars)\n```\n","kind":"code","seed":"// ==== Seedling.java ====\npublic class Seedling {\n\n}\n\n// ==== Tray.java ====\npublic class Tray {\n\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Tray tray = new Tray();\n        tray.add(new Seedling(\"Basil\", 3));\n        tray.add(new Seedling(\"Tomato\", 4));\n        tray.add(new Seedling(\"Mint\", 4));\n        System.out.println(tray);\n        System.out.println(new Seedling(\"Thyme\", 2));\n    }\n}\n","solution":"// ==== Seedling.java ====\npublic class Seedling {\n    private String name;\n    private int price;\n\n    public Seedling(String name, int price) {\n        this.name = name;\n        this.price = price;\n    }\n\n    public int getPrice() {\n        return this.price;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \" (\" + this.price + \" dollars)\";\n    }\n}\n\n// ==== Tray.java ====\nimport java.util.ArrayList;\n\npublic class Tray {\n    private ArrayList<Seedling> seedlings;\n\n    public Tray() {\n        this.seedlings = new ArrayList<>();\n    }\n\n    public boolean add(Seedling seedling) {\n        if (this.seedlings.size() >= 6) {\n            return false;\n        }\n        this.seedlings.add(seedling);\n        return true;\n    }\n\n    public int total() {\n        int sum = 0;\n        for (Seedling seedling : this.seedlings) {\n            sum += seedling.getPrice();\n        }\n        return sum;\n    }\n\n    @Override\n    public String toString() {\n        return this.seedlings.size() + \" seedlings, \" + this.total() + \" dollars\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Tray tray = new Tray();\n        tray.add(new Seedling(\"Basil\", 3));\n        tray.add(new Seedling(\"Tomato\", 4));\n        tray.add(new Seedling(\"Mint\", 4));\n        System.out.println(tray);\n        System.out.println(new Seedling(\"Thyme\", 2));\n    }\n}\n","hints":["Seedling is a class like the lesson's first Act, the one with `getMinutes()`: two instance variables, a constructor, a getter and `toString`. Tray's box has no fields: its only instance variable is the list that the arrow stands for, `private ArrayList<Seedling> seedlings;`.","`0..6` means at most 6, so `add` returns `false` without adding when the list's size is already 6. `total` adds up `getPrice()` over the list, and `toString` uses the list's size and `total()`."],"tests":[{"name":"Test 1","stdin":"","call":"Tray tray = new Tray();\ntray.add(new Seedling(\"Basil\", 3));\ntray.add(new Seedling(\"Tomato\", 4));\ntray.add(new Seedling(\"Mint\", 4));\nSystem.out.println(tray);\nSystem.out.println(new Seedling(\"Thyme\", 2));\n","expect":"3 seedlings, 11 dollars\nThyme (2 dollars)","hidden":false},{"name":"Test 2","stdin":"","call":"Tray tray = new Tray();\nSystem.out.println(tray);\nint added = 0;\nfor (int price = 1; price <= 8; price++) {\n    if (tray.add(new Seedling(\"Chive\", price))) {\n        added++;\n    }\n}\nSystem.out.println(added + \" \" + tray.total());\nSystem.out.println(tray);\n","expect":"0 seedlings, 0 dollars\n6 21\n6 seedlings, 21 dollars","hidden":true}],"require":[{"pattern":"\\bprivate\\s+ArrayList<Seedling>\\s+\\w+\\s*[;=]","message":"The arrow from Tray to Seedling stands for a list: `private ArrayList<Seedling> seedlings;`."}],"forbid":[]}]}