ermostat and set it to the desired temperature within a pre-specified range. If the user tries set the temperature outside this range it should throw a TemperatureTooHigh or TemperatureTooLow exception. Use inheritance to create an exception superclass TemperatureOutofRange and subclasses TemperatureTooHigh and TemperatureTooLow. Sample Tester code: Thermostat t = new Thermostat(0, 100); t. setTemp(50); // Should be OK. t.setTemp(150); // Should throw TemperatureTooHigh exception. t.setTemp(-50); // Should throw TemperatureToolLow exception. Write a Tester class to demonstrate throwing and catching of exceptions. Show that the catch specifying the superclass catches the subclass exceptions. The order of exception handlers is important. If you try to catch a superclass exception ty
Java question
Write a Thermostat class such that a user of the Thermostat class can create an object
of Thermostat and set it to the desired temperature within a pre-specified range. If the
user tries set the temperature outside this range it should throw a TemperatureTooHigh
or TemperatureTooLow exception. Use inheritance to create an exception superclass
TemperatureOutofRange and subclasses TemperatureTooHigh and
TemperatureTooLow.
Sample Tester code:
Thermostat t = new Thermostat(0, 100);
t. setTemp(50); // Should be OK.
t.setTemp(150); // Should throw TemperatureTooHigh exception.
t.setTemp(-50); // Should throw TemperatureToolLow exception.
Write a Tester class to demonstrate throwing and catching of exceptions. Show that the
catch specifying the superclass catches the subclass exceptions. The order of exception
handlers is important. If you try to catch a superclass exception type before a subclass
type, the compiler would generate errors. Also show the re-throwing of exceptions.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images