Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 2 February 2019

SECOND WEEK TERM WORK Program 4 [ IA-1 1 A) ]

4) A chemical company named “XYZ” manufactures various chemicals. To automate and
monitor the manufacturing process, they are planning to install an “Automated
Manufacturing and Maintenance System”. One important task of this system is to monitor
the temperature of the furnace. If the temperature of the furnace rises above 2000C, then it
should immediately generate an alarm. It should also generate a separate alarm if the
temperature of the furnace falls below 500C. You are hired as a Java Programmer to develop
this system. Write a Java Program which simulates the above scenario using exception
handling mechanism.
Create a class named Furnace having furnaceTemp as its attribute, and monitorTemp() &
displayTemp() as its methods. monitorTemp() throws TempTooHighException and
TempTooLowException. Program should first monitor the temperature, and if it is in the
permissible limit, display the temperature using displayTemp() method.

package exception4;
import java.util.*;
class TempTooHighException extends Exception{
public TempTooHighException(String message) {
super(message);
}
}
class TempTooLowException extends Exception{
public TempTooLowException(String message) {
super(message);
}
}
class Furnace{
double furnaceTemp=0;
Furnace(double temp)
{
furnaceTemp=temp;
}
void monitorTemp() throws TempTooHighException,TempTooLowException
{
if(furnaceTemp>200)
{
throw new TempTooHighException("Temperature Above 200'C");
}
else if(furnaceTemp<50)
{
throw new TempTooLowException("Temperature Below 50'C");
}
else
{
displayTemp();
}
}
void displayTemp()
{
System.out.println("Temperature of the Furnace=:"+furnaceTemp);
}
}
public class XYZ {
public static void main(String[] args) {
int n;
do{
Scanner input=new Scanner(System.in);
System.out.println("Enter the Furnace Temperature");
double temp=input.nextDouble();
Furnace obj=new Furnace(temp);
try {
obj.monitorTemp();
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e.getMessage());
}
System.out.println("Do u want to exit Press 0");
     n=input.nextInt();
}while(n!=0);
}
}

0 comments:

Post a Comment