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()
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller milasandeva. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.28. You're not tied to anything after your purchase.