java programming Write a Thermostat class such that a user of the Thermostat cla
ID: 3716804 • Letter: J
Question
java programming
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.