Write a class, Each class will have a constructor and the necessary methods for
ID: 3822455 • Letter: W
Question
Write a class, Each class will have a constructor and the necessary methods for accessing and modifying
variables so that the class functions correctly with the idea the class encapsulates. Each class will store
the minimum variables needed for describing itself (IE: a circle requires a center point and a radius).
You must use the provided point3d class. Credit is awarded on a per-feature bases, features are denoted
under classes, if a feature does not work or does not work correctly it will not be awarded credit. (IE: if
your sphere does not calculate volume correctly you do not get partial credit for -almost- calculating
volume correctly).
(a) The Sphere class
1. surface area
2. enclosed volume
***Here is the point3d class
import math
'''
A simple Point object, it takes in an X, Y, Z coordinate on instantiation
point.location returns a list of [x,y,z]
p.x returns the x value, it is set as a propert so no need for parenthesis
p.y, p.z are the same
p.distFrom is the distance between points formula.
'''
class Point(object):
def __init__(self, x, y, z):
self.p = [x, y, z]
@property
def location(self):
return self.p
@property
def x(self):
return self.p[0]
@x.setter
def x(self, value):
self.p[0] = value
@property
def y(self):
return self.p[1]
@y.setter
def y(self, value):
self.p[1] = value
@property
def z(self):
return self.p[2]
@z.setter
def z(self, value):
self.p[2] = value
def distFrom(self, a):
return math.sqrt( (self.x - a.x )**2 + (self.y - a.y )**2 + ( self.z - a.z )**2 )
Explanation / Answer
CODE:
import math
class Sphere(object):
_radius = None
_PI = 3.14
def __init__(self, radius):
self._radius = radius
def findArea(self):
sa = 4 * self._PI * self._radius * self._radius
return sa
def findVolume(self):
volume = (4 / 3) * self._PI * self._radius * self._radius * self._radius
return volume
radius = float(raw_input('Please Enter the Radius of a Sphere: '))
sp = Sphere(radius)
print "Volume of sphere is: " + str(sp.findArea())
print "Area of sphere is: " + str(sp.findVolume())
OUTPUT:
$ python Sphere.py
Please Enter the Radius of a Sphere: 5
Volume of sphere is: 314.0
Area of sphere is: 392.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.