Choosing properties and methods for a class from a scenario

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

Choosing properties and methods for a class from a scenario

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A game needs a Player class. The scenario says: 'A player has a name and a score.' Which are properties?
A name and score
B has and needs
2. A scenario says: 'A dog can bark and sit.' How should bark and sit be written in the Dog class?
A As properties: this.bark and this.sit
B As methods: bark() and sit()
3. A library program tracks books. The scenario says: 'A book has a title, an author and can be borrowed.' Which is the best method name?
A borrow()
B title()
4. A school canteen program only needs each Student's name and lunch order. The scenario also mentions their favourite colour and pet's name. What should you do?
A Leave out favourite colour and pet's name
B Add them all as properties just in case
5. Scenario: 'A bank account has a balance. You can deposit and withdraw money.' Which list is correct?
A Properties: deposit, withdraw. Method: balance()
B Property: balance. Methods: deposit(), withdraw()

Match the Pairs

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

Left columnRight column
1this.balance = 0; Aconstructor(name, house) { this.name = name; this.house = house; }
2deposit(amount) { this.balance += amount; } Bclass Player { jump() { console.log('Jump!'); } }
3Scenario: 'A student has a name and a house.' The nouns name and house are stored as properties. CA method: the verb 'deposit' is an action the object can do
4Scenario: 'A game character can jump.' The verb jump becomes a method. DBark is a verb (an action), so it should be a method, not a property.
5class Book { constructor(title, authorsFavouriteColour, coverPhotoSize) { } } EA property: the noun 'balance' is data the object stores
6class Dog { constructor() { this.bark = 'Woof'; } } FIt stores details the program does not need. Leave out unneeded detail like favourite colour and photo size.

Definitions

properties and methods

Fill in the Blanks

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

1.
// Scenario: a student has a name. constructor(name) { this.____ = name; }
2.
// Scenario: a dog can bark. class Dog { ____() { console.log("Woof!"); }
3.
// A player can add points to the score. player._________(5);
4.
// Needed: title and borrowed (not colour) this.________ = false;
5.
// A cat is hungry until it eats. eat() { this.______ = false; }

Spot the Error

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

Question 1
A class Pet {
B class Pet() {
C this.name = name;
D this.age = age;
E }
F }
Question 2
A class Player {
B constructor(name, score) {
C constructer(name, score) {
D this.score = score;
E }
F }
Question 3
A Class Car {
B constructor(model) {
C this.model = model;
D }
E drive() { return 'Vroom'; }
F }
Question 4
A class Dog {
B constructor(name, breed) {
C name = name;
D this.breed = breed;
E }
F }
Question 5
A class Light
B constructor() {
C this.isOn = false;
D }
E }

Answer Key

Choosing properties and methods for a class from a scenario
Section A: Quick Questions
Q1
A — name and score
Q2
B — As methods: bark() and sit()
Q3
A — borrow()
Q4
A — Leave out favourite colour and pet's name
Q5
B — Property: balance. Methods: deposit(), withdraw()
Section B: Match the Pairs
Q1
this.balance = 0; → A property: the noun 'balance' is data the object stores
Q2
deposit(amount) { this.balance += amount; } → A method: the verb 'deposit' is an action the object can do
Q3
Scenario: 'A student has a name and a house.' The nouns name and house are stored as properties. → constructor(name, house) { this.name = name; this.house = house; }
Q4
Scenario: 'A game character can jump.' The verb jump becomes a method. → class Player { jump() { console.log('Jump!'); } }
Q5
class Book { constructor(title, authorsFavouriteColour, coverPhotoSize) { } } → It stores details the program does not need. Leave out unneeded detail like favourite colour and photo size.
Q6
class Dog { constructor() { this.bark = 'Woof'; } } → Bark is a verb (an action), so it should be a method, not a property.
Section C: Definitions
Q1
properties and methods — Read the scenario: nouns (data to store) become properties and verbs (actions) become methods. Keep only the details the program needs and leave the rest out.
Section D: Fill in the Blanks
Q1
// Scenario: a student has a name. constructor(name) { this.name = name; }
Q2
// Scenario: a dog can bark. class Dog { bark() { console.log("Woof!"); }
Q3
// A player can add points to the score. player.addPoints(5);
Q4
// Needed: title and borrowed (not colour) this.borrowed = false;
Q5
// A cat is hungry until it eats. eat() { this.hungry = false; }
Section E: Spot the Error
Q1
B — A class declaration does not take parentheses after its name. The class that holds the Pet's properties (name, age) should start with class Pet {.
Q2
C — The method that sets up the properties must be spelled constructor. With the typo, JavaScript treats it as an ordinary method and the properties are never set when you use new.
Q3
A — The class keyword must be lowercase because JavaScript is case sensitive. It should be class Car {.
Q4
C — The noun 'name' should be stored as a property, so it needs this.name = name;. Without this, the value is not saved on the object.
Q5
A — The class body must be opened with a curly brace. The line should be class Light {.