Classes (OOP) (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Classes (Object-Oriented Programming)

What is a class?

  • A class is a blueprint used to create objects in Object-Oriented Programming (OOP)

  • It defines a structure for attributes (data) and methods (behaviour)

Key terms

Term

Definition

Object

An instance of a class with its own state and behaviour

Attribute

A variable defined in a class (also called a class variable)

Instance Variable

Attribute stored inside an individual object

Method

A function defined within a class; describes object behaviour

Instantiation

The process of creating an object from a class

Identifier

The name used to refer to an object

Example

  • A Student class could contain:

    • Attributes: name, dateOfBirth, gender

    • Each Student object (e.g. John, 06/10/2015, Male) has its own values

Prebuilt vs custom classes

Prebuilt classes

Custom classes

Provided by the language

Defined by the programmer

E.g. String, Date, Random, Scanner in Java

E.g. Animal, Student, Book

  • Below is a visual representation of both a class and objects that have been instantiated

  • In the image, the identifiers are P1 and P2

Example of a class and objects

Example of a class and objects

Programming classes (OOP)

How do you program a class?

  • In this example we will:

    • Define a class for a car sales program where the following is required:

      • Name of Manufacturer

      • Model of Car

      • Price

      • Mileage

      • Whether the car is preowned

  • Instantiate two objects for the car sales program ensuring that they have appropriate identifiers

  • List two methods that would be useful for a car object

Pseudocode

classes

Pseudocode for creating classes, a constructor, and functions

Java

//creating a class called cars

public class Cars {

//creating class attributes for a car

    private String Manufacturer;

    private String Model;

    private int Price;

    private int Mileage;

    private boolean PreOwned;

    //Constructor - This is used to create the objects. More on this in the object chapter)

    public Cars(String manufacturer, String model, int price, int mileage, boolean preOwned) {

        this.Manufacturer = manufacturer;

        this.Model = model;

        this.Price = price;

        this.Mileage = mileage;

        this.PreOwned = preOwned;

    }

    //method to make the car go faster.

    public void Accelerate(){

        //code to be executed which will make the car go faster

    }

    //Method to honk horn

    public void honkHorn(){

        // code to be executed which will honk the car horn

    }

}

Python

#creating a class called cars

class Cars:

    # constructor - This is used to create the objects. More on this in the object chapter)

    def __init__(self, manufacturer, model, price, mileage, preOwned):

        self.Manufacturer = manufacturer

        self.Model = model

        self.Price = price

        self.Mileage = mileage

        self.PreOwned = preOwned

       

    #method to make the car go faster.

    def Accelerate(self):

        # code to be executed which will make the car go faster

 

    # method to honk horn

    def honkHorn(self):

        # code to be executed which will honk the car horn

Worked Example

A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.

The class Vehicle stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is added to the current speed each time the vehicle increases its speed.

Class diagram for "Vehicle" showing attributes: ID, MaxSpeed, CurrentSpeed, IncreaseAmount, HorizontalPosition, and methods: Constructor, Get/Set functions.

Write program code to declare the class Vehicle. All attributes must be private.

You only need to declare the class and its constructor. Do not declare any other methods.

Use your programming language’s appropriate constructor.

If you are writing program code in Python, include attribute declarations using comments. [5]

Answer

  • Class header (and close where appropriate) [1 mark]

  • 5 (private) attribute declarations including data types [1 mark]

  • Constructor header (and close where appropriate) taking 3 parameters (min) [1 mark]

  • Assigning ID, MaxSpeed and IncreaseAmount to parameters [1 mark]

  • Assigning CurrentSpeed and HorizontalPosition to 0 [1 mark]

Example program code:

VB.NET

Class Vehicle
  Private ID As String
  Private MaxSpeed As Integer
  Private CurrentSpeed As Integer
  Private IncreaseAmount As Integer
  Private HorizontalPosition As Integer

  Sub New(IDP, MaxSpeedP, IncreaseAmountP)
    ID = IDP
    MaxSpeed = MaxSpeedP
    CurrentSpeed = 0
    IncreaseAmount = IncreaseAmountP
    HorizontalPosition = 0
  End Sub
End Class

Java

class Vehicle{
  private String ID;
  private Integer MaxSpeed;
  private Integer CurrentSpeed;
  private Integer IncreaseAmount;
  private Integer HorizontalPosition;

  public Vehicle(String IDP, Integer MaxSpeedP, Integer IncreaseAmountP {
    ID = IDP;
    MaxSpeed = MaxSpeedP;
    IncreaseAmount = IncreaseAmountP;
    CurrentSpeed = 0;
    HorizontalPosition = 0;
  }}

Python

class Vehicle:
  #self.__ID string
  #self.__MaxSpeed integer
  #self.__CurrentSpeed integer
  #self.__IncreaseAmount integer
  #self.__HorizontalPosition

  def __init__(self, IDP, MaxSpeedP, IncreaseAmountP):
    self.__ID = IDP
    self.__MaxSpeed = MaxSpeedP
    self.__IncreaseAmount = IncreaseAmountP
    self.__CurrentSpeed = 0
    self.__HorizontalPosition = 0

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.