PS Classes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PS - Classes

Note: Problems 1,2, and 3 use the class “Point”. I will send the code for this class on the group because
copy pasting it directly from this pdf will cause a lot of indentation errors, and you can copy paste it onto
the beginning of each of your answers

class Point(object):
def __init__(self,x=0,y=0):
assert (type(x)==int or type(x)==float) and (type(y)==int or type(y)==float), "Bad input!"
self.x = x
self.y = y
def __add__(self,other):
assert type(other)==Point, "Bad Input!"
return Point(self.x+other.x,self.y+other.y)
def __str__(self):
return "("+str(self.x)+","+str(self.y)+")"

1
Problem 1

a) Triangle (45 points). In this problem, you will design an abstract data type for triangles based on the
planar Point class from Solving Session 10. The class Point is included in testPrograms.py for convenience.
A planar triangle is represented by a its three corner points p,q, and r. Using the class Point, design the
class Triangle which defines a triangle as an Abstract Data Type. Include the data attributes p,q,r, each
of type Point, and the method attributes:

• init , which (in addition to self) takes values of p,q, and r as input arguments. This method should
return the exception ”Bad Input!” if one of its three input arguments is not of type Point
• str , which casts the point into a string of the form "(p,q,r)", as shown in the below test program.
• perimeter, which returns the perimeter of the triangle
• centerOfGravity, which returns the center of gravity of the triangle, i.e, the point ( ,

• Overloaded binary operator add to translate a triangle by a given point v: if T is a triangle, then T
+ v should be a triangle whose corner points are the corner points p,q, and r of T translated by v,
i.e., p + v,q + v, and r + v. See the below test program. This method should return the exception
”Bad Input!” if the type of v is not Point.

• isEquilateral, which checks whether or not the triangle is equilateral (i.e., all its edges are of the
same length). This method should return True if the triangle is equilateral and False otherwise.
Note: The length of each edge is of type float. Do not use the == operator to check if two floats x
and y are equal; check instead if , e.g., for .

b) Equilateral triangle (15 points). Design the class EquilateralTriangle as a subclass of Triangle:

– Do not include new data attributes –


Override the special method:
• init , which takes values of p,q, and r as input arguments and invokes Triangle. init . This
method should return the exception ”Bad Input!” if the input arguments do not form an
equilateral triangle.
– Include the new method:
• edgeLength, which returns the edge length of the equilateral triangle.

2
Test Program:
p = Point(0,0)
q = Point(1,0)
r = Point(0.5,3**0.5/2)
T1 = Triangle(p,q,r)
T2 = Triangle(p,q,Point(0.5,1))
print("T1:",T1)
print("T2:",T2)
print("Perimeter of T1:",T1.perimeter())
print("Center of gravity of T1:",T1.centerOfGravity())
print("T1 equilateral:",T1.isEquilateral())
print("T2 equilateral:",T2.isEquilateral())
print("Translation of T2 by (10,10):",T2+Point(10,10))
T3 = EquilateralTriangle(p, q, r)
print("T3:",T3)
print("Edge length of T3:", T3.edgeLenght())
print("Translation of T3 by (5,5):",T3+Point(5,5))

Output:
T1: ((0,0),(1,0),(0.5,0.8660254037844386))
T2: ((0,0),(1,0),(0.5,1))

Perimeter of T1: 3.0


Center of gravity of T1: (0.5,0.28867513459481287)

T1 equilateral: True
T2 equilateral: False

Translation of T2 by (10,10): ((10,10),(11,10),(10.5,11))

T3: ((0,0),(1,0),(0.5,0.8660254037844386))
Edge length of T3: 1.0
Translation of T3 by (5,5): ((5,5),(6,5),(5.5,5.866025403784438))

Problem 2
USE THE SAME POINT “CLASS” AS IN THE EXERCISE ABOVE
In this problem, you will design an abstract data type for rectangles based on the planar Point class from
Solving Session 10.
A rectangle R is represented by two points p and q, where p is the lower left corner R and q is the upper
right corner of R. Using the class Point, design the class Rectangle which defines a rectangle as an Abstract
Data Type (ADT). Include the data attributes p and q and the method attributes:
__init__ takes p and q as input arguments. This method should return the exception “Bad Input!” if the
following condition does not hold: type of p and q is Point, p.x ≤ q.x, and p.y ≤ q.y.

Special method __str__, which casts the rectangle into a string of the form "(p=(a,b),q=(c,d))".

height, which returns the height

3
width, which returns the width

perimeter, which returns the perimeter

area, which returns the area

contains, which checks if a given point pt is inside the rectangle. This method should return the
exception “Bad Input!” if the type of pt is not Point
Test program:

#Include a statement to initialize a rectangle R whose lower left corner is (1,2) and upper right corner is
(3.2,4)
print(R)

print(R.width())
print(R.height())

print(R.area())
print(R.perimeter())
print(R.contains(Point(1.5,3)))

print(R.contains(Point(10.5,3)))

Output:

(p=(1,2),q=(3.2,4))
2.2

4.4
8.4

True
False

Problem 3
USE POINT CLASS AGAIN
In this problem, you will design an abstract data types for circles based on planar Point class from
Programming Assignment 10.
The class Point is included in testPrograms.py for convenience.
A circle C is represented by a Pointcenter and a nonnegative number radius. Using the class Point, design the
class Circle which defines a circle as an Abstract Data Type (ADT). Include the data attributes center and radius
and the method attributes:

• __init__, which takes center and radius as input arguments, with default values center=Point(0,0) and radius=1.
This method should return the exception ”Bad Input!” center is not of type Point, or radius is not of type int
or float, or radius is not nonnegative.

• str__, which casts the point into a string of the form "((center.x,center.y),radius)", as shown in the below test
program.

4
• diameter, which returns the diameter (i.e., 2 ×radius)

• perimeter, which returns the perimeter (i.e., 2π ×radius)

• area, which returns the area (i.e., π ×radius2)


• contains, which checks if a given point pt is inside the circle. This method should return the exception ”Bad
Input!” if the type of pt is not Point

• intersect, which checks if the disk associated with the circle (i.e., the circle and its interior) has a nonempty
intersection with the disk associated with another given circle other. This method should return the
exception ”Bad Input!” if the type of other is not Circle.

Test program:
C1 = Circle()

C2 = Circle(Point(1,0.5),0.75)

C3 = Circle(Point(10,5),2)

print(C1)

print(C2)

print(C3)

print(C1.diameter())

print(C2.perimeter())

print(C3.area())

print(C1.contains(Point(0.5,0.5)))

print(C1.contains(Point(5,5)))

print(C1.intersect(C2))

print(C1.intersect(C3))

Output:
((0,0),1)
((1,0.5),0.75)
((10,5),2)
2
4.71238898038469
12.566370614359172
True
False
True
False

5
Problem 4
a) Netflux user class (40 points). In this problem, you will design an abstract data type for a Netflux user called
NetUser.
A NetUser has three data attributes: a string name, a string email, and a dictionary movies. The dictionary movies has
movie titles (strings) as keys, and integer ratings as values. Moreover, NetUser has the following method attributes:

• init__, which takes name and email as input arguments. This method should return the exception “Bad Input!”
if either name or email is not a string. It should also initialize the movies dictionary to empty dictionary.
• addMovie, which takes a string title (representing a movie title) as input and adds it to the dictionary movies
with a corresponding value of 0.
• rateMovie, which takes a string title (representing a movie title) and a positive integer score (representing the
movie rating) as input. It should raise an assertion error if title is not in movies or if score is not a positive
integer. Otherwise, it should update the dictionary movies so that the value corresponding to title is changed
to score.
• commonMovies, which takes as input a NetUser other. It should return a list of movies that are common to both
users. If the type of other is not NetUser, it should raise an assertion error.
• str , which casts NetUser as a string in the following form:
Name: ...
E-mail: ...
Number of movies in dictionary: ...
It’s movie time because EECE 230 (which is fun and easy) is over!
Any correct solution is worth full credit.
Test program:
IB=NetUser("IbnBalluta The Explorer","[email protected]")
IB.addMovie("The Mask of Zorro")

IB.rateMovie("The Mask of Zorro",10)

IB.addMovie("The 100 year-old man who climbed out of the window and disappeared")
IB.rateMovie("The 100 year-old man who climbed out of the window and disappeared",9)
IB.addMovie("The Matrix")

IB.addMovie("Amelie")

MS=NetUser("Majhoul Al-Shoubasi","[email protected]")
MS.addMovie("The Matrix")
MS.rateMovie("The Matrix",10)
MS.addMovie("The Godfather")

MS.addMovie("Howl’s Moving Castle")

print(IB,"\n")
print(MS,"\n")
print(MS.commonMovies(IB))

6
Output:

Name: IbnBalluta The Explorer

E-mail: [email protected]
Number of movies in dictionary: 4
It’s movie time because EECE 230 (which is fun and easy) is over!

Name: Majhoul Al-Shoubasi

E-mail: [email protected]
Number of movies in dictionary: 3

It’s movie time because EECE 230 (which is fun and easy) is over!
[’The Matrix’]

Problem 5

a) ThreeDPoint class (40 points).


In this part, you will design abstract data types for 3-dimensional points. A 3-dimensional point p
is represented by three real numbers x,y, and z: x-coordinate p.x, y-coordinate p.y, and z-coordinate
p.z. Design the class threeDPoint which defines 3-dimensional point as an Abstract Data Type. Include
the data attributes x,y, and z and the method attributes:

• init , which takes x, y, and z as input arguments with zero default values. This method should
return the exception "Incorrect type!" if the types of x, y, and z are not int or float.
• Special method str__ , which casts the point into a string of the form "(x, y, z)"

• Overloaded binary operator add__ (self,other) to perform the pointwise addition of two
3dimensional points:
(x,y,z) + (x0,y0,z0) = (x + x0,y + y0,z + z0).

• Overloaded binary operator __mul (self,c) to perform the scalar multiplication of a 3-


dimensional point p and a scalar c of type int or float: p∗c = q, where q is the 3-dimensional point
given by q.x = p.x ∗ c, q.y = p.y ∗ c, and q.z = p.z ∗ c. This method should return the exception
"Incorrect type!" if the type of c is not int or float.

Include also the following function (not a method of threeDPoint):

• distance(p,q), which given two 3-dimensional points p and q, returns their Euclidean dis-
tance: sqrt((p.x − q.x)2 + (p.y − q.y)2 + (p.z − q.z)2) . This method should return the exception "Incorrect
type!" if the types of p or q are not threeDPoint.

7
Test program:

A = threeDPoint()

B = threeDPoint(1,3.1,2)
C = threeDPoint(2.1,12,5.4)
print(A)

print(B)

print(C)
print(B+C)

print(B*1.1)
print(B*3)

print(distance(B,C))
Output:

(0, 0, 0)

(1, 3.1, 2)

(2.1, 12, 5.4)


(3.1, 15.1, 7.4)
(1.1, 3.4100000000000006, 2.2)
(3, 9.3, 6)

9.590620417887468

Test program:
L = [threeDPoint(7,22,21),threeDPoint(1,3.1,2),threeDPoint(10,13.1,22),\

threeDPoint(0.9,4,3),threeDPoint(11,5.7,-13),threeDPoint(10,13,122)]
(p,q)=closestPair(L)
print("Closest pair: ",p, "and ", q)
Output:
Closest pair: (1, 3.1, 2) and (0.9, 4, 3)

You might also like