top of page

Python Programming Help | Practice Sample Coding Paper | Realcode4you

Requirement Task

1. Create a Static utility class called Temperature. Temperature should contain two static methods. Once that accepts temperature as Celsius, and returns it as Fahrenheit. Another that accepts temperature as Fahrenheit and returns it as Celsius.

a. Formula: Celsius = (5/9) * (Fahrenheit - 32)

b. Formula: Fahrenheit = (9/5) * Celsius + 32


2. Create a TemperatureSensor class to represent the details of a temperate taken at a particular time by a home sensor device. A temperature sensor object contains the following fields:

a. The current temperature reading

b. The current temperature type – can be Celsius or Fahrenheit. c. Date

d. Time – can use a double for this (and assume a 24 hour clock)

e. Location of reading – can be zone 1, zone 2, zone 3.


3. The TemperatureSensor class must contain a static variable that records the number of sensors set up and running in your network. The initialiser must increase this number by one each time it is called.


4. Implement an initializer for the class that takes as parameters the five fields listed above. It should initialise the fields in the object if the parameter data is valid i.e. the date should not be in the past, the temperature temp and the zone must be a valid one. If any of the data is invalid then throw an exception.


5. Allow for default parameters that sets the type to Celsius as a default, the zone to Zone 1 and the date to today. Other data should be simply initialised to empty values.


6. Create a print() definition that displays an appropriately formatted string containing full information about the temperature reading.


7. Implement a definition which returns the current temperature reading.


8. Implement a definition which returns the current temperature reading as Celsius. If the current temperature type is set to Fahrenheit, it must convert it first (using the utility static method)


9. Implement a Test class which:

a. Creates 4 Temperature objects and stores them in an array of temperatures – these must contain a mixture of Celsius and Fahrenheit readings, and be for different zones.

b. Iterates across the array calling print() on each temperature object in turn printing the result to the console.

c. Calculates and displays the average temperature reading in Celsius based on the temperatures in the array.

d. Displays the total number of sensors


Code Implementation


Temparature_Sensor.py



import datetime
"""import pytz"""
#static Utility class temperature
class Temperature:
    @staticmethod
    def convert_to_celsius(f):
         return (f - 32) * (5 / 9)
        
    @staticmethod
    def convert_to_fahrenheit(c):
        return (c * (9 / 5)) + 32
        
class TemperatureSensor:
    number_of_sensors = 0

    def __init__(self, current_temperature = "",temperature_type = "Celsius",date = datetime.datetime.now(),time="",location = "Zone 1"):
       

        self.current_temperature = current_temperature
        self.temperature_type = temperature_type
        
        # date in YYYY-MM-DD
        current_date = datetime.datetime.now()
        if date.year < current_date.year and date.month < current_date.month  and date.day < current_date.day :
            raise Exception('the date should not be in the past')
        
        self.date = date
        
        hour = int(time.split(':')[0])
        minutes = int(time.split(':')[1]) 
        if hour < 0 or hour > 23 or minutes < 0 or minutes > 59:
            raise Exception('Time should be in 0 to 23') 
        self.time = time
        
        if location in ['Zone 1', 'Zone 2', 'Zone 3']:
            self.location = location
        else:
             raise Exception('Enter valid zone - can be zone 1, zone 2, zone 3')

        TemperatureSensor.number_of_sensors = TemperatureSensor.number_of_sensors + 1
            
        
        
    #method to print current temperature    
    
    def PrintCurrentTemp(self):
        print("The current temperature reading :",self.current_temperature)
        print("The current temperature type:",self.temperature_type)
        print("Date (YYYY-MM-DD):",self.date.strftime("%Y-%m-%d"))
        print("Time:",self.time)
        print("Zone :",self.location)
        
    def currentReading(self):
        return self.current_temperature
    
    def FtoCelsius(self):
        if self.temperature_type == "Fahrenheit":
            return Temperature.convert_to_celsius(self.current_temperature)
        else:
            return self.current_temperature
        


#date in (YYYY-MM-DD)
temp1 = TemperatureSensor(current_temperature = 100,temperature_type = "Fahrenheit" ,date = datetime.datetime.strptime('2019-04-29','%Y-%m-%d'),time = '01:00',location = 'Zone 2')
temp2 = TemperatureSensor(current_temperature = 98,date = datetime.datetime.strptime('2019-04-29','%Y-%m-%d'),time = '04:00',location = 'Zone 3')
temp3 = TemperatureSensor(current_temperature = 54,time = '04:00',location = 'Zone 3')
temp4 = TemperatureSensor(current_temperature = 78,time = '04:00')

array  = [temp1,temp2,temp3,temp4]
avg =  0
for i in range(len(array)):
    array[i].PrintCurrentTemp()
    avg = avg + array[i].FtoCelsius()

print("Averange temp :",avg//len(array))
print("The total number of sensors: ",temp4.number_of_sensors)
    

a=5
print(a)


1 Comment


Nikhil Ji
Nikhil Ji
Nov 04, 2023

"Statistics Assignment Help" is an invaluable resource for students and professionals seeking assistance with their statistical assignments. The service provides expert guidance and support in understanding complex statistical concepts, analyzing data, and completing assignments accurately. With a team of experienced statisticians and educators, it ensures that students receive the help they need to excel in their studies. Whether you're struggling with hypothesis testing, regression analysis, or any other statistical topic, "Statistics Assignment Help" is a reliable and efficient solution to boost your understanding and academic performance.


Like
bottom of page