Programming technique · B3.1.2, B3.1.4
Classes and objects
A class describes a type. Each object created from that class has its own identity and instance state while following the same initialisation and method definitions.
Design before coding
+ name: String+ score: int + Player(name: String, score: int)+ addPoints(points: int): void+ summary(): String + name: str+ score: int + __init__(name, score)+ add_points(points)+ summary(): str Define one class
Instance state
Named values stored by each object, such as name or score.
Constructor
Code that establishes a new object's starting state when new creates it.
__init__
The initialiser runs after the object is created and establishes its starting attributes.
Instance method
Behaviour that acts on the receiving object.
Create independent objects
namescore initialiseadd pointssummary ari
- name
- "Ari"
- score
- 10 → 15
sam
- name
- "Sam"
- score
- 20
Trace construction and method calls
| Statement | Created object or receiver | Ari state | Sam state |
|---|---|---|---|
new Player("Ari", 10) | new object assigned to ari | Ari, 10 | — |
new Player("Sam", 20) | new object assigned to sam | Ari, 10 | Sam, 20 |
ari.addPoints(5) | ari | Ari, 15 | Sam, 20 |
| Statement | Created object or receiver | Ari state | Sam state |
|---|---|---|---|
Player("Ari", 10) | object assigned to ari | Ari, 10 | — |
Player("Sam", 20) | object assigned to sam | Ari, 10 | Sam, 20 |
ari.add_points(5) | ari becomes self | Ari, 15 | Sam, 20 |
Class is not object
The class is the reusable type definition. Ari and Sam are separate objects created from it.
this means this object
Inside the class, this.name identifies the field belonging to the receiving object.
self is the receiver
Python passes the receiving object into the method as self.
Caller prints returned text
summary() returns a value; the caller decides whether and where to display it.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Student Object
SelectedDesign and construct a Student class with name and grade fields, a constructor, an improveGrade method and a returning summary method. Create two objects with different starting values, change only one student and prove that the second object's state remains unchanged. Include a small UML class diagram before coding.
Bank Account
SelectedDesign and construct a BankAccount class with holder and balance fields, a constructor, deposit and withdraw methods, and a returning summary method. Instantiate at least two accounts and demonstrate that method calls affect only the receiving object. Detailed private-state rules are introduced in encapsulation, so keep this first model transparent and focused on object construction.
Dice Class
SelectedDesign and construct a Dice class with sides and currentValue fields. The constructor sets the number of sides, roll generates and stores a valid random value, and summary returns the object's current state. Create dice with different numbers of sides and test that each keeps independent state.
Virtual Pet
SelectedDesign and construct a Pet class with name, hunger and happiness fields. Add a constructor, feed and play methods, and a returning summary method. Create two pets with different starting state, call different methods on them and trace which object receives each call. Include UML that matches the completed class.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.