Assignment is about a class which describes a jug of water. The jugs have an individual capacity (in litres) and water can be poured in and out. Water in the jug has a temperature which changes as additional water of a given temperature is poured in. For the purpose of this assignment it is assumed that the water's specific heat capacity and its density is temperature-independent. Further, it is assumed that the jug itself has negligible heat capacity and perfect insulation.
The assignment is to define a class Jug which exposes the following methods:
No other methods or attributes are exposed. Thus, if you define methods or variables to help you implement the class Jug, then their names should start with an underscore.
Assume that the water mixes and achieves a homogeneous temperature without delay. If you pour water into a jug without it overflowing, then the resulting temperature is calculated as expected from the details given at the top.
If the jug is full and added water causes it to overflow, then the temperature of water in the jug is derived as follows. When you pour water of temperature Θ
in
Θin into a full jug with water temperature Θ
Θ, as an infinitesimal volume dV
dV is added Θ
Θ changes to
Θ+dΘ=Θ∗capacity+Θ
in
∗dV
in
capacity+dV
in
.
Θ+dΘ=(Θ∗capacity+Θin∗dVin)/(capacity+dVin).
Integrating this equation will yield the final temperatureΘ(V
in
)
Θ(Vin)after a volumeV
in
Vinwith temperatureΘ
in
Θinhas been poured into a full jug, continuously mixing with the water in the jug and overflowing. Please derive Θ(V
in
)
Θ(Vin) analytically and use the resulting expression in your pour_in() method.
import random
class InvalidDepthError(Exception):
def __str__(self):
return "Invalid Depth"
class WaterBody:
# class variables
RHO = 997
G = 9.81
# constructor that takes in volume
def __init__(self,volume):
self.volume=volume
# static method takes in a FLOAT
# HOW TO MAKE SURE IT TAKES IN FLOAT????
@staticmethod
def get_hydrostatic_pressure(depth:float):
# if depth < 0 then raise error of invalid depth
if(depth<0):
raise InvalidDepthError
return WaterBody.RHO * WaterBody.G * depth
def get_water_mass(self):
return WaterBody.RHO * self.volume
# is small , is medium , is large methods to check volume.
@staticmethod
def is_small(volume):
return volume<50 # return is volume<50
@staticmethod
def is_medium(volume):
return 50<=volume<=100 # return is volume between 50 and 100
@staticmethod
def is_large(volume):
return volume>100 # return is volume > 100
@classmethod
def spawn():
return WaterBody(random.randint(1,100)*random.randint(1,100))
# random.randint(1,100) generates number between 0 and 100. We multiply them to obtain, much larger number
if __name__=="__main__":
pool = WaterBody(10)
print(pool.get_hydrostatic_pressure(1))
print(pool.get_water_mass())
try:
pool.get_hydrostatic_pressure(-1)
except Exception as e:
print(e)
Comments
Leave a comment