Final challenge: read and draw
After this: Name an arrow, choose a diamond, count a diagram, predict a program and write a class line from its arrow.
This challenge has five steps, and each step is scored on its own. You will name a line from its style and its end, choose a diamond for a new pair of classes, draw a short program in your head and count its diagram, predict what three people describe, and write the first line of a class from its arrow. Code is shown in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Name the line
Name the line
A dashed line with a hollow triangle runs from Card to PaymentMethod. Which sentence says what it means?
- Card implements PaymentMethod
Choose the diamond
A Team keeps a list of players. Players are made first, sign for a team, and can move to another team later. How is the line between Team and Player drawn?
Count the diagram
Draw the class diagram of this program. How many boxes and lines does it have?
class Plant: def __init__(self, name): self.name = name
class Rose(Plant): def smell(self): return self.name + " smells sweet"
class Bed: def __init__(self): self.plants = []
def plant(self, p): self.plants.append(p)
class Garden: def __init__(self): self.beds = []
def new_bed(self): b = Bed() self.beds.append(b) return b
class Gardener: def water(self, bed): print("watering", len(bed.plants), "plants")
garden = Garden()bed = garden.new_bed()bed.plant(Rose("Rosa"))bed.plant(Plant("Fern"))Gardener().water(bed)import java.util.ArrayList;import java.util.List;
class Plant { protected String name;
Plant(String name) { this.name = name; }}
class Rose extends Plant { Rose(String name) { super(name); }
String smell() { return name + " smells sweet"; }}
class Bed { List<Plant> plants = new ArrayList<>();
void plant(Plant p) { plants.add(p); }}
class Garden { private List<Bed> beds = new ArrayList<>();
Bed newBed() { Bed b = new Bed(); beds.add(b); return b; }}
class Gardener { void water(Bed bed) { System.out.println("watering " + bed.plants.size() + " plants"); }}
public class Main { public static void main(String[] args) { Garden garden = new Garden(); Bed bed = garden.newBed(); bed.plant(new Rose("Rosa")); bed.plant(new Plant("Fern")); new Gardener().water(bed); }}Predict it
Student and Teacher are built on Person, and each writes its own describe, as the school diagram shows. What does this print?
class Person: def __init__(self, name): self.name = name
def describe(self): return self.name
class Student(Person): def __init__(self, name, year): super().__init__(name) self.year = year
def describe(self): return f"{self.name} (year {self.year})"
class Teacher(Person): def __init__(self, name, subject): super().__init__(name) self.subject = subject
def describe(self): return f"{self.name}, {self.subject}"
people = [Teacher("Ms Iyer", "Art"), Student("Chen", 1), Person("Sam")]for p in people: print(p.describe())import java.util.List;
class Person { protected String name;
Person(String name) { this.name = name; }
String describe() { return name; }}
class Student extends Person { private int year;
Student(String name, int year) { super(name); this.year = year; }
@Override String describe() { return name + " (year " + year + ")"; }}
class Teacher extends Person { private String subject;
Teacher(String name, String subject) { super(name); this.subject = subject; }
@Override String describe() { return name + ", " + subject; }}
public class Main { public static void main(String[] args) { List<Person> people = List.of(new Teacher("Ms Iyer", "Art"), new Student("Chen", 1), new Person("Sam")); for (Person p : people) { System.out.println(p.describe()); } }}Write the class line
The diagram has a solid line from Teacher to Person, with a hollow triangle at Person. Complete the first line of Teacher, so that the program prints Mr Rao, Maths.
class Person: def __init__(self, name): self.name = name
def describe(self): return self.name
____________________ # ← Teacher is a Person def __init__(self, name, subject): super().__init__(name) self.subject = subject
def describe(self): return f"{self.name}, {self.subject}"
print(Teacher("Mr Rao", "Maths").describe())class Person { protected String name;
Person(String name) { this.name = name; }
String describe() { return name; }}
____________________ // ← Teacher is a Person private String subject;
Teacher(String name, String subject) { super(name); this.subject = subject; }
@Override String describe() { return name + ", " + subject; }}
public class Main { public static void main(String[] args) { System.out.println(new Teacher("Mr Rao", "Maths").describe()); }}- Name a line from its style and its end, choose a diamond for a new pair, count the diagram of a short program, predict a family of classes, and write a class line from its arrow.