{"id":"class-diagrams-interfaces","slug":"interfaces","title":"Interfaces, and a whole program","text":"### An interface's box\n\nAn interface (module 35) gets a box like a class, with «interface» above its name. It has no instance variables and no constructor, so below its name, its box lists only the methods.\n\nThe recycling center's weighing station weighs anything that can say what it weighs, so it has an interface `Weighable`. A crate of bottles and a bale of paper both implement it:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Weighable, Crate and Bale\">\n<svg class=\"uml-svg\" viewBox=\"0 0 341 210\" width=\"341\" height=\"210\" style=\"width:21.32rem;min-width:19.54rem;aspect-ratio:341/210\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge u-dash\" fill=\"none\" d=\"M175.8 81L175.8 92L86 92L86 116\"/><path class=\"u-edge u-dash\" fill=\"none\" d=\"M175.8 81L175.8 92L265.5 92L265.5 116\"/><path class=\"u-tri\" d=\"M175.8 70L168.8 81L182.8 81Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"117.3\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-head\" fill=\"none\" x=\"117.3\" y=\"6\" width=\"117\" height=\"40\"/><text x=\"175.8\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«interface»</text><text x=\"175.8\" y=\"38\" text-anchor=\"middle\" class=\"u-name\">Weighable</text><text x=\"125.3\" y=\"62\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M117.3 46h117\"/><rect class=\"u-frame\" fill=\"none\" x=\"117.3\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"116\" width=\"160\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"116\" width=\"160\" height=\"24\"/><text x=\"86\" y=\"132\" text-anchor=\"middle\" class=\"u-name\">Crate</text><text x=\"14\" y=\"156\">-bottles: int</text><text x=\"14\" y=\"180\">+Crate(bottles: int)</text><text x=\"14\" y=\"196\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 140h160M6 164h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"116\" width=\"160\" height=\"88\"/><rect class=\"u-bg\" fill=\"none\" x=\"196\" y=\"116\" width=\"139\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"196\" y=\"116\" width=\"139\" height=\"24\"/><text x=\"265.5\" y=\"132\" text-anchor=\"middle\" class=\"u-name\">Bale</text><text x=\"204\" y=\"156\">-kilos: int</text><text x=\"204\" y=\"180\">+Bale(kilos: int)</text><text x=\"204\" y=\"196\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M196 140h139M196 164h139\"/><rect class=\"u-frame\" fill=\"none\" x=\"196\" y=\"116\" width=\"139\" height=\"88\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Weighable</strong>, an interface<ul><li>public method <code>weight(): int</code></li></ul></li><li><strong>Crate</strong>, a class that implements Weighable<ul><li>private field <code>bottles: int</code></li><li>public constructor <code>Crate(bottles: int)</code></li><li>public method <code>weight(): int</code></li></ul></li><li><strong>Bale</strong>, a class that implements Weighable<ul><li>private field <code>kilos: int</code></li><li>public constructor <code>Bale(kilos: int)</code></li><li>public method <code>weight(): int</code></li></ul></li></ul></details></figure>\n\n\n- The interface's methods are public, `+`. The interface only lists them, so a line for each is all there is to show.\n- A class that implements the interface is joined to it by a **dashed** line with a hollow triangle at the interface: the triangle of `extends`, dashed for `implements`.\n- Each class's box lists the interface's methods again, since each class writes them.\n- A class that implements several interfaces has a dashed line to each of them.\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Weighable> station = new ArrayList<>();\n        station.add(new Crate(24));\n        station.add(new Bale(250));\n        int total = 0;\n        for (Weighable item : station) {\n            total += item.weight();\n        }\n        System.out.println(total + \" kg\");\n    }\n}\n// ==== Weighable.java ====\npublic interface Weighable {\n    int weight();\n}\n// ==== Crate.java ====\npublic class Crate implements Weighable {\n    private int bottles;\n\n    public Crate(int bottles) {\n        this.bottles = bottles;\n    }\n\n    @Override\n    public int weight() {\n        return 2 + this.bottles / 2;\n    }\n}\n// ==== Bale.java ====\npublic class Bale implements Weighable {\n    private int kilos;\n\n    public Bale(int kilos) {\n        this.kilos = kilos;\n    }\n\n    @Override\n    public int weight() {\n        return this.kilos;\n    }\n}\n```\n\n```output\n264 kg\n```\n\n### A whole program\n\nA diagram of a whole program uses everything at once. Here's the weighing station with a `Scale` that remembers what it has weighed, and old tires, from step 6, that can be weighed as well:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Weighable, DropOff, Tires, Crate and Scale\">\n<svg class=\"uml-svg\" viewBox=\"0 0 435 426\" width=\"435\" height=\"426\" style=\"width:27.19rem;min-width:24.93rem;aspect-ratio:435/426\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M86 137L86 148L84.3 148L84.3 184\"/><path class=\"u-edge u-dash\" fill=\"none\" d=\"M235 81L235 148L161.7 148L161.7 184\"/><path class=\"u-edge u-dash\" fill=\"none\" d=\"M235 81L235 148L349 148L349 184\"/><path class=\"u-edge\" fill=\"none\" d=\"M254.5 332L254.5 160L274 160L274 70\"/><path class=\"u-tri\" d=\"M86 126L79 137L93 137Z\"/><path class=\"u-tri\" d=\"M235 70L228 81L242 81Z\"/><path class=\"u-edge\" fill=\"none\" d=\"M279.5 80L274 70L268.5 80\"/><rect class=\"u-bg\" fill=\"none\" x=\"196\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-head\" fill=\"none\" x=\"196\" y=\"6\" width=\"117\" height=\"40\"/><text x=\"254.5\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«interface»</text><text x=\"254.5\" y=\"38\" text-anchor=\"middle\" class=\"u-name\">Weighable</text><text x=\"204\" y=\"62\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M196 46h117\"/><rect class=\"u-frame\" fill=\"none\" x=\"196\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"160\" height=\"40\"/><text x=\"86\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"86\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">DropOff</text><text x=\"14\" y=\"62\">-kilos: int</text><text x=\"14\" y=\"86\">+DropOff(kilos: int)</text><text x=\"14\" y=\"102\" class=\"u-it\">+fee(): int</text><text x=\"14\" y=\"118\">...</text><path class=\"u-sep\" fill=\"none\" d=\"M6 46h160M6 70h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"7\" y=\"184\" width=\"232\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"7\" y=\"184\" width=\"232\" height=\"24\"/><text x=\"123\" y=\"200\" text-anchor=\"middle\" class=\"u-name\">Tires</text><text x=\"15\" y=\"224\">-count: int</text><text x=\"15\" y=\"248\">+Tires(kilos: int, count: int)</text><text x=\"15\" y=\"264\">+fee(): int</text><text x=\"15\" y=\"280\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M7 208h232M7 232h232\"/><rect class=\"u-frame\" fill=\"none\" x=\"7\" y=\"184\" width=\"232\" height=\"104\"/><rect class=\"u-bg\" fill=\"none\" x=\"269\" y=\"184\" width=\"160\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"269\" y=\"184\" width=\"160\" height=\"24\"/><text x=\"349\" y=\"200\" text-anchor=\"middle\" class=\"u-name\">Crate</text><text x=\"277\" y=\"224\">-bottles: int</text><text x=\"277\" y=\"248\">+Crate(bottles: int)</text><text x=\"277\" y=\"264\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M269 208h160M269 232h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"269\" y=\"184\" width=\"160\" height=\"88\"/><rect class=\"u-bg\" fill=\"none\" x=\"142\" y=\"332\" width=\"225\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"142\" y=\"332\" width=\"225\" height=\"24\"/><text x=\"254.5\" y=\"348\" text-anchor=\"middle\" class=\"u-name\">Scale</text><text x=\"150\" y=\"380\">+Scale()</text><text x=\"150\" y=\"396\">+weigh(item: Weighable): void</text><text x=\"150\" y=\"412\">+total(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M142 356h225M142 364h225\"/><rect class=\"u-frame\" fill=\"none\" x=\"142\" y=\"332\" width=\"225\" height=\"88\"/><text x=\"282\" y=\"83\" class=\"u-mult\">*</text><text x=\"248.5\" y=\"314\" text-anchor=\"end\" class=\"u-role\">weighed</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Weighable</strong>, an interface<ul><li>public method <code>weight(): int</code></li></ul></li><li><strong>DropOff</strong>, an abstract class<ul><li>private field <code>kilos: int</code></li><li>public constructor <code>DropOff(kilos: int)</code></li><li>public abstract method <code>fee(): int</code></li><li>more members, not shown</li></ul></li><li><strong>Tires</strong>, a class that extends DropOff and implements Weighable<ul><li>private field <code>count: int</code></li><li>public constructor <code>Tires(kilos: int, count: int)</code></li><li>public method <code>fee(): int</code></li><li>public method <code>weight(): int</code></li></ul></li><li><strong>Crate</strong>, a class that implements Weighable<ul><li>private field <code>bottles: int</code></li><li>public constructor <code>Crate(bottles: int)</code></li><li>public method <code>weight(): int</code></li></ul></li><li><strong>Scale</strong>, a class<ul><li>public constructor <code>Scale()</code></li><li>public method <code>weigh(item: Weighable): void</code></li><li>public method <code>total(): int</code></li></ul></li></ul><p>Connections:</p><ul><li>Each Scale has any number of Weighable objects (weighed).</li></ul></details></figure>\n\n\nTo read a diagram like this, go through it in layers:\n\n1. **The boxes.** What each one is: a class, an abstract class («abstract») or an interface («interface»).\n2. **The triangles.** Each solid one is an `extends`, each dashed one an `implements`. Tires does both: it extends DropOff and implements Weighable.\n3. **The arrows.** Each is a field, and a star makes it a list. The Scale keeps a list of Weighables: it can weigh crates and tires alike, and any class that implements Weighable later.\n4. **The members**, box by box. A line `...` stands for members that the diagram leaves out, as in DropOff's box: step 6 showed the rest.\n\nTo write the program, start with what needs no other class of the diagram: here the interface and DropOff. Then write the classes that extend or implement them, Tires and Crate, and last the classes whose arrows point at the others, the Scale. Each file then compiles as soon as it's written, since everything it names is already there.\n","task":"The ward's cleaning staff clean anything that's `Cleanable`. `Cleanable.java` is ready. Write the class `Wheelchair` in `Wheelchair.java`, as the diagram shows:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Cleanable and Wheelchair\">\n<svg class=\"uml-svg\" viewBox=\"0 0 201 288\" width=\"201\" height=\"288\" style=\"width:12.57rem;min-width:11.52rem;aspect-ratio:201/288\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge u-dash\" fill=\"none\" d=\"M100.5 97L100.5 130\"/><path class=\"u-tri\" d=\"M100.5 86L93.5 97L107.5 97Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"24\" y=\"6\" width=\"153\" height=\"80\"/><rect class=\"u-head\" fill=\"none\" x=\"24\" y=\"6\" width=\"153\" height=\"40\"/><text x=\"100.5\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«interface»</text><text x=\"100.5\" y=\"38\" text-anchor=\"middle\" class=\"u-name\">Cleanable</text><text x=\"32\" y=\"62\">+clean(): void</text><text x=\"32\" y=\"78\">+isClean(): boolean</text><path class=\"u-sep\" fill=\"none\" d=\"M24 46h153\"/><rect class=\"u-frame\" fill=\"none\" x=\"24\" y=\"6\" width=\"153\" height=\"80\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"130\" width=\"189\" height=\"152\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"130\" width=\"189\" height=\"24\"/><text x=\"100.5\" y=\"146\" text-anchor=\"middle\" class=\"u-name\">Wheelchair</text><text x=\"14\" y=\"170\">-number: int</text><text x=\"14\" y=\"186\">-uses: int</text><text x=\"14\" y=\"210\">+Wheelchair(number: int)</text><text x=\"14\" y=\"226\">+use(): void</text><text x=\"14\" y=\"242\">+clean(): void</text><text x=\"14\" y=\"258\">+isClean(): boolean</text><text x=\"14\" y=\"274\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 154h189M6 194h189\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"130\" width=\"189\" height=\"152\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Cleanable</strong>, an interface<ul><li>public method <code>clean(): void</code></li><li>public method <code>isClean(): boolean</code></li></ul></li><li><strong>Wheelchair</strong>, a class that implements Cleanable<ul><li>private field <code>number: int</code></li><li>private field <code>uses: int</code></li><li>public constructor <code>Wheelchair(number: int)</code></li><li>public method <code>use(): void</code></li><li>public method <code>clean(): void</code></li><li>public method <code>isClean(): boolean</code></li><li>public method <code>toString(): String</code></li></ul></li></ul></details></figure>\n\n\n- A new wheelchair is clean: it has had no uses since it was last cleaned.\n- `use` counts one use, and `clean` sets the count back to 0.\n- `isClean` says whether the wheelchair has had no uses since it was last cleaned.\n- `toString` returns `Wheelchair 7: 2 uses since cleaning`.\n\nThe check runs:\n\n```java\nWheelchair chair = new Wheelchair(7);\nchair.use();\nchair.use();\nSystem.out.println(chair + \" \" + chair.isClean());\nCleanable job = chair;\njob.clean();\nSystem.out.println(chair + \" \" + chair.isClean());\n```\n\nand expects:\n\n```\nWheelchair 7: 2 uses since cleaning false\nWheelchair 7: 0 uses since cleaning true\n```\n","kind":"code","seed":"// ==== Wheelchair.java ====\npublic class Wheelchair {\n\n}\n\n// ==== Cleanable.java ====\npublic interface Cleanable {\n    void clean();\n    boolean isClean();\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Wheelchair chair = new Wheelchair(7);\n        chair.use();\n        chair.use();\n        System.out.println(chair + \" \" + chair.isClean());\n        Cleanable job = chair;\n        job.clean();\n        System.out.println(chair + \" \" + chair.isClean());\n    }\n}\n","solution":"// ==== Wheelchair.java ====\npublic class Wheelchair implements Cleanable {\n    private int number;\n    private int uses;\n\n    public Wheelchair(int number) {\n        this.number = number;\n        this.uses = 0;\n    }\n\n    public void use() {\n        this.uses++;\n    }\n\n    @Override\n    public void clean() {\n        this.uses = 0;\n    }\n\n    @Override\n    public boolean isClean() {\n        return this.uses == 0;\n    }\n\n    @Override\n    public String toString() {\n        return \"Wheelchair \" + this.number + \": \" + this.uses + \" uses since cleaning\";\n    }\n}\n\n// ==== Cleanable.java ====\npublic interface Cleanable {\n    void clean();\n    boolean isClean();\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Wheelchair chair = new Wheelchair(7);\n        chair.use();\n        chair.use();\n        System.out.println(chair + \" \" + chair.isClean());\n        Cleanable job = chair;\n        job.clean();\n        System.out.println(chair + \" \" + chair.isClean());\n    }\n}\n","hints":["The dashed triangle means `public class Wheelchair implements Cleanable`. The two fields are ints, and a new wheelchair starts with 0 uses.","`clean` and `isClean` are Cleanable's methods, so they're `public`, with `@Override` above them. `isClean` returns `this.uses == 0`."],"tests":[{"name":"Test 1","stdin":"","call":"Wheelchair chair = new Wheelchair(7);\nchair.use();\nchair.use();\nSystem.out.println(chair + \" \" + chair.isClean());\nCleanable job = chair;\njob.clean();\nSystem.out.println(chair + \" \" + chair.isClean());\n","expect":"Wheelchair 7: 2 uses since cleaning false\nWheelchair 7: 0 uses since cleaning true","hidden":false},{"name":"Test 2","stdin":"","call":"Wheelchair spare = new Wheelchair(12);\nSystem.out.println(spare + \" \" + spare.isClean());\nspare.use();\nspare.clean();\nspare.use();\nspare.use();\nspare.use();\nSystem.out.println(spare);\n","expect":"Wheelchair 12: 0 uses since cleaning true\nWheelchair 12: 3 uses since cleaning","hidden":true}],"require":[{"pattern":"\\bclass\\s+Wheelchair\\s+implements\\s+Cleanable\\b","message":"The dashed triangle from Wheelchair to Cleanable means `public class Wheelchair implements Cleanable`."}],"forbid":[],"more":[{"task":"Here are the tires of step 6 in the weighing station's diagram. Fill in the three blanks, words from class headers, so that the code matches the diagram.\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Weighable, DropOff and Tires\">\n<svg class=\"uml-svg\" viewBox=\"0 0 319 282\" width=\"319\" height=\"282\" style=\"width:19.94rem;min-width:18.28rem;aspect-ratio:319/282\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M233 137L233 148L187.4 148L187.4 172\"/><path class=\"u-edge u-dash\" fill=\"none\" d=\"M64.5 81L64.5 148L110.1 148L110.1 172\"/><path class=\"u-tri\" d=\"M233 126L226 137L240 137Z\"/><path class=\"u-tri\" d=\"M64.5 70L57.5 81L71.5 81Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"40\"/><text x=\"64.5\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«interface»</text><text x=\"64.5\" y=\"38\" text-anchor=\"middle\" class=\"u-name\">Weighable</text><text x=\"14\" y=\"62\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 46h117\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"6\" width=\"117\" height=\"64\"/><rect class=\"u-bg\" fill=\"none\" x=\"153\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"153\" y=\"6\" width=\"160\" height=\"40\"/><text x=\"233\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"233\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">DropOff</text><text x=\"161\" y=\"62\">-kilos: int</text><text x=\"161\" y=\"86\">+DropOff(kilos: int)</text><text x=\"161\" y=\"102\">+getKilos(): int</text><text x=\"161\" y=\"118\" class=\"u-it\">+fee(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M153 46h160M153 70h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"153\" y=\"6\" width=\"160\" height=\"120\"/><rect class=\"u-bg\" fill=\"none\" x=\"32.8\" y=\"172\" width=\"232\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"32.8\" y=\"172\" width=\"232\" height=\"24\"/><text x=\"148.8\" y=\"188\" text-anchor=\"middle\" class=\"u-name\">Tires</text><text x=\"40.8\" y=\"212\">-count: int</text><text x=\"40.8\" y=\"236\">+Tires(kilos: int, count: int)</text><text x=\"40.8\" y=\"252\">+fee(): int</text><text x=\"40.8\" y=\"268\">+weight(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M32.8 196h232M32.8 220h232\"/><rect class=\"u-frame\" fill=\"none\" x=\"32.8\" y=\"172\" width=\"232\" height=\"104\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Weighable</strong>, an interface<ul><li>public method <code>weight(): int</code></li></ul></li><li><strong>DropOff</strong>, an abstract class<ul><li>private field <code>kilos: int</code></li><li>public constructor <code>DropOff(kilos: int)</code></li><li>public method <code>getKilos(): int</code></li><li>public abstract method <code>fee(): int</code></li></ul></li><li><strong>Tires</strong>, a class that extends DropOff and implements Weighable<ul><li>private field <code>count: int</code></li><li>public constructor <code>Tires(kilos: int, count: int)</code></li><li>public method <code>fee(): int</code></li><li>public method <code>weight(): int</code></li></ul></li></ul></details></figure>\n\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        Tires tires = new Tires(36, 4);\n        Weighable item = tires;\n        DropOff dropOff = tires;\n        System.out.println(item.weight() + \" \" + dropOff.fee());\n    }\n}\n// ==== Weighable.java ====\npublic interface Weighable {\n    int weight();\n}\n// ==== DropOff.java ====\npublic [[abstract]] class DropOff {\n    private int kilos;\n\n    public DropOff(int kilos) {\n        this.kilos = kilos;\n    }\n\n    public int getKilos() {\n        return this.kilos;\n    }\n\n    public abstract int fee();\n}\n// ==== Tires.java ====\npublic class Tires [[extends]] DropOff [[implements]] Weighable {\n    private int count;\n\n    public Tires(int kilos, int count) {\n        super(kilos);\n        this.count = count;\n    }\n\n    @Override\n    public int fee() {\n        return 4 * this.count;\n    }\n\n    @Override\n    public int weight() {\n        return this.getKilos();\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        Tires tires = new Tires(36, 4);\n        Weighable item = tires;\n        DropOff dropOff = tires;\n        System.out.println(item.weight() + \" \" + dropOff.fee());\n    }\n}\n// ==== Weighable.java ====\npublic interface Weighable {\n    int weight();\n}\n// ==== DropOff.java ====\npublic abstract class DropOff {\n    private int kilos;\n\n    public DropOff(int kilos) {\n        this.kilos = kilos;\n    }\n\n    public int getKilos() {\n        return this.kilos;\n    }\n\n    public abstract int fee();\n}\n// ==== Tires.java ====\npublic class Tires extends DropOff implements Weighable {\n    private int count;\n\n    public Tires(int kilos, int count) {\n        super(kilos);\n        this.count = count;\n    }\n\n    @Override\n    public int fee() {\n        return 4 * this.count;\n    }\n\n    @Override\n    public int weight() {\n        return this.getKilos();\n    }\n}\n","hints":["Look above DropOff's name, and at the two lines from Tires: which one is solid, which dashed?","«abstract» goes into DropOff's header as `abstract`. A solid line with a triangle is `extends`, a dashed one is `implements`, and `extends` comes first: `public class Tires extends DropOff implements Weighable`."],"tests":[{"name":"Output","stdin":"","expect":"36 16","hidden":false}],"require":[],"forbid":[]},{"task":"To close the module, a whole program from one diagram. A board-game café bills each visit for table time and drinks. Write the four parts of this diagram, each in its own file:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Billable, TableTime, Drink and Visit\">\n<svg class=\"uml-svg\" viewBox=\"0 0 614 282\" width=\"614\" height=\"282\" style=\"width:38.38rem;min-width:35.18rem;aspect-ratio:614/282\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge u-dash\" fill=\"none\" d=\"M297 81L297 148L144 148L144 172\"/><path class=\"u-edge u-dash\" fill=\"none\" d=\"M297 81L297 148L450 148L450 172\"/><path class=\"u-edge\" fill=\"none\" d=\"M404 38L352 38\"/><path class=\"u-tri\" d=\"M297 70L290 81L304 81Z\"/><path class=\"u-edge\" fill=\"none\" d=\"M362 32.5L352 38L362 43.5\"/><rect class=\"u-bg\" fill=\"none\" x=\"242\" y=\"6\" width=\"110\" height=\"64\"/><rect class=\"u-head\" fill=\"none\" x=\"242\" y=\"6\" width=\"110\" height=\"40\"/><text x=\"297\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«interface»</text><text x=\"297\" y=\"38\" text-anchor=\"middle\" class=\"u-name\">Billable</text><text x=\"250\" y=\"62\">+price(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M242 46h110\"/><rect class=\"u-frame\" fill=\"none\" x=\"242\" y=\"6\" width=\"110\" height=\"64\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"172\" width=\"276\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"172\" width=\"276\" height=\"24\"/><text x=\"144\" y=\"188\" text-anchor=\"middle\" class=\"u-name\">TableTime</text><text x=\"14\" y=\"212\">-hours: int</text><text x=\"14\" y=\"228\">-players: int</text><text x=\"14\" y=\"252\">+TableTime(hours: int, players: int)</text><text x=\"14\" y=\"268\">+price(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 196h276M6 236h276\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"172\" width=\"276\" height=\"104\"/><rect class=\"u-bg\" fill=\"none\" x=\"312\" y=\"172\" width=\"276\" height=\"104\"/><rect class=\"u-head\" fill=\"none\" x=\"312\" y=\"172\" width=\"276\" height=\"24\"/><text x=\"450\" y=\"188\" text-anchor=\"middle\" class=\"u-name\">Drink</text><text x=\"320\" y=\"212\">-name: String</text><text x=\"320\" y=\"228\">-large: boolean</text><text x=\"320\" y=\"252\">+Drink(name: String, large: boolean)</text><text x=\"320\" y=\"268\">+price(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M312 196h276M312 236h276\"/><rect class=\"u-frame\" fill=\"none\" x=\"312\" y=\"172\" width=\"276\" height=\"104\"/><rect class=\"u-bg\" fill=\"none\" x=\"404\" y=\"6\" width=\"204\" height=\"120\"/><rect class=\"u-head\" fill=\"none\" x=\"404\" y=\"6\" width=\"204\" height=\"24\"/><text x=\"506\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Visit</text><text x=\"412\" y=\"46\">-guest: String</text><text x=\"412\" y=\"70\">+Visit(guest: String)</text><text x=\"412\" y=\"86\">+add(item: Billable): void</text><text x=\"412\" y=\"102\">+total(): int</text><text x=\"412\" y=\"118\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M404 30h204M404 54h204\"/><rect class=\"u-frame\" fill=\"none\" x=\"404\" y=\"6\" width=\"204\" height=\"120\"/><text x=\"358\" y=\"31\" class=\"u-mult\">*</text><text x=\"378\" y=\"53\" text-anchor=\"middle\" class=\"u-role\">items</text></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Billable</strong>, an interface<ul><li>public method <code>price(): int</code></li></ul></li><li><strong>TableTime</strong>, a class that implements Billable<ul><li>private field <code>hours: int</code></li><li>private field <code>players: int</code></li><li>public constructor <code>TableTime(hours: int, players: int)</code></li><li>public method <code>price(): int</code></li></ul></li><li><strong>Drink</strong>, a class that implements Billable<ul><li>private field <code>name: String</code></li><li>private field <code>large: boolean</code></li><li>public constructor <code>Drink(name: String, large: boolean)</code></li><li>public method <code>price(): int</code></li></ul></li><li><strong>Visit</strong>, a class<ul><li>private field <code>guest: String</code></li><li>public constructor <code>Visit(guest: String)</code></li><li>public method <code>add(item: Billable): void</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 Visit has any number of Billable objects (items).</li></ul></details></figure>\n\n\n- Table time costs 3 dollars per player per hour.\n- A drink costs 4 dollars, or 6 if it's large.\n- A new visit has nothing on its bill. `add` puts an item on it, and `total` returns the prices of the visit's items added up.\n- A visit's `toString` returns `Visit of Ana: 3 items, 34 dollars`, with the visit's own guest, number of items and total.\n\nThe check runs:\n\n```java\nVisit visit = new Visit(\"Ana\");\nvisit.add(new TableTime(2, 4));\nvisit.add(new Drink(\"cocoa\", true));\nvisit.add(new Drink(\"tea\", false));\nSystem.out.println(visit);\nSystem.out.println(new TableTime(1, 3).price());\n```\n\nand expects:\n\n```\nVisit of Ana: 3 items, 34 dollars\n9\n```\n","kind":"code","seed":"// ==== Billable.java ====\npublic interface Billable {\n\n}\n\n// ==== TableTime.java ====\npublic class TableTime {\n\n}\n\n// ==== Drink.java ====\npublic class Drink {\n\n}\n\n// ==== Visit.java ====\npublic class Visit {\n\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Visit visit = new Visit(\"Ana\");\n        visit.add(new TableTime(2, 4));\n        visit.add(new Drink(\"cocoa\", true));\n        visit.add(new Drink(\"tea\", false));\n        System.out.println(visit);\n        System.out.println(new TableTime(1, 3).price());\n    }\n}\n","solution":"// ==== Billable.java ====\npublic interface Billable {\n    int price();\n}\n\n// ==== TableTime.java ====\npublic class TableTime implements Billable {\n    private int hours;\n    private int players;\n\n    public TableTime(int hours, int players) {\n        this.hours = hours;\n        this.players = players;\n    }\n\n    @Override\n    public int price() {\n        return 3 * this.players * this.hours;\n    }\n}\n\n// ==== Drink.java ====\npublic class Drink implements Billable {\n    private String name;\n    private boolean large;\n\n    public Drink(String name, boolean large) {\n        this.name = name;\n        this.large = large;\n    }\n\n    @Override\n    public int price() {\n        if (this.large) {\n            return 6;\n        }\n        return 4;\n    }\n}\n\n// ==== Visit.java ====\nimport java.util.ArrayList;\n\npublic class Visit {\n    private String guest;\n    private ArrayList<Billable> items;\n\n    public Visit(String guest) {\n        this.guest = guest;\n        this.items = new ArrayList<>();\n    }\n\n    public void add(Billable item) {\n        this.items.add(item);\n    }\n\n    public int total() {\n        int sum = 0;\n        for (Billable item : this.items) {\n            sum += item.price();\n        }\n        return sum;\n    }\n\n    @Override\n    public String toString() {\n        return \"Visit of \" + this.guest + \": \" + this.items.size() + \" items, \" + this.total() + \" dollars\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Visit visit = new Visit(\"Ana\");\n        visit.add(new TableTime(2, 4));\n        visit.add(new Drink(\"cocoa\", true));\n        visit.add(new Drink(\"tea\", false));\n        System.out.println(visit);\n        System.out.println(new TableTime(1, 3).price());\n    }\n}\n","hints":["Write the files in the order of the lesson: Billable first (one method header), then TableTime and Drink, which implement it (the dashed triangles), and Visit last. Visit's arrow with a star is `private ArrayList<Billable> items;`.","Visit's `total` adds up `price()` over its list, and each item runs its own class's `price`. `toString` uses the list's size and `total()`."],"tests":[{"name":"Test 1","stdin":"","call":"Visit visit = new Visit(\"Ana\");\nvisit.add(new TableTime(2, 4));\nvisit.add(new Drink(\"cocoa\", true));\nvisit.add(new Drink(\"tea\", false));\nSystem.out.println(visit);\nSystem.out.println(new TableTime(1, 3).price());\n","expect":"Visit of Ana: 3 items, 34 dollars\n9","hidden":false},{"name":"Test 2","stdin":"","call":"Visit visit = new Visit(\"Leo\");\nSystem.out.println(visit + \" \" + visit.total());\nBillable coffee = new Drink(\"coffee\", false);\nvisit.add(coffee);\nvisit.add(new TableTime(3, 2));\nvisit.add(coffee);\nSystem.out.println(visit.total() + \" \" + coffee.price());\nSystem.out.println(visit);\n","expect":"Visit of Leo: 0 items, 0 dollars 0\n26 4\nVisit of Leo: 3 items, 26 dollars","hidden":true}],"require":[{"pattern":"\\binterface\\s+Billable\\s*\\{[^}]*\\bint\\s+price\\s*\\(\\s*\\)\\s*;","message":"Billable's box lists one method: write `int price();` inside the interface."},{"pattern":"\\bimplements\\s+Billable\\b","min":2,"message":"Both dashed triangles point to Billable: TableTime and Drink each `implements Billable`."},{"pattern":"\\bprivate\\s+ArrayList<Billable>\\s+\\w+\\s*[;=]","message":"The arrow from Visit to Billable, with a star, is a list: `private ArrayList<Billable> items;`."}],"forbid":[]}]}