100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Computer science Cambridge chapter 20

Rating
-
Sold
1
Pages
11
Uploaded on
02-02-2022
Written in
2021/2022

It contains notes on further programming including files and object oriented coding in python

Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Study Level
Publisher
Subject
Course

Document information

Uploaded on
February 2, 2022
Number of pages
11
Written in
2021/2022
Type
Summary

Subjects

Content preview

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()
$8.14
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
milasandeva

Get to know the seller

Seller avatar
milasandeva Private British School
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
3 year
Number of followers
1
Documents
2
Last sold
3 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions