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

java programming Write a Thermostat class such that a user of the Thermostat cla

ID: 3716804 • Letter: J

Question


java programming

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 3. Sample Tester code: Thermostat t new ThermoStat(0, 100); t.setTemp(50); tsetTemp(150); t.setTemp(-50); l Should be OK Il Should throw TemperatureTooHigh exception. // Should throw TemperatureToolLow exception. Write a program 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 crrors. Also show the re-throwing of exceptions. implement a priority queue capable of holding objects of an arbitrary type, T, by defining a Friorityoueue class that implements the queue with an Arraytist. A priority queue is a type of list where every item added to the queue also has an associated priority. Define priority in 4

Explanation / Answer

3)

//Test java program
//TestThemoStat.java
public class TestThemoStat
{
   public static void main(String[] args) {
      
       //Create an instance of ThermoStat class
       //with min and max tempature
       ThermoStat t=new ThermoStat(0, 100);
      
       t.setTemp(50);
       t.setTemp(150); //Throw an exception of too high
       t.setTemp(-50); //Throw an exception of too low

      
   }
}

----------------------------------------------------------------------------

//TemperatureOutofRange.java
//Inherits from Exception class
public class TemperatureOutofRange extends Exception
{
   public TemperatureOutofRange(String msg)
   {
       super(msg);
   }
}

----------------------------------------------------------------------------


//TemperatureTooHigh.java
//Subclass of super class TemperatureOutofRange
public class TemperatureTooHigh extends TemperatureOutofRange
{
   public TemperatureTooHigh(String msg) {
       super(msg);
   }
}

----------------------------------------------------------------------------


//TemperatureTooLow.java
//Subclass of super class TemperatureOutofRange
public class TemperatureTooLow extends TemperatureOutofRange
{
   public TemperatureTooLow(String msg) {
       super(msg);
   }
  
}

----------------------------------------------------------------------------


//ThermoStat.java
public class ThermoStat
{
   private int min;
   private int max;
   private int temp;
  
   //Constructor to set min and max
   public ThermoStat(int min,int max) {
       this.min=min;
       this.max=max;
   }
   //Method to set temp and throw exception if any
   public void setTemp(int temp)
   {
       try
       {
           //check if temp is below min
           if(temp<min)
               //throw TemperatureTooLow exception
               throw new TemperatureTooLow("Exception: TemperatureTooLow");
           //check if temp is above min
           if(temp>max)
               //throw TemperatureTooHigh exception
               throw new TemperatureTooHigh("Exception: TemperatureTooHigh");
           //set temp value
           this.temp=temp;

       }
       //catch the subclass , TemperatureTooLow exception
       catch (TemperatureTooLow e) {
           System.out.println(e.getMessage());
       }
       //catch the subclass , TemperatureTooHigh exception
       catch (TemperatureTooHigh e) {
           System.out.println(e.getMessage());
       }
   }
}

----------------------------------------------------------------------------

Sample Output:

Exception: TemperatureTooHigh
Exception: TemperatureTooLow

--------------------------------------------------------------------------------------------------------------------------------------------------------

4)

//PriorityQueue.java
import java.util.ArrayList;
public class PriorityQueue
{

   private ArrayList nameQueue;
   private ArrayList<Integer> priorityQueue;

   //Constructor
   public PriorityQueue()
   {
       nameQueue=new ArrayList();
       priorityQueue=new ArrayList<Integer>();
   }
   /*The method add that take item and priority*/
   public <T> void add(T item,int priority)
   {
       nameQueue.add(item);
       priorityQueue.add(priority);

   }
   /*
   * The isEmpty that returns 0
   * if the nameQueue is empty
   * */
   public boolean isEmpty()
   {
       return nameQueue.size()==0;

   }
   /*
   * The method remove that returns
   * the item by its priority.
   * */
   public <T> T remove()
   {
       int max=0,maxPriority=0;
       for(int i=0;i<nameQueue.size();i++)
       {
           if(priorityQueue.get(i)>maxPriority)
           {
               max=i;
               maxPriority=priorityQueue.get(i);
           }
       }
       priorityQueue.remove(max);
       return (T) nameQueue.remove(max);
   }//end of remove method
}//end of the class

----------------------------------------------------------------------------

//PriorityDriver.java
public class PriorityDriver
{
   public static void main(String args[])
   {
       //Create an instance of PriorityQueue class
       PriorityQueue q=new PriorityQueue();
       //call add to set name and priority
       q.add("X", 10);
       //call add to set name and priority
       q.add("Y", 1);
       //call add to set name and priority
       q.add("Z", 3);

       //Output
       System.out.println(q.remove()); // Returns X
       System.out.println(q.remove()); // Returns Z
       System.out.println(q.remove()); // Returns Y
   }
}

Sample Output:
X
Z
Y