In: Computer Science
The below_freezing function takes a string as its parameter and return True if the temperature specified by the string is below freezing point for water. It returns False if the temperature is at or above the freezing point. The string takes the format of a decimal number followed by a letter where F stands for Fahrenheit, C for Celsius, and K for Kelvin. For example, below_freezing("45.5F") and below_freezing("300K") both return False. below_freezing("-1.2C") and below_freezing("272K") both return True It's critical that you include test case code to show proper testing. PLEASE DO IN PYTHON
#This program will take string as its parameter and return True if the temperature specified by the string is below freezing point for water #Creating a class Temprature class Temprature: def below_freezing(self,tempString): Len_Of_String=len(tempString) if Len_Of_String==0 or Len_Of_String ==1: # will check if correct values are passed print("Please specify correct values") return False tempType=tempString[Len_Of_String-1] temp=float(tempString[0:Len_Of_String-1]) if tempType != 'C' and tempType != 'K' and tempType != 'F': # will check for right input print("temperature can be mentioned in F for Fahrenheit, C for Celsius, and K for Kelvin only") return False # Below will compare temperature is below freezing point or not if tempType=='C': if temp <= 0: return True else: return False if tempType=='F': if temp <= 32: return True else: return False if tempType=='K': if temp <= 273.2: return True else: return False if __name__=="__main__": TempObj=Temprature() # creating an object of class Temprature #Below are the some test cases TempObj.below_freezing("") TempObj.below_freezing("1D") TempObj.below_freezing("1") #using print to print true and false values print(TempObj.below_freezing("45.5C")) print(TempObj.below_freezing("-1.2C")) print(TempObj.below_freezing("300K")) print(TempObj.below_freezing("272K")) print(TempObj.below_freezing("45.5F")) print(TempObj.below_freezing("5.5F")) Output of test cases:
Please specify correct values
temperature can be mentioned in F for Fahrenheit, C for Celsius,
and K for Kelvin only
Please specify correct values
False
True
False
True
False
True