Answer to Question #167206 in Python for Jordan

Question #167206

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:

  • Jug creation: This takes a single parameter (aside from the standard self) which is the capacity (maximum volume of water it can accommodate) and creates an empty jug of that capacity.
  • Method pour_out(volume, into_jug=None): Water is poured out of the jug. volume is the volume in litres that is poured out. Alternatively, the string 'all' can be provided as volume, in which case the full volume of water currently in the jug is poured out. If the named parameter into_jug is assigned then the water is poured into the assigned jug. If into_jug isn't assigned, then the water is poured down the drain.
  • Method pour_in(volume, temperature): Water of the given volume and temperature is poured into the jug. Any excess water beyond the jug's capacity goes into the jug, mixes and causes water to overflow.
  • Method temperature(): Returns the temperature of the water in the jug.
  • Method water_volume(): Returns the volume of the water in the jug.
  • Method capacity(): Returns the capacity of the jug.

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.


1
Expert's answer
2021-02-27T11:54:56-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS