A: Write an Exception class called TemperatureTooLow.java B: Write a class calle
ID: 3784502 • Letter: A
Question
A: Write an Exception class called TemperatureTooLow.java B: Write a class called Fridge.java, which has the following method: public boolean checkTemp(int temp){ … } If this method is called with temp < 10, it throws TemperatureTooLow Exception otherwise returns true. C: Write a TempTest.java class that does the following: 1- Creates an instance of the Fridge class 2- Calls the checkTemp method and catches the TemperatureTooLow Exception. It calls checkTemp method once with the temp > 10 and once with the temp < 10 and prints the results. needed coding java for all three parts
Explanation / Answer
/******************TemperatureTooLow.java**********************/
/**
* TemperatureTooLow class must extend the Throwable class for custom Exception
* class
* */
public class TemperatureTooLow extends Throwable {
/**
* default constructor
* */
public TemperatureTooLow() {
super();
// TODO Auto-generated constructor stub
}
}
/***********************Fridge.java**********************/
public class Fridge {
/**
* checkTemp method checks that given temp is less then 10 or not if temp is
* less then 10 then it will throw an exception of TemperatureTooLow
* otherwise it return true
* */
public boolean checkTemp(int temp) throws TemperatureTooLow {
if (temp < 10) {
throw new TemperatureTooLow();
} else {
return true;
}
}
}
/************************************TempTest.java************************/
public class TempTest {
/**
* @param args
*/
public static void main(String[] args) {
// creating instance of fridge class
Fridge fridge = new Fridge();
try {
// calling check temp with temp 8
// it will throw an exception as temp is less then 10
boolean flag = fridge.checkTemp(8);
System.out.println(flag);
} catch (TemperatureTooLow exception) {
System.out.println(exception);
}
}
}
/*****************************output*********************/
TemperatureTooLow if less then 10
true if greater or equal to 10
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.