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

Help with a java program. A: Write an Exception class called TemperatureTooLow.j

ID: 3833072 • Letter: H

Question

Help with a java program.

A: Write an Exception class called TemperatureTooLow.java

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 instance variable. 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

TemperatureTooLow.java

public class TemperatureTooLow extends Exception {

   //Parameterized constructor
   public TemperatureTooLow(String mesg) {
       System.out.println(mesg);
      
   }
}

_______________________

Fridge.java

public class Fridge {
   // Declaring instance variables
   private int temperature;

   // Zero argumented constructor
   public Fridge() {
       super();

   }

   // Parameterized constructor
   public Fridge(int temperature) {
       super();
       this.temperature = temperature;
   }

   // Setter method
   public void setTemperature(int temperature) {
       this.temperature = temperature;
   }

   public boolean tempNormal() throws TemperatureTooLow {
       if (temperature < 10)
           throw new TemperatureTooLow("Current Temperature is :"+ temperature);
       else
           return true;
   }

}

_____________________

TempTest.java

public class TempTest {

   public static void main(String[] args) {
       // Create an object of Fridge class
       Fridge f = new Fridge();

       // Setting the temperature by calling the setter method
       f.setTemperature(35);
       try {

           // Calling the method on the Fridge class
           System.out.println(f.tempNormal());
           f.setTemperature(8);
           System.out.println(f.tempNormal());
       } catch (TemperatureTooLow e) {
           System.out.println("Exception :" + e);
       }

   }

}

______________________

Output:

true
Current Temperature is :8
Exception :org.students.TemperatureTooLow

________________Thank You

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