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

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.
Can you show the calculations of how the sensors are working; 1-ACS712 (10A) current sensor. 2-UV...
Can you show the calculations of how the sensors are working; 1-ACS712 (10A) current sensor. 2-UV radiation sensor. 3-DH22 Temperature sensor. please show step by step calculations.
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
Question 2: sensor Char, medaling (17 pts) Q2: Experimental data’s for relative temperature sensors Table 1...
Question 2: sensor Char, medaling (17 pts) Q2: Experimental data’s for relative temperature sensors Table 1 T(C ) 30 40 50 60 70 R( ohm) 111.4658 115.6045 119.1927 123.3417 127.3224 Table 2 T(C ) 30 40 50 60 70 Time 0 60 sec 120 sec 180 240 R( ohm) 111.4658 115.6045 119.1927 123.3417 127.3224                 a) Compute the sensor sensitivity using the data off the table 1? b) Study the sensor linearity based on the data of the table 1?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT