Writing an object description from a requirement

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

Writing an object description from a requirement

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A requirement says: 'The library records each book's title and number of pages.' Which object description fits best?
A Book | title: string, pages: integer | getTitle(), getPages()
B Library | books: array | addBook()
2. Requirement: 'A student's name and age must be stored and the age can be changed.' Which description is correct?
A Student | name: integer, age: string | getAge(), setAge(newAge)
B Student | name: string, age: integer | getAge(), setAge(newAge)
3. Requirement: 'A pet shop tracks each dog's name and weight in kilograms.' Which attribute list is better?
A name: string, weight: float, favouriteColour: string, owner: string
B name: string, weight: float
4. Which method name follows camelCase for reading the attribute 'totalMarks'?
A getTotalMarks()
B get_total_marks()
5. Requirement: 'The system must show a car's registration and let the owner update its odometer reading.' Which methods suit the Car object?
A getRegistration(), setOdometer(newReading)
B setRegistration(newReg), deleteCar(), driveToSchool()

Match the Pairs

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

Left columnRight column
1Student | name: string, age: integer | getAge(), setAge(newAge) AStudent | name: string, age: integer | getAge(), setAge(newAge)
2name: string, age: integer BA complete object description: the object name, its attributes with data types, then its methods
3A requirement says a library must record each book's title and number of pages, so the object is Book and only those two attributes are needed CThe attributes part of the description: each data item the object stores, with its data type
4Methods are named with the same convention as the rest of the program (camelCase), starting with a verb such as get or set DThe attributes have no data types. They should be written as name: string, age: integer.
5Student | name, age | getAge(), setAge(newAge) EfavouriteColour is not needed by the requirement. Only relevant attributes should be included, so it should be removed.
6Student | name: string, age: integer, favouriteColour: string | getAge() FBook | title: string, pages: integer | getTitle(), getPages()

Definitions

object description

Fill in the Blanks

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

1.
Requirement: store a pet's name and age ___ | name: string, age: integer | getAge()
2.
Requirement: an item has a name and cost Item | name: string, cost: _____ | getCost()
3.
Requirement: show a dog's name Dog | name: string, age: integer | _______()
4.
Requirement: a cat's age can be changed Cat | age: integer | ______(newAge)
5.
Requirement: store a car's make and year Car | make: string, ____: integer | getYear()

Spot the Error

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

Question 1
A Object: students
B Attr: name: string
C Attr: age: integer
D Method: getAge()
Question 2
A Object: Book
B Attr: title: integer
C Attr: pages: integer
D Method: getTitle()
Question 3
A Need: track a pet's name
B Object: Owner
C Attr: name: string
D Method: getName()
Question 4
A Object: pet_owner
B Attr: name: string
C Attr: phone: string
D Method: getPhone()
Question 5
A Object: Product
B Attr: price
C Attr: stock: integer
D Method: getPrice()

Answer Key

Writing an object description from a requirement
Section A: Quick Questions
Q1
A — Book | title: string, pages: integer | getTitle(), getPages()
Q2
B — Student | name: string, age: integer | getAge(), setAge(newAge)
Q3
B — name: string, weight: float
Q4
A — getTotalMarks()
Q5
A — getRegistration(), setOdometer(newReading)
Section B: Match the Pairs
Q1
Student | name: string, age: integer | getAge(), setAge(newAge) → A complete object description: the object name, its attributes with data types, then its methods
Q2
name: string, age: integer → The attributes part of the description: each data item the object stores, with its data type
Q3
A requirement says a library must record each book's title and number of pages, so the object is Book and only those two attributes are needed → Book | title: string, pages: integer | getTitle(), getPages()
Q4
Methods are named with the same convention as the rest of the program (camelCase), starting with a verb such as get or set → Student | name: string, age: integer | getAge(), setAge(newAge)
Q5
Student | name, age | getAge(), setAge(newAge) → The attributes have no data types. They should be written as name: string, age: integer.
Q6
Student | name: string, age: integer, favouriteColour: string | getAge() → favouriteColour is not needed by the requirement. Only relevant attributes should be included, so it should be removed.
Section C: Definitions
Q1
object description — Names an object, its attributes with data types and its methods, using only what the requirement needs. Example: Student has name: string, age: integer, getAge().
Section D: Fill in the Blanks
Q1
Requirement: store a pet's name and age Pet | name: string, age: integer | getAge()
Q2
Requirement: an item has a name and cost Item | name: string, cost: float | getCost()
Q3
Requirement: show a dog's name Dog | name: string, age: integer | getName()
Q4
Requirement: a cat's age can be changed Cat | age: integer | setAge(newAge)
Q5
Requirement: store a car's make and year Car | make: string, year: integer | getYear()
Section E: Spot the Error
Q1
A — An object is named with a singular, capitalised noun because it describes one item. It should be 'Student', not the lowercase plural 'students'.
Q2
B — A title is text, so its data type must be string. It should be 'Attr: title: string'.
Q3
B — The requirement is about a pet's name, so the object to describe is Pet, not Owner. It should be 'Object: Pet'.
Q4
A — Object names use capitalised words joined together with no underscores, so snake case breaks the convention. It should be 'Object: PetOwner'.
Q5
B — Every attribute in an object description needs a data type. A price can have cents, so it should be 'Attr: price: float'.