Question

In: Computer Science

This is an abstract class to represent a weather sensor. Weather sensors * report weather parameters...

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 */
   }

}

Solutions

Expert Solution

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;

      }

}


Related Solutions

Wireless Sensor Networks/Internet of Things Considering sensors and signal processing (based on in-class materials or your...
Wireless Sensor Networks/Internet of Things Considering sensors and signal processing (based on in-class materials or your own knowledge if you have a background here) how would you determine if the entity in your sensor field was an elephant or a lion
(Python) In a weather station, there is a sensor that measures the temperature three times a...
(Python) In a weather station, there is a sensor that measures the temperature three times a day (in Celsius). Write a program that asks the user to input three numbers, corresponding to the sensor's three readings for a particular day. Then, print the minimum, maximum and average value of the three numbers. Note: If one or two inputs are either less than -70, or greater than +50 degrees, you should ignore those one or two inputs, and calculate the minimum,...
You are investigating two sensors to use in an experiment. Sensor A has area 600 ±...
You are investigating two sensors to use in an experiment. Sensor A has area 600 ± 0.3 mm2 and the gap thickness is 0.3 ± 0.01 mm. Sensor B has area 400 ± 0.25 mm2 and the gap thickness is 0.2 ± 0.02 mm. Estimate the relative (hint: this is a %) and absolute uncertainties (hint: this has units of capacitance) in capacitance for both sensors. Your experiment requires that the capacitance is measured accurately within 5%. Which would you...
describe location of sensor and working of processors and sensors in BMW car series 7 or...
describe location of sensor and working of processors and sensors in BMW car series 7 or 9.
An abstract report on test for carbohydrates
An abstract report on test for carbohydrates
We went over Sensors in class. There are other types of third party sensors that work...
We went over Sensors in class. There are other types of third party sensors that work with the EV3 (or the NXT). Describe what these types of sensors would be used for and how they work. They can be short descriptions, but each sensor description should be about three lines and in plain English. Tell the name of the third party company that sells the sensor and how they can be used with the EV3 or NXT (ex. some may...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's cart. Items of Type T can be added * or removed from the cart. A hashmap is used to keep track of the number of items * that have been added to the cart example 2 apples or 4 shirts. * @author Your friendly CS Profs * @param -Type of items that will be placed in the Cart. */ public abstract class AbstractCart {...
10. Weather forecast : athe table below indicates the accuracy of a local weather report with...
10. Weather forecast : athe table below indicates the accuracy of a local weather report with respect to rain or no rain over the past year. This table gives the results of 365 consecutive days and compares whether it rained or not to whether or not rain was predicted. Did it actually rain? yes No Report predicted Rain 102 18 Report predicted No Rain 40 205 If one day is randomly selected from these 365 days, what is the probability...
Describe the principles and operations of movement sensors: Doppler-Effect Velocity sensor and Spring-Mass Accelerometor; State the...
Describe the principles and operations of movement sensors: Doppler-Effect Velocity sensor and Spring-Mass Accelerometor; State the advantage and disadvantage of each type of movement sensors.
The purpose of creating an abstract class is to model an abstract situation. Example: You work...
The purpose of creating an abstract class is to model an abstract situation. Example: You work for a company that has different types of customers: domestic, international, business partners, individuals, and so on. It well may be useful for you to "abstract out" all the information that is common to all of your customers, such as name, customer number, order history, etc., but also keep track of the information that is specific to different classes of customer. For example, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT