a) Write down a class definition for a class named City. Each city object must h
ID: 3809950 • Letter: A
Question
a) Write down a class definition for a class named City. Each city object must have four attributes: name, x, y, and pop. The first two should be thought of as the (x, y) coordinates of the city (imagining the city as residing in the xy-plane). The third attribute is meant to represent the city's current population. The City class also needs two methods. First, you must write a FuturePop method which predicts the future population of the city using a basic exponential growth model. Given two inputs r (the rate of growth) and t (the time elapsed from present, in years), the FuturePop method should return the result of (current population) 2.7^rt Second, you must also write a DistanceTo method which takes another city (call it other) as its input and returns the distance from the first city to the second using the Euclidean distance formula: distance from first city to second city = Squareroot (x_1 - x_2)^2 + (y_1 - y_2)^2.Explanation / Answer
import math
from decimal import Decimal
class City(object):
def __init__(self,x,y,pop):
self.x=x
self.y=y
self.pop=pop
def FuturePop(self,r,t):
return "{:.2E}".format(Decimal(self.pop*2*7)**(r*t))
def DistanceTo(self,other):
return math.sqrt( ((self.x-other.x)**2) +((self.y-other.y)**2) )
c=City(2,3,100)
d=City(4,4,200)
print(c.FuturePop(10,10))
print(c.DistanceTo(d))
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.