In: Computer Science
This is an abstract class to represent a weather sensor. Weather
sensors
* report weather parameters to a weather station. Sensor readings
are pulled by
* calling the method read. Each sensor has a fixed failure rate
that switches
* the sensor state to damaged and needs to be fixed. A damaged
sensor will
* throw an exception when trying to get its reading.
*
*/
public abstract class WSensor {
/*
* Your Task: Declare attributes to represent failure
rate and damaged state.
*/
/**
* Initialize the sensor with the given failure
rate.
* @param failureRate
*/
public WSensor(double failureRate) {
/* Your Task */
}
/**
* Reads the measurement from the sensor and accounts
for the sensor failures. A
* random number is utilized to simulate the
probability of failures. Once the
* probability is less than the failure rate the sensor
state is switched to
* damaged.
*
* @return the measurement of the sensor.
* @throws SensorFailedException
* if the sensor is damaged.
*/
public final double read() throws
SensorFailedException {
/* Your Task */
return get();
}
/**
* Get the measurement of the sensor regardless of its
state.
*
* @return the measurement of the sensor regardless of
its state.
*/
protected abstract double get();
/**
* Sets the measurement value of the sensor regardless
of the state.
*
* @param value
* the value of the measurement
*/
public abstract void set(double value);
/**
* Fixes a damaged sensor
*/
public final void fix() {
/* Your Task */
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// WSensor.java
public abstract class WSensor {
/*
* Your Task: Declare attributes to represent failure rate and damaged
* state.
*/
private double failure_rate; // a failure rate between 0 and 1.0
private boolean damaged; // true if sensor is damaged, else false
/**
* Initialize the sensor with the given failure rate.
*
* @param failureRate
*/
public WSensor(double failureRate) {
// initializing failure rate
this.failure_rate = failureRate;
// assuming sensor is not damaged initially
this.damaged = false;
}
/**
* Reads the measurement from the sensor and accounts for the sensor
* failures. A random number is utilized to simulate the probability of
* failures. Once the probability is less than the failure rate the sensor
* state is switched to damaged.
*
* @return the measurement of the sensor.
* @throws SensorFailedException
* if the sensor is damaged.
*/
public final double read() throws SensorFailedException {
// if the sensor is currently damaged, throwing SensorFailedException,
// assuming SensorFailedException has a default constructor, otherwise,
// add pass some message as needed
if (damaged) {
throw new SensorFailedException();
}
// generating a probability value between 0.0 and 1.0
double probability = Math.random();
// if probability is less than current sensor reading
if (probability < get()) {
// setting sensor to be damaged
damaged = true;
// throwing exception.
throw new SensorFailedException();
}
// otherwise, returning the sensor reading
return get();
}
/**
* Get the measurement of the sensor regardless of its state.
*
* @return the measurement of the sensor regardless of its state.
*/
protected abstract double get();
/**
* Sets the measurement value of the sensor regardless of the state.
*
* @param value
* the value of the measurement
*/
public abstract void set(double value);
/**
* Fixes a damaged sensor
*/
public final void fix() {
// simply setting damaged to false
damaged = false;
}
}