1. Write a Temperature class to represent Celsius and Fahrenheit temperatures. Y
ID: 3695160 • Letter: 1
Question
1. Write a Temperature class to represent Celsius and Fahrenheit temperatures. Your goal is to make this client code work:
Write a Temperature class to represent Celsius and Fahrenheit temperatures. Your goal is to make this client code work:
>>> #constructors
>>> t1 = Temperature()
>>> t1
Temperature(0.0,'C')
>>> t2 = Temperature(100,'f')
>>> t2
Temperature(100.0,'F')
>>> t3 = Temperature('12.5','c')
>>> t3
Temperature(12.5,'C')
>>> #convert
>>> t1.convert()
Temperature(32.0,'F')
>>> t4 = t1.convert()
>>> t4
Temperature(32.0,'F')
>>> #__str__
>>> print(t1)
0.0°C
>>> print(t2)
100.0°F
>>> #==
>>> t1 == t2
False
>>> t4 == t1
True
>>> #raised errors
>>> Temperature('apple','c')
Traceback (most recent call last):
…
ValueError: could not convert string to float: 'apple'
>>> Temperature(21.4,'t')
Traceback (most recent call last):
…
UnitError: Unrecognized temperature unit 't'
Notes:
In addition to the usual __repr__, you should write the method __str__. __str__ is similar to __repr__ in that it returns a str, but is used when a ‘pretty’ version is needed, for example for printing.
Unit should be set to ‘C’ or ‘F’ but ‘c’ and ‘f’ should also be accepted as inputs.
you must create an error class UnitError that subclasses Exception (it doesn’t have to anything additional to that). This error should be raised if the user attempts to set the temperature unit to something other than ‘c’,’f’,’C” or ‘F’
if the user tries to set the degree to something that is not understandable as a float, an exception should be raised (you can get this almost for free)
In the same module/file, implement a class TempConverter that that provides a GUI for converting temperatures. TempConverter should be a usable widget, to do this it MUST subclass Frame (similar to the Calculator). The GUI can be made visible by executing:
>>> TempConverter().pack()
>>>
Explanation / Answer
class Temperature { private double value; private char scale; public Temperature(){ value = 0; scale = 'C'; } public Temperature(double value, char scale) { setTemperature(value, scale); } public double getTempInCelsius() { if(scale == 'C') { return value; } else { return (double)5/9 *(value - 32); } } public double getTempInFahrenheit() { if(scale == 'F') { return value; }else { return (double)(9/5 *(value)) + 32; } } public void setValue(double value){ this.value=value; } public void setScale(char scale){ this.scale=scale; } public void setTemperature(double value, char scale){ this.value = value; if (scale == 'F'){ this.scale = scale; }else { this.scale = 'C'; } } public boolean isGreater(Temperature another){ return (this.getTempInCelsius()> another.getTempInCelsius() ); } public boolean equals(Temperature another){ return (this.getTempInCelsius()== another.getTempInCelsius() ); } public boolean isSmaller(Temperature another){ return (this.getTempInCelsius()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.