Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java A: Write an Exception class called TemperatureTooLow.java that will be used

ID: 3852995 • Letter: J

Question

Java

A: Write an Exception class called TemperatureTooLow.java that will be used in the following program.

B: Write a class called Fridge.java, which has an instance variable called temperature of type int and the following methods:

public void setTemp(int temp){
...
}

public boolean tempNormal(){
...
}

The setTemp method sets the temperature instancevariable.
When tempNormal method is called, if temperature < 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 setTemp method to set the fridge’s temperature to 35 and then prints the result of calling tempNormal method.

3 - Next it Calls the setTemp method again to set the fridge’s temperature to 8 and again prints the result of calling tempNormal method.

4 - In steps 2 and 3, the tempTest.java class should catch the TemperatureTooLow Exception where required.

5 - The TemperatureTooLow Exception shows the current temperature of the fridge when thrown.

Explanation / Answer

The answer is as follows:

public class TemperatureTooLow extends Exception {
       private int temperature;
       public TemperatureTooLow(int temp){
             temperature = temp;
       }
       public int getTemp(){
             return temp;
       }
}

public class Fridge {
      private int temperature;
      public void setTemp(int temp){
          temperature = temp;
      }
      public boolean tempNormal() throws TemperatureTooLow {
         try {
              if (temperature < 10)
                  throw new TemperatureTooLow(temperature);
              else
                  return true;
         } catch (TemperatureTooLow e){
              System.out.println("The current temperature is " + e.getTemp());
         }
      }
}

public class tempTest {
     public static void main(String[] args){
           Fridge fr;

           fr.setTemp(35);
           System.out.println(fr.tempNormal());
           fr.setTemp(8);
           fr.tempNormal();
     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote