Designing classes with a class diagram

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Designing classes with a class diagram

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A class diagram box is split into three parts. What goes in the TOP part?
A The methods, e.g. bark()
B The class name, e.g. Dog
2. Which part of a class diagram lists the data an object stores, like name and age?
A The attributes section (middle)
B The methods section (bottom)
3. A class diagram for Student lists 'study()' and 'get_grade()'. What are these?
A Attributes, because they store data about the student
B Methods, because they are actions the class can do
4. In a class diagram, which way does the inheritance arrow point?
A From the child class to the parent class
B From the parent class to the child class
5. A diagram shows an arrow from Car to Vehicle. Which code matches it?
A class Car(Vehicle):
B class Vehicle(Car):

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Dog - name - age + speak() AThe arrow points the wrong way. It should go from the child class Puppy to the parent class Dog.
2Puppy ──▷ Dog BThe box is in the wrong order. A class diagram lists the class name first, then the attributes, then the methods.
3Turning a diagram box into code: the class name from the box becomes the class header Cclass Puppy(Dog):
4Turning an inheritance arrow from Puppy to Dog into code: the parent class goes in brackets Dclass Dog:
5Diagram box for Dog lists: Dog, speak(), name, age (methods before attributes) EA class diagram box for Dog: the class name on top, then its attributes (name, age), then its methods (speak)
6Dog ──▷ Puppy (but the code says class Puppy(Dog):) FAn inheritance arrow showing that Puppy is a child class that inherits from the parent class Dog

Definitions

class diagram

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
# Diagram box: Dog _____ Dog: pass
2.
# Arrow: Dog points to Animal class Dog(______): pass
3.
# Pet box has the attribute name class Pet: def ________(self, name):
4.
# Pet box - method: speak() class Pet: def _____(self):
5.
# Arrow: Puppy points to Dog class _____(Dog): pass

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Dog:
B def __init__(self, name):
C name = name
D def bark(self):
E print("Woof")
Question 2
A class Pet:
B def __init__(name):
C self.name = name
Question 3
A class Animal:
B speak(self):
C print("...")
Question 4
A class Animal: pass
B class Dog -> Animal:
C def bark(self):
D print("Woof")
Question 5
A class Student:
B def __init__(self, name):
C self.name = name
D def greet(self)
E print("Hi " + self.name)

Answer Key

Designing classes with a class diagram
Section A: Quick Questions
Q1
B — The class name, e.g. Dog
Q2
A — The attributes section (middle)
Q3
B — Methods, because they are actions the class can do
Q4
A — From the child class to the parent class
Q5
A — class Car(Vehicle):
Section B: Match the Pairs
Q1
Dog - name - age + speak() → A class diagram box for Dog: the class name on top, then its attributes (name, age), then its methods (speak)
Q2
Puppy ──▷ Dog → An inheritance arrow showing that Puppy is a child class that inherits from the parent class Dog
Q3
Turning a diagram box into code: the class name from the box becomes the class header → class Dog:
Q4
Turning an inheritance arrow from Puppy to Dog into code: the parent class goes in brackets → class Puppy(Dog):
Q5
Diagram box for Dog lists: Dog, speak(), name, age (methods before attributes) → The box is in the wrong order. A class diagram lists the class name first, then the attributes, then the methods.
Q6
Dog ──▷ Puppy (but the code says class Puppy(Dog):) → The arrow points the wrong way. It should go from the child class Puppy to the parent class Dog.
Section C: Definitions
Q1
class diagram — A box listing a class's name, attributes and methods. An arrow joins a child class to the parent it inherits from. You draw it as a plan before writing the code.
Section D: Fill in the Blanks
Q1
# Diagram box: Dog class Dog: pass
Q2
# Arrow: Dog points to Animal class Dog(Animal): pass
Q3
# Pet box has the attribute name class Pet: def __init__(self, name):
Q4
# Pet box - method: speak() class Pet: def speak(self):
Q5
# Arrow: Puppy points to Dog class Puppy(Dog): pass
Section E: Spot the Error
Q1
C — The diagram lists name as an attribute of Dog. Writing name = name only makes a local variable, so the object never keeps it. It should be self.name = name.
Q2
B — Every method in a class needs self as its first parameter so it can reach the object's attributes. It should be def __init__(self, name):.
Q3
B — The speak method from the diagram must be defined with the def keyword. It should be def speak(self):.
Q4
B — The diagram's inheritance arrow becomes the parent class name in brackets after the child class name. It should be class Dog(Animal):.
Q5
D — The greet method from the diagram is missing the colon at the end of its def line. Python needs the colon to start the method body. It should be def greet(self):.