OBJECT-ORIENTED PROGRAMMING
A programming paradigm that uses self contained objects, which contain programming
statements methods and data known as attributes or properties.
OOP is often used to solve complex problems as it enables programmers to work and describe
real life things.
EVERYTHING IS AN OBJECT
Class - is a template that defines the methods and data of a certain type of object. E.g.
Attributes - data items
Methods - procedures and functions defined as a part of a
class
Encapsulation - is a concept that data and methods are
placed together as a single unit (class). The data can be
accessed via the methods provided by the class.
Object - is an instance of a class. It needs to be declared using a
class type that has already been defined.
CLASS DEFINITION
each class needs a constructor. When an object is created we need to set initial values to these
objects. The following example is in python
def_ _ init _ _ (self, name, surname):
self.firstName = name
self.secondName = surname
employ1 = employe(“Mary”,”Smith”)
print(employee1.firstName)
print(employee1.secondName)
_ _init_ _(): constructor, built in function always called when an object is created. It assigns values
to object properties or perform other operations necessary when object is created
self : is a parameter reference to the current instance of the class and it is used to access the
variables that belong to the class (a different name apart from self can be used)
class employee
def_ _ init _ _ (self, name, surname):
self.firstName = name
self.secondName = surname
def showdetails(self):
print(employee1.firstName)
print(employee1.secondName)
, #main program
#prompt the user to enter name, surname
#create two objects
#employee1 : Mary Jones
#employee2 : John Smith
#use showdetails to display name and surname of both
OOP
Exercise 1: The vending machine is part of a program that is written using object-oriented
programming (OOP). The vending machine makes use of two classes that are described in the
following tables. Write program code to declare both classes: FoodItem and vendingMachine.
a. Four objects of type foodItem are declared with the identifiers: chocolate, sweets, sandwich,
apple. Write program code to declare an instance of vendingMachine with the identifier machineOne
and the objects: chocolate (name:Choco , code:QWER cost:12,00) , sweets (name: Candy , code:QBGH
cost:20,00), sandwich (name:Ham & Cheese , code:QBGH cost:17,00) , fruit (name:Apple , code:QERT
cost:16,00)
b. The method checkValid() takes the food item code as a parameter. It checks the code against
each element in items and returns:
• –1 if the code is not valid
• -2 if the code is valid, but the moneyIn is less than the cost of the item
• the index of the item, if the code is valid and the moneyIn is greater than or equal to the cost of
the item. Write program code to implement checkValid()
A programming paradigm that uses self contained objects, which contain programming
statements methods and data known as attributes or properties.
OOP is often used to solve complex problems as it enables programmers to work and describe
real life things.
EVERYTHING IS AN OBJECT
Class - is a template that defines the methods and data of a certain type of object. E.g.
Attributes - data items
Methods - procedures and functions defined as a part of a
class
Encapsulation - is a concept that data and methods are
placed together as a single unit (class). The data can be
accessed via the methods provided by the class.
Object - is an instance of a class. It needs to be declared using a
class type that has already been defined.
CLASS DEFINITION
each class needs a constructor. When an object is created we need to set initial values to these
objects. The following example is in python
def_ _ init _ _ (self, name, surname):
self.firstName = name
self.secondName = surname
employ1 = employe(“Mary”,”Smith”)
print(employee1.firstName)
print(employee1.secondName)
_ _init_ _(): constructor, built in function always called when an object is created. It assigns values
to object properties or perform other operations necessary when object is created
self : is a parameter reference to the current instance of the class and it is used to access the
variables that belong to the class (a different name apart from self can be used)
class employee
def_ _ init _ _ (self, name, surname):
self.firstName = name
self.secondName = surname
def showdetails(self):
print(employee1.firstName)
print(employee1.secondName)
, #main program
#prompt the user to enter name, surname
#create two objects
#employee1 : Mary Jones
#employee2 : John Smith
#use showdetails to display name and surname of both
OOP
Exercise 1: The vending machine is part of a program that is written using object-oriented
programming (OOP). The vending machine makes use of two classes that are described in the
following tables. Write program code to declare both classes: FoodItem and vendingMachine.
a. Four objects of type foodItem are declared with the identifiers: chocolate, sweets, sandwich,
apple. Write program code to declare an instance of vendingMachine with the identifier machineOne
and the objects: chocolate (name:Choco , code:QWER cost:12,00) , sweets (name: Candy , code:QBGH
cost:20,00), sandwich (name:Ham & Cheese , code:QBGH cost:17,00) , fruit (name:Apple , code:QERT
cost:16,00)
b. The method checkValid() takes the food item code as a parameter. It checks the code against
each element in items and returns:
• –1 if the code is not valid
• -2 if the code is valid, but the moneyIn is less than the cost of the item
• the index of the item, if the code is valid and the moneyIn is greater than or equal to the cost of
the item. Write program code to implement checkValid()