ΕΠΑΛ Python – Ασκήσεις στον Αντικειμενοστραφή Προγραμματισμό (Απαντήσεις)

Ασκ-01

class plane:
   def __init__(self, make, model, year, seats):
      self.make = make
      self.model = model
      self.year = year
      self.seats = seats
      self.speed = 290
      self.time = “00:00 πμ”

   def Identifiction(self):
      print ‘I am a plane’,self.make,self.model

   def SpeedUp(self,value):
      self.speed = self.speed + value
      print “I changed my speed to”, self.speed, “km/h”

   def TakeOff(self, time):

      self.time = time
      print “I took off … at”,self.time

plane1 = plane(“BOEING”, “757”, “2009”, 480)
plane2 = plane(“AEROFLOT”, “AR320”, “2012”, 320)

plane1.Identifiction()
plane1.TakeOff(“04:35 μμ”)
plane1.CurrentSpeed()
print
plane2.Identifiction()
plane2.SpeedUp(-20)

Ασκ-02

class fraction:
   def __init__(self, numer, denom):
      self.numer = numer
      self.denom = denom

   def add(self, other):
      new_numer = self.numer*other.denom + self.denom*other.numer
      new_denom = self.denom*other.denom
      return fraction(new_numer,new_denom)

   def multiply(self, other):
      new_numer = self.numer*other.numer
      new_denom = self.denom*other.denom
      return fraction(new_numer,new_denom)

fraction1 = fraction(5,4)
fraction2 = fraction(2,3)

fraction_add = fraction1.add(fraction2)
print ‘(‘,fraction1.numer,’/’,fraction1.denom,’)+(‘,fraction2.numer,’/’,fraction2.denom,’)=’,fraction_add.numer,’/’,fraction_add.denom

fraction_multi = fraction1.multiply(fraction2)
print ‘(‘,fraction1.numer,’/’,fraction1.denom,’)*(‘,fraction2.numer,’/’,fraction2.denom,’)=’,fraction_multi.numer,’/’,fraction_multi.denom

Ασκ-03

class Student:
   def __init__(self, am, name, grades):
      self.am = am
      self.name = name
      self.grades = grades[:4]

   def ShowStudentData(self):
      print  “Student_ID:”, self.am
      print  “Student name:”, self.name
      print  “Student grades:”, self.grades

   def CalculateGPA(self):
      s=0
      for item in self.grades:
         s = s + item
      gpa=s / float(len(self.grades))
      return  gpa

babbis = Student(5199, ‘babbis boulis’, [15,17,18,19])
babbis.ShowStudentData()
print  ‘Grade Point Average:’, babbis.CalculateGPA()

 

 

Ασκ-03

  1. onoma
  2. def perasa_mathima(self,p):
        self.credits=self.credits+p
  3. foititis1 = Foititis(103,”Κωνσταντίνου”,0)
  4. foititis1.perasa_mathima(5)

 

Ασκ-04

α) Κατασκευαστής είναι η μέθοδος __init__

β),γ)
class Kinito:
def __init__(self,marka,model,cpu_cores,cam_resolution): #—-ερώτημα β
self.marka=marka
    self.model=model
    self.cpu_cores=cpu_cores                       #—-ερώτημα β
    self.cam_resolution=cam_resolution             #—-ερώτημα β
  def fortizi(self):
    print “Το κινητό φορτίζει”

phone1=Kinito(“orange”,“S3”,4,10)                    #—-ερώτημα γ

 

error: το περιεχόμενο προστατεύεται !!