Programming Companion
Java

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.

IB DP CS standards B3.1.2 and B3.1.4: Construct class designs using UML, define classes, establish starting state and instantiate objects.

Design before coding

Player
+ name: String+ score: int
+ Player(name: String, score: int)+ addPoints(points: int): void+ summary(): String
The class name, fields and operations correspond directly to the requirements.
Player
+ name: str+ score: int
+ __init__(name, score)+ add_points(points)+ summary(): str
The UML describes the same responsibilities even though Python does not declare these types in the class syntax shown.

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

Player class
namescore
initialiseadd pointssummary

ari

name
"Ari"
score
10 → 15

sam

name
"Sam"
score
20
The method is defined once, but ari.addPoints(5) changes Ari because ari is the receiver. Sam's state is independent.
The method is defined once, but ari.add_points(5) changes Ari because ari becomes self. Sam's state is independent.

Trace construction and method calls

StatementCreated object or receiverAri stateSam state
new Player("Ari", 10)new object assigned to ariAri, 10
new Player("Sam", 20)new object assigned to samAri, 10Sam, 20
ari.addPoints(5)ariAri, 15Sam, 20
StatementCreated object or receiverAri stateSam state
Player("Ari", 10)object assigned to ariAri, 10
Player("Sam", 20)object assigned to samAri, 10Sam, 20
ari.add_points(5)ari becomes selfAri, 15Sam, 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

Challenge ID: PC-T19-C01 · Standards: B3.1.2, B3.1.4

Design 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.

Scaffold available
A Student class blueprint connected to several student objects with independent values.

Bank Account

Challenge ID: PC-T19-C02 · Standards: B3.1.2, B3.1.4

Design 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.

Scaffold available
A BankAccount object with holder, balance, deposit and withdraw responsibilities.

Dice Class

Challenge ID: PC-T19-C03 · Standards: B3.1.2, B3.1.4

Design 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.

Scaffold available
A Dice class blueprint creating dice objects with their own current values.

Virtual Pet

Challenge ID: PC-T19-C04 · Standards: B3.1.2, B3.1.4

Design 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.

Scaffold available
A virtual Pet object with name, hunger, happiness, feed and play responsibilities.