Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Terminology:
#
lab2a.py
def
kinetic_energy(mass,velocity):
value
=
0.5
*
mass
*
velocity
**
2
return(value)
mass=input("Enter
value
of
mass:
")
mass
=float(mass)
mass=input("Enter
value
of
velocity:")
mass
=float(velocity)
result
=
kinetic_energy(mass,velocity)
print("The
value
of
kinetic
energy
is
",
result)
m1=9.8
v
1=
10.00
print("The
value
of
kinetic
energy
is
",
kinetic_energy(m1,v1)
m2
=
100
v2
=
909.0
print(“The
value
of
kinetic
energy
is
“,
kinetic_energy(m2,v2))
How
do
we
implement
the
above
code
to
become
OOP
program
1. Identify
the
variable
and
function
in
the
program.
(variable
–
attribute,
function
-‐
method)
2. Create
a
class
say
call
Object
3. Add
a
constructor
method
called
__init__
(self,mass,velocity)
and
method
that
you
identify
in
step
1
4. Create
an
instance
of
the
class
–
object
variable
5. Called
the
object
method
Our
OOP
program
will
look
as
follows:
#
lab2b.py
class
Object:
def
__init__(self,mass,velocity):
self.mass
=
mass
self.velocity
=
velocity
def
kinetic_energy(self):
return(0.5*self.mass*self.velocity**2)
Q1.
Add
and
an
additional
method
called
potential_energy
in
class
Object
where
the
formula
is
(You
need
to
modify
the
constructor
to
include
the
two
parameter
gravity
and
height.)
Q2.
Add
an
additional
method
called
total_energy
where
the
formula
is:
self.kinetic_energy()
+
self.potential_energy()
Q4.
Identify
a
formula
and
variable
that
you
can
turn
into
a
class
with
attribute
and
method.
Create
an
instance
of
the
class,
and
then
called
the
object
method.
Save
the
file
as
lab2f.py