I'm having a problem figuring out what goes in the SecurityAlarm class. It is an abstract base class with 3 methods: alarmFailure() (cannot be overridden by any subclass), and alarmSensor(), alarm() (both are abstract. EntryAlarm is a class that inherits from SecurityAlarm. If the simulation of an alarm exceeds the constant value (the threshold), the entry alarm is triggered.
Security Alarm:
public abstract class SecurityAlarm
{
/**
* Constant values
*/
public static final int ALARM_NORMAL = 0;
public static final int ALARM_ACTIVATED = 1;
public static final int ALARM_FAILURE = 2;
/**
* This method cannot be overridden by any subclass
*/
public void alarmFailure(String a)
{
}
/*
* Abstract class alarm
*/
public abstract void alarm();
/*
* Abstract class sensorSample
*/
public abstract void sensorSample();
}
EntryAlarm:
import java.lang.Math;
import java.text.DecimalFormat;
public class EntryAlarm extends SecurityAlarm
{
final double ENTRY_THRESHOLD = 0.5;
String location;
int alarmNumber;
double entrySensor = 0;
public int sensorSample()
{
int status = ALARM_NORMAL;
DecimalFormat twoDigits = new DecimalFormat("##,##0.0");
entrySensor = Math.random()*1000; // simulate getting a temperature reading
if(entrySensor > ENTRY_THRESHOLD)
{
alarm();
status = ALARM_ACTIVATED;
}
else
if(entrySensor < ENTRY_THRESHOLD)
{
alarmFailure("\nThe "+location
+" entry alarm deteted a reading of "
+entrySensor
+"\nwhich is below the failure threshold of "+ENTRY_THRESHOLD);
status = ALARM_FAILURE;
}
return status;
}
public void alarm()
{
DecimalFormat twoDigits = new DecimalFormat("##,##0.0");
System.out.println("*** The "+location+" entry sensor "+alarmNumber+" entry alarm sensor has detected a potential entry.");
System.out.println("*** The reading of "+twoDigits.format(entrySensor)+" has exceeded the threshold of "+ENTRY_THRESHOLD+"\n");
}
}
To test the code, FinalTest
public class FinalTest
{
public static void main(String[] args)
{
SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
new EntryAlarm("rear entrance",10),
new COAlarm("furnace room", 130) );
int status = 0;
System.out.println("Simulation of the alarm testing.\n");
// test alarm simulations
for(int i=0; i < 3;++i)
for(int j=0; j < alarm.length;++j)
status = alarm[k],sensorSample();
System.out.println("\nEnd of program.")
}
}