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: 3853024 • 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

//Please see the solution below , if you like the answer please do thumbs up

A. TemperatureTooLow.java

public class TemperatureTooLow extends Exception{
  
   //Constructor
   public TemperatureTooLow(String msg)
   {
       System.out.println(msg);
   }

}

B. Fridge.java


public class Fridge {
   private int temperature ;
  
   public void setTemp(int temp){
       temperature =temp;      
   }

   public boolean tempNormal() throws TemperatureTooLow{
       if(temperature <10)
           throw new TemperatureTooLow("The current temperature of the fridge is :"+temperature);
       else
           return true;
   }
  
}


C. TempTest.java

public class TempTest {
  
   public static void main(String[] args) {
  
   //Creates an instance of the Fridge class
   Fridge f1=new Fridge();
  
  
   try
   {
  
   //Calls the setTemp method to set the fridge’s temperature to 35
   f1.setTemp(35);
      
   //prints the result of calling tempNormal method.
   System.out.println(f1.tempNormal());
      
   //Calls the setTemp method again to set the fridge’s temperature to 8
   f1.setTemp(8);
   //prints the result of calling tempNormal method.
   System.out.println(f1.tempNormal());
  
   }
   catch(TemperatureTooLow e)
   {
       System.out.println("Exception is :"+e);
   }
}
}

OUTPUT:

true
The current temperature of the fridge is :8
Exception is :TemperatureTooLow

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