Λογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 4 χρόνια και 5 μήνες)

141 εμφανίσεις

class Elephant(object):

"""A virtual pet"""

def __init__(self, name, age, weight,
,
trunklength
):

self.name = name

self.age

= age

self.weight

= weight

=

self.trunklength

=
trunklength

We now have a class of Elephants

Elephants starts with a capital letter

Our Elephants all have names and ages.

__init__:

__init__()
is always the constructor in Python

The two __ on both sides of the word init indicate that it is a special name

Constructor is automatically invoked when the an object of this type is created.

Creating an Elephant named Rita:

class Elephant(object):

"""A virtual pet""“

def __init__(self, name, age, weight,
,
trunklength
):

self.name = name

self.age

= age

self.weight

= weight

=

self.trunklength

=
trunklength

# main

elephant1 = Elephant(“Rita”,32,6080,10,96)

print(“The Elephant’s name is “ + elephant1.name + “and her age is “ +
str
(elephant1.age))

elephant2 = Elephant(“Harold”,28,7940,14,100)

print(“The Elephant’s name is “ + elephant2.name + “and his age is “ +
str
(elephant2.age))

Creating an Elephant named Rita:

class Elephant(object):

"""A virtual pet""“

def __init__(self, name, age, weight,
,
trunklength
):

self.name = name

self.age

= age

self.weight

= weight

=

self.trunklength

=
trunklength

# main

elephant1 = Elephant(“Rita”,32,6080,10,96)

print(“The Elephant’s name is “ + elephant1.name + “and her age is “ + elephant1.age)

print(“Her trunk’s volume is" )

???

Creating an Elephant named Rita:

class Elephant(object):

"""A virtual pet"""

def __init__(self, name, age, weight,
,
trunklength
):

self.name = name

self.age

= age

self.weight

= weight

=

self.trunklength

=
trunklength

def
trunkvol
(self):

return(
**2 *
math.pi

*
self.trunklength
)

# main

elephant1 = Elephant("Rita",32,6080,10,96)

print("The Elephant's name is " + elephant1.name + "and her age is " +
str
(elephant1.age))

print("Her trunk's volume is " +
str
(elephant1.trunkvol() ))

Example:

class Rectangle(object):

"""

A rectangle is a shape with width and height [DESCRIPTION]

width
-

number

[PROPERTIES]

height
-

number

"""

def __init__(self, width, height):

#[CONSTRUCTOR]

self.width

= width

self.height

= height

"""

def
Rect_function
(self, ...):

[TEMPLATE]

return
self.width

...

self.height
...

""“

#main

rectangle1 = Rectangle(10, 10)

#[EXAMPLES]

rectangle2 = Rectangle(20, 5)

from cisc106 import *

class Rectangle:

"""

A rectangle is a shape with width and height

[DESCRIPTION]

width
-

number

[PROPERTIES]

height
-

number

"""

def __init__(self, width, height):

#[CONSTRUCTOR]

self.width

= width

self.height

= height

"""

def
Rect_function
(self, ...):

[TEMPLATE]

return
self.width

...

self.height
...

"""

def area(self):

"""

Computes the area of a Rectangle object

aRectangle

-

Rectangle

return
-

number

"""

return
self.width

*
self.height

rectangle1 = Rectangle(10, 10)

#[EXAMPLES]

rectangle2 = Rectangle(20, 5)

assertEqual
(rectangle1.area(), 100)

assertEqual
(rectangle2.area(), 100)

assertEqual
(Rectangle(6,8).area(),48)

from cisc106 import *

class Rectangle:

def __init__(self, width, height):

#[CONSTRUCTOR]

self.width

= width

self.height

= height

def area(self):

return
self.width

*
self.height

def
boxvol
(
self,x
):

return
self.width

*
self.height

* x

mugwump

= Rectangle(10, 10)

#[EXAMPLES]

rectangle2 = Rectangle(20, 5)

assertEqual
(
mugwump.area
(), 100)

assertEqual
(rectangle2.boxvol(2), 200)

assertEqual
(Rectangle(6,8).area(),48)

Class for

a wallet?

class Wallet(object):

def __init__(
self,tw,te,fi,on,ch
):

self.twenties

=
tw

self.tens

=
te

self.fives

=
fi

self.ones

= on

self.change

=
ch

self.amount

=
self.calcamt
()

def
calamt
(self):

return
self.twenties
*20 +
self.tens

* 10 +
self.fives

* 5 +
self.ones

+
self.change
/100.0

def spend(
self,spend_amt
):

if
spend_amt

>= 20:

self.twenties

=
self.twenties

-

math.floor
(
spend_amt
/20)

spend_amt

= spend_amt%20

if
spend_amt

>= 10:

self.tens

=
self.tens

-

math.floor
(
spend_amt
/10)

spend_amt

= spend_amt%10

if
spend_amt

>= 5:

self.fives

=
self.fives

-

math.floor
(
spend_amt
/5)

spend_amt

= spend_amt%5

if
spend_amt

>= 1:

self.ones

=
self.ones

-

math.floor
(
spend_amt
)

spend_amt

= spend_amt%1

self.change

=
self.change

-

spend_amt

* 100

self.amount

=
self.calcamt
()

wallet = Wallet(2,3,5,3,24)

assertEqual
(wallet.amount,98.24)

wallet.spend
(22.12)

assertEqual
(
wallet.amount
, _________)

assertEqual
(
wallet.amount
, 76.12)

wallet.spend
(35.25)

assertEqual
(
wallet.amount
,_________)

assertEqual
(wallet.amount,40.87)

Now write a method
money

to the wallet

(Much simpler function!)

class Wallet:

def __init__(
self,tw,te,fi,on,ch
):

self.twenties

=
tw

self.tens

=
te

self.fives

=
fi

self.ones

= on

self.change

=
ch

self.amount

=
self.calc_amt
()

def
calc_amt
(self):

return
self.twenties
*20 +
self.tens

* 10 +
self.fives

* 5 +
self.ones

+
self.change
/100.0

self,tw,te,fi,on,ch
):

self.twenties

+=
tw

self.tens

=
self.tens

+
te

self.fives

=
self.fives

+
fi

self.ones

=
self.ones

+ on

self.change

=
self.change

+
ch

self.amount

=
self.calc_amt
()

def spend(
self,spend_amt
):

if
spend_amt

>= 20:

self.twenties

=
self.twenties

-

math.floor
(
spend_amt
/20)

spend_amt

= spend_amt%20

if
spend_amt

>= 10:

self.tens

=
self.tens

-

math.floor
(
spend_amt
/10)

spend_amt

= spend_amt%10

if
spend_amt

>= 5:

self.fives

=
self.fives

-

math.floor
(
spend_amt
/5)

spend_amt

= spend_amt%5

if
spend_amt

>= 1:

self.ones

=
self.ones

-

math.floor
(
spend_amt
)

spend_amt

= spend_amt%1

self.change

=
self.change

-

spend_amt

* 100

self.amount

=
self.calc_amt
()

wally

= Wallet(3,2,2,4,1,32)

(1,1,1,1,1)

from
datetime

import *

class
StopWatch
(object):

def __init__(self):

now =
datetime.now
()

self.hr =
now.hour

self.min =
now.minute

self.sec =
now.second

self.micro

=
now.microsecond

def
converttoms
(self):

return
self.micro+self.sec
*1000000+self.min*60*1000000+self.hr*60*60*1000000

def
time_diff
(
self,watch
):

x =
self.converttoms
()

y =
watch.converttoms
()

diff = abs(x
-

y)

return(diff)

time1 =
StopWatch
()

print time1.micro

time2 =
StopWatch
()

print time2.micro

print(time1.time_diff(time2))

Class

Stopwatch?