Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

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

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注