Informatika2-2014/Gyakorlat05

A MathWikiből
(Változatok közti eltérés)
69. sor: 69. sor:
 
== Feladatok ==
 
== Feladatok ==
 
=== 1. feladat ===
 
=== 1. feladat ===
 +
Your task is to implement the simple elevator in Python using classes. Before implementation, please
 +
play the demo. It would help you understand the project. We would like you to implement your own
 +
“efficiency” strategy for dealing with customers. The default strategy is the simple “start at the bottom,
 +
go to the top, then go to the bottom”. Can you write a better strategy, one that is more efficient?
 +
 +
Project Description / Specification
 +
 +
1. Create three classes: Building, Elevator, and Customer.
 +
2. Equip the building with an elevator. Ask user to customize the number of floors and the
 +
number of customers.
 +
3. Program should have error checking to make sure the user inputs are valid. For example, if a
 +
user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again.
 +
4. Each customer starts from a random floor, and has a random destination floor.
 +
5. Each customer will use the elevator only once, i.e., when a customer moves out of the elevator,
 +
he/she will never use it again.
 +
6. When all customers have reached their destination floor, the simulation is finished.
 +
7. Part of the grade on this project will be the appropriateness of your classes, methods, and any
 +
functions you use. The quality of the code will now matter as well as the performance. No
 +
skeleton file is provided; you need to come up with this yourself.
 +
8. All classes’ methods require a docstring for a general description of the method.
 +
9. Implement both your own strategy and the default strategy and compare. Your strategy does
 +
not have to be better (your grade will not depend on it) but the comparison is required.
 +
10. Don’t use any global variables.
 +
=== 2. feladat ===
 
* Írj mátrix osztályt az alábbi metófudokkal:
 
* Írj mátrix osztályt az alábbi metófudokkal:
 
<python>
 
<python>

A lap 2014. március 10., 23:24-kori változata

Tartalomjegyzék

Osztályok

  • Az osztályok egységbe zárnak és a felesleges információt elrejtik.
  • Az osztályok metódusokat és változókat tartalmaznak. Metódusokat tekintsük osztályokon belüli függvényeknek.
  • Tekintsünk erre úgy, hogy a kód egy részét (mind tárolókat, mind a függvényeket nézve) összefogjuk.
  • Egy objektum egy osztály konkrét megvalósítása.

Egy metódust az alábbi módon definiálunk pythonban:

class Foo:
   def setx(self, x):
      self.x = x
   def bar(self):
      print self.x
  • Itt a lényeges dolog a self változó, ami mindig az első argumentuma a metódusnak. Ez mindig a konkrét megvalósított objektumot jelöli. Tehát külön értékadás nélkül self.x az osztályon (objektumon) belüli x változó, aminek az értékét állítjuk be a setx függvénnyel.
  • Az alábbi módon hozhatunk létre objektumokat és hívhatjuk meg azok metódusait:
f = Foo()
f.setx(5)
f.bar()
Foo.setx(f,5)
Foo.bar(f)
  • Fontos, hogy Pythonban futási időben is változhatnak az osztályhoz tartozó változók:
del f.x
  • Itt tehát magát a változót töröljük.
  • Minden egyes objektum mögött a háttérben létezik egy szótár, ami tartalmazza az osztály elemeit:
g = Foo()
vars(g)
g.__dict__
  • Konstruktornak hívjuk azt a metódust, ami az objektum létrehozásával együtt hívódik meg:
class Test:
    def __init__(self):
        print "Mostmar elhiszed?"
 
x = Test()
  • A destruktor konzisztens módon az objektum törlésével együtt hívódik meg.
>>> class A:
...   pass
... 
>>> A.v1 = 10
>>> x=A() # létrehozok két példányt
>>> y=A()
>>> x.v1  # példányon belül is elérem az osztály szintű változót
10
>>> x.v1 = 34 # itt viszont létrehozok egy példány szintűt aminek a neve felülírja az osztály szintűt
>>> y.v1
10
>>> x.v1
34
>>> A.v1
10
>>> A.v1 = 12 # itt az osztályszintű változót változtatjuk
>>> x.v1 # mivel felülírtuk a v1-t az x példányban így ezen már nincs hatása
34
>>> y.v1
12
>>> A.v1
12

Feladatok

1. feladat

Your task is to implement the simple elevator in Python using classes. Before implementation, please play the demo. It would help you understand the project. We would like you to implement your own “efficiency” strategy for dealing with customers. The default strategy is the simple “start at the bottom, go to the top, then go to the bottom”. Can you write a better strategy, one that is more efficient?

Project Description / Specification

1. Create three classes: Building, Elevator, and Customer. 2. Equip the building with an elevator. Ask user to customize the number of floors and the number of customers. 3. Program should have error checking to make sure the user inputs are valid. For example, if a user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again. 4. Each customer starts from a random floor, and has a random destination floor. 5. Each customer will use the elevator only once, i.e., when a customer moves out of the elevator, he/she will never use it again. 6. When all customers have reached their destination floor, the simulation is finished. 7. Part of the grade on this project will be the appropriateness of your classes, methods, and any functions you use. The quality of the code will now matter as well as the performance. No skeleton file is provided; you need to come up with this yourself. 8. All classes’ methods require a docstring for a general description of the method. 9. Implement both your own strategy and the default strategy and compare. Your strategy does not have to be better (your grade will not depend on it) but the comparison is required. 10. Don’t use any global variables.

2. feladat

  • Írj mátrix osztályt az alábbi metófudokkal:
  class Matrix:
    def const(self,n,x):
      ...
    def zeros(self,n):
      ...
    def printer(self):
      ...
    def set(self,i,j,x):
      ...
    def get(self,i,j):
      ...
    def hasValue(self,i,j):
      ...
    def getLine(self,i):
      ...
    def pow(self):
      ...
    def product(self,v)
  • próbálj meg hibakezelést beépíteni a függvényekbe (try --- except)
Személyes eszközök