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.")
   }

}

Recommended Answers

All 14 Replies

alarmFailure() (cannot be overridden by any subclass)

Make the alarmFailure function as final:

public final void alarmFailure(String a)
   {

   }

hope it helps.

The sensorSample is declared as void in the super class:

public abstract [B]void [/B]alarm();

butu in the subclasse it id declared as int:

public [B]int [/B]sensorSample()

hope it helps.

This is olso wrong:

SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
                              new EntryAlarm("rear entrance",10),
                              new COAlarm("furnace room", 130) );

the is no constructur in the SecurityAlarm superclass.

Hope it helps.

This is olso wrong:

SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
                              new EntryAlarm("rear entrance",10),
                              new COAlarm("furnace room", 130) );

the is no constructur in the SecurityAlarm superclass.

Hope it helps.

The final test is the provided code. I have to change the SecurityAlarm code to make the FinalTest program run.

This code is also provided to base the EntryAlarm off of (FireAlarm should work with FinalTest after creating SecurityAlarm)

import java.lang.Math;
import java.text.DecimalFormat;

public class FireAlarm extends SecurityAlarm
{
	final double MAX_TEMP_THRESHOLD = 200.0;
	final double TEMP_FAIL_THRESHOLD = 40.0;

	double tempThreshold = 0.0;
	double tempSensor = 0.0;
	String location;

	public FireAlarm(String loc)
	{
		location = new String(loc);
		tempThreshold = MAX_TEMP_THRESHOLD;
	}

	public FireAlarm(String loc, double threshold)
	{
		location = new String(loc);
		if(threshold < MAX_TEMP_THRESHOLD)
			tempThreshold = MAX_TEMP_THRESHOLD;
		else
			tempThreshold = threshold;
	}

	public int sensorSample()
	{
		int status = ALARM_NORMAL;
		DecimalFormat twoDigits = new DecimalFormat("##,##0.0");

		tempSensor = Math.random()*1000;  // simulate getting a temperature reading

		if(tempSensor > tempThreshold)
		{
		    alarm();
		    status = ALARM_ACTIVATED;
		}
		else
			if(tempSensor < TEMP_FAIL_THRESHOLD)
			{
			   alarmFailure("\nThe "+location
			                +" fire alarm senses a temperature of "
			                +twoDigits.format(tempSensor)
			                +"\nwhich is below the failure threshold of "+TEMP_FAIL_THRESHOLD);
			   status = ALARM_FAILURE;
		    }

		return status;
	}

	public void alarm()
	{
		DecimalFormat twoDigits = new DecimalFormat("##,##0.0");

		System.out.println("*** The "+location+" fire alarm sensor has detected a potential fire.");
		System.out.println("*** The temperature of "+twoDigits.format(tempSensor)+" has exceeded the threshold of "+tempThreshold+"\n");
	}
}

Have you seen the 3 post I post below?

Yes I guess I don't seem to get how I would add that to the SecurityAlarm code.

What you mean by "that"

I would add that to the SecurityAlarm code

Do you mean the constructors?

What you mean by "that"


Do you mean the constructors?

yes I don't understand how to add those constructors.

Well can you ive a description of the attributs of your classes.
Just the attributs please.

for instance the EntryAlarm had those attributs:

String location;
   int alarmNumber;
   double entrySensor = 0;

What about FireAlarm and COAlarm?

Well can you ive a description of the attributs of your classes.
Just the attributs please.

SecurityAlarm: Has 3 constant values, AlarmFailure method which cannot be overridden by an subclass, Alarm method (abstract), sensorSample method (abstract)
FireAlarm (code give to me): Inherits from SecurityAlarm
FinalTest: Tests the program (code is also given to me)
This is the information I am given. I need to know what to put in the SecurityAlarm class to make the FireAlarm & FinalTest that were given to work.

This is olso wrong:

SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
                              new EntryAlarm("rear entrance",10),
                              new COAlarm("furnace room", 130) );

the is no constructur in the SecurityAlarm superclass.

Hope it helps.

moutanna: why do you think the SecurityAlarm class needs an explicit constructor?

Hi;

moutanna: why do you think the SecurityAlarm class needs an explicit constructor?

I'm thinking nothing at the moment; I'm just trying to understand the requirement.

SecurityAlarm: Has 3 constant values, AlarmFailure method which cannot be overridden by an subclass, Alarm method (abstract), sensorSample method (abstract)
FireAlarm (code give to me): Inherits from SecurityAlarm
FinalTest: Tests the program (code is also given to me)
This is the information I am given. I need to know what to put in the SecurityAlarm class to make the FireAlarm & FinalTest that were given to work.

I was asking about the attributts (data member) not functions:
for instance the EntryAlarm class have those members

final double ENTRY_THRESHOLD = 0.5;//
   String location;
   int alarmNumber;
   double entrySensor = 0;

And this statement in the FinalTest class:

SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
       new EntryAlarm("rear entrance",10),
       new COAlarm("furnace room", 130) );

Suppose that you have :
SecurityAlarm : super calss;
FireAlarm : have a constructor that have two arguments: the first of type String and the second seems to be double;
EntryAlarm : have a constructor with String and int parameters ;
And COAlarm : have a constructor with String and int parameters.

If it is so; buck the the JamesCherrill question:
It is possible to declare a constructor in the super class SecurityAlarm after moving the String “location” attribute to it as follow:

public class SecurityAlarm{
protected String location;
//…other declarations
// the default constructor if necessary goes here
protected SecurityAlarm(String location){
this.location=location;
}

// AlarmFailure method which cannot be overridden by any subclass
  public [B]final [/B]void alarmFailure(String a)
   {
//code here
   }

//…..code goes here
}

The FireAlarm suppose that it have an attribute of type double let it be “attrDouble” then:

public class FireAlarm extends SecurityAlarm{
protected double attrDouble;
public FireAlarm(String location, double attrDouble){
super(location);
this. attrDouble= attrDouble;
}
}
//other code goes here!

EntryAlarm

public class EntryAlarm extends SecurityAlarm{
protected int attrtInt;
public EntryAlarm (String location, int attrInt){
super(location);
this. attrtInt = attrtInt;
}
}
//other code goes here!

And so goes COAlarm ;
Then this statement will be correct:

SecurityAlarm[] alarm = (new FireAlarm("kitchen", 120.0),
       new EntryAlarm("rear entrance",10),
       new COAlarm("furnace room", 130) );

Hope it helps.

Hi, yes, you can do a superclass constructor just like you suggested - it's quite a normal thing to do. I was origionally wondering why you had flagged the lack of an explicit superclass constructor as an error; maybe I misunderstood your post.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.