• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 2 fuera de 11 páginas
Resumen

Summary Computer science Cambridge chapter 20

Document preview thumbnail
Vista previa 2 fuera de 11 páginas

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

Vista previa del contenido

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()

Información del documento

Nivel de Estudio
Tema
Subido en
2 de febrero de 2022
Número de páginas
11
Escrito en
2021/2022
Tipo
Resumen
$8.24

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
1
Seguidores
1
Artículos
2
Última venta
4 año hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes