Make a class called F that implements the function
$$f(x; a, w) = \exp(−ax)\sin(wx).$$A value(x) method computes values of f, while a and w are class attributes. Test the class with the following main program:
from math import *
f = F(a=1.0, w=0.1)
print(f.value(x=pi))
f.a = 2
print(f.value(pi))
# Sample solution 1
from math import *
class F:
def __init__(self, a, w):
self.a = a
self.w = w
def value(self, x):
f = exp(-self.a*x)*sin(self.w*x)
return f
# Testing code below
f = F(a=1.0, w=0.1)
print(f.value(x=pi))
f.a = 2
print(f.value(x=pi))
0.01335383513703555 0.0005770715401197441
Make a class called Simple with one attribute i, one method double which replaces the value of i by i+i, and a constructor that initializes the attribute. Try out the following code for testing the class:
s1 = Simple(4)
for i in range(4):
s1.double()
print(s1.i)
s2 = Simple('Hello')
s2.double(); s2.double()
print(s2.i)
s2.i = 100
print(s2.i)
Before you run this code, convince yourself what the output of the print statements will be.
# Sample solution 2
class Simple:
def __init__(self, i):
self.i = i
def double(self):
self.i += self.i
# Testing code below
s1 = Simple(4)
for i in range(4):
s1.double()
print(s1.i)
s2 = Simple('Hello')
s2.double(); s2.double()
print(s2.i)
s2.i = 100
print(s2.i)
64 HelloHelloHelloHello 100
Add an attribute called transactions to the Account class given below. The new attribute counts the number of transactions done in the deposit and withdraw methods. The total number of transactions should be printed in the dump method. Write a simple test program to demonstrate that transaction gets the right value after some calls to deposit and withdraw.
class Account:
def __init__(self, name, account_number, initial_amount):
self.name = name
self.no = account_number
self.balance = initial_amount
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def dump(self):
s = "%s, %s, balance: %s" % \
(self.name, self.no, self.balance)
print(s)
# Sample solution 3
class Account:
def __init__(self, name, account_number, initial_amount):
self.name = name
self.no = account_number
self.balance = initial_amount
self.number_of_transactions = 0 # A counter to keep track of the number of transactions performed. This is initialised to zero when the account is set-up.
def deposit(self, amount):
self.balance += amount
self.number_of_transactions += 1
def withdraw(self, amount):
self.balance -= amount
self.number_of_transactions += 1
def dump(self):
s = '%s, %s, balance: %s, number of transactions performed: %s' % \
(self.name, self.no, self.balance, self.number_of_transactions)
print(s)
account = Account("Test", 1, 100)
account.deposit(50)
account.dump()
account.withdraw(20)
account.dump()
Test, 1, balance: 150, number of transactions performed: 1 Test, 1, balance: 130, number of transactions performed: 2
Make a class called Line whose constructor takes two points $p_1$ and $p_2$ (2-tuples or 2-lists) as input. The line goes through these two points (see function line defined below for the relevant formula of the line). A value(x) method computes a value on the line at the point x.
def line(x0, y0, x1, y1):
"""
Compute the coefficients a and b in the mathematical
expression for a straight line y = a*x + b that goes
through two points (x0, y0) and (x1, y1).
x0, y0: a point on the line (floats).
x1, y1: another point on the line (floats).
return: coefficients a, b (floats) for the line (y=a*x+b).
"""
a = (y1 - y0)/float(x1 - x0)
b = y0 - a*x0
return a, b
# Sample solution 4
class Line:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def value(self, x):
# This implements the function (defined above) in the class
a = (self.p2[1] - self.p1[1])/float(self.p2[0] - self.p1[0])
b = self.p1[1] - a*self.p1[0]
y = a*x + b
return y
l = Line([0, 0], [1, 0.5])
print(l.value(0.5))
0.25
Consider a quadratic function $f(x; a, b, c) = ax^2 + bx + c$. Make a class called Quadratic for representing f, where a, b, and c are attributes, and the methods are:
interval [L, R], 3. roots for computing the two roots.
# Sample solution 5
from math import *
from numpy import *
class Quadratic:
def __init__(self, a, b, c):
# Initialise the class' attributes
self.a = a
self.b = b
self.c = c
def value(self, x):
f = self.a*x**2 + self.b*x + self.c # Remember to use .self to access the class attributes
return f
def table(self, L, R, n):
xarray = linspace(L, R, n)
print("x\t f(x)") # The "\t" is a tab character.
for x in xarray:
print("%.2f\t %.2f" % (x, self.value(x)))
def roots(self):
# Using the quadratic formula to compute the roots
x1 = (-self.b + sqrt(self.b**2 - 4*self.a*self.c))/(2.0*self.a)
x2 = (-self.b - sqrt(self.b**2 - 4*self.a*self.c))/(2.0*self.a)
return x1, x2
q = Quadratic(a=1, b=5, c=1)
print(q.value(1))
print(q.roots())
q.table(4, 5, 10)
7 (-0.20871215252208009, -4.7912878474779195) x f(x) 4.00 37.00 4.11 38.46 4.22 39.94 4.33 41.44 4.44 42.98 4.56 44.53 4.67 46.11 4.78 47.72 4.89 49.35 5.00 51.00
Make a class that can only do one thing: print a writes "Hello, World!" to the screen, where a is an instance of the class.
# Sample solution 6
class Hello:
def __init__(self):
self.message = "Hello, World!"
def __str__(self):
return self.message
a = Hello()
print(a)
Hello, World!
Modify the class from the first exercise such that the following code works:
f = F2(1.0, 0.1)
print(f(pi))
f.a = 2
print(f(pi))
print(f)```
# Sample solution 7
from math import *
class F2:
def __init__(self, a, w):
self.a = a
self.w = w
def __call__(self, x):
# Evaluate the function f for a particular x value
f = exp(-self.a*x)*sin(self.w*x)
return f
def __str__(self):
return "f(x; a, w) = exp(-a*x)*sin(w*x)"
# Testing code below
f = F2(1.0, 0.1)
print(f(pi))
f.a = 2
print(f(pi))
print(f)
0.01335383513703555 0.0005770715401197441 f(x; a, w) = exp(-a*x)*sin(w*x)