Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with this program in java, here is the task thanks a lot! Inheritance

ID: 3876836 • Letter: N

Question

Need help with this program in java, here is the task

thanks a lot!

Inheritance and Interfaces. **I have written half of my program just wanted to confirm with an expert that what I have is correct or not** So please add necessary comments.

NOTICE: There are NO static variables allowed in this lab. The only class that a static method is allowed is in theTestDriver class. The only static method that you actually really need is main. All member variables must be private. Output statements should only be in the test class!

The purpose of this assignment is to practice developing classes, to use inheritance and interfaces, to develop a program to thoroughly test your classes and interfaces, and to generate official Javadoc documentation on all your classes.

Overview

You will be developing one interface, one base class that implements the interface, one derived class, and one test class. The interface and classes create a simple model of some basic electronic components and their behaviors. The basic formulas you will need to use to complete the model are explained and provided within this document. The following describes the interface and classes you must develop and test.

PowerDissipation Interface

This interface supports reporting the amount of power consumed by an electronic component given a currentvalue. If a component's power rating is exceeded it is expected to throw an Exception. This interface contains the following method:

double powerFromCurrent (double current) throws Exception;

Resistor Class

This class contains the primary electrical properties of a resistor. It has a resistance value in ohms which must be greater than 0, a tolerance value expressed as a number between 0 and 1 (not inclusive), and a power ratingexpressed as some number of watts greater than 0. This class implements the PowerDissipation interface.

The class has one constructor which takes values for the three properties and validates them. If any of the values are out of range, an Exception must be thrown containing a meaningful error message.

The class has one getter for the resistance value. It does not have setters. Once a Resistor object is created, it is immutable.

The class overrides the toString method. It returns a nicely formatted String reporting the values of the three properties.

It has the following two methods:

double minResistance( );

double maxResistance( );

These methods each return a value that is a function of the resistance property and the tolerance property. As an example, if the resistance is 10000, and the tolerance is 0.10, then:

Minimum resistance = 10000 * (1.0 - 0.10) = 9000

Maximum resistance = 10000 * (1.0 + 0.10) = 11000

It implements the method of the PowerDissipation interface. The following formula is needed to calculate the power dissipated by the resistor for a given current.

pd = current value * current value * maximum resistance value

If the calculated power dissipation exceeds the resistor’s power rating, then an exception must be thrown containing an appropriate error message.

VariableResistor Class

This class extends the Resistor class. It adds the ability to control the amount of resistance. It contains one additional property which is the control setting. The control setting can be varied from 0 to 1 inclusive. A setting of 0 means the resistance is effectively bypassed. A setting of 1 means 100% of the resistance is effectively seen.

The only constructor for this class takes 4 parameters. If any of the parameters are invalid an exception is thrown which contains a meaningful error message.

There is a setter for the control setting property. The setter must validate the parameter value. If it is out of range, it throws an IllegalArgumentException which contains a meaningful error message.

The base class methods for getResistance, minResistance, and maxResistance must be overridden to allow this class to apply the control setting as illustrated here:

baseClassMethod( ) * control setting value

For example, if the resistance value is 10000, and the control setting is 0.5, then this class’s getResistancemethod would return 5000.

This class overrides the toString method of the base class. It adds the control setting value to the string from the base class.

This class overrides the base class method for the PowerDissipation interface. The code is basically the same as in the base class, but the maximum resistance value is different in this class than in the base class.

TestDriver Class

This class must contain test cases that thoroughly tests all aspects of the Resistor and VariableResistor classes. Itcontains the main method for the project. Each test case must be commented as to what is being tested. Each test case will generally include only a few lines of code, including an output statement that reports the result of that test.

Develop test cases for the Resistor class where all the inputs to the various class methods are valid.

Develop test cases for the VariableResistor class where all the inputs to the various class methods are valid.

Develop test cases for the PowerDissipation interface where all the inputs to the various interface methods are valid. Apply those test cases to both the Resistor object and the VariableResistor object.

Develop test cases for the Resistor class that cause all possible exceptions to be thrown. Test each boundary that can cause an exception to occur.

NOTE: When testing for an expected exception, a test case needs to be structured as follows:

try {
// code that is supposed to cause an exception
// output statement to report the test failed to generate the expected exception
}
catch ( Exception e) {
// output statement to report the expected exception did occur
}

Develop test cases for the VariableResistor class that cause all possible exceptions to be thrown. Test each boundary that can cause an exception to occur.

Develop test cases for the PowerDissipation interface that cause all possible exceptions to be thrown. Test each boundary that can cause an exception to occur. Apply these tests to a Resistor object and a VariableResistorobject.

Documentation

You must provide complete documentation for the PowerDissipation interface, and both the Resistor class and theVariableResistor class. Javadoc comments must be provided at the class and interface level that include the author plus an overview of the purpose of the interface or class. Javadoc comments must be provided for each method of the Resistor and VariableResistor classes. The method level comments must include parameter explanations, return explanations, and exception explanations. Any preconditions should be explained with the parameters and exceptions, and postconditions should be explained with the return.

The TestDriver class needs to have a class level Javadoc comment identifying the author, class section, professor, TA, and assignment. It should also explain the purpose of the TestDriver class.

Development Strategy

Develop things incrementally. Start with the Resistor class without implementing the PowerDissipation interface. When you have that set up, then write the test cases for that class. Once that class is tested, develop theVariableResistor class. Then write the test cases for that class. Then add the PowerDissipation interface. Implement that interface into the Resistor class. Add test cases for that. Finally override the PowerDissipationmethods from the Resistor class in the VariableResistor class. Then add the test cases for that.

Rubric (35 pts)

Use of any static variables will result in an automatic 25% point penalty. Use of static methods in any class other than the TestDriver class will result in an automatic 25% point penalty.

PowerDissipation Interface (3 pts)

Resistor Class (9)

Constructor with exception
1 getter
min and max resistance
toString
Implements PowerDissipation interface

VariableResistor Class (10)

Properly uses inheritance from base class
Constructor
1 setter with exception
Overrides get, min, and max resistance
Overrides toString
Overrides PowerDissipation method

Test Class (9 pts)

Resistor class test cases for normal operation
VariableResistor class test cases for normal operation
PowerDissipation interface test cases for normal operation on both classes
Resistor class test cases for exceptions
VariableResistor class test cases for exceptions
PowerDissipation interface test cases for exception on both classes
Comments for every test case

Documentation (4 pts)

Class and method level Javadoc for Resistor class
Class and method level Javadoc for VariableResistor class
Interface level Javadoc for PowerDissipation interface.
Class level Javadoc for TestDriver class.

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//PowerDissipation.java interface

public interface PowerDissipation {

      /**

      * method to calculate and return power dissipation

      */

      double powerFromCurrent(double current) throws Exception;

}

//Resistor.java class

public class Resistor implements PowerDissipation {

      /**

      * Class to represent a Resistor component

      */

      private double resistance;

      private double tolerance;

      protected int powerRating;

      /**

      * Constructor to initialize all 3 variables after verification

      */

      public Resistor(double resistance, double tolerance, int powerRating)

                  throws Exception {

            if (resistance <= 0 || tolerance <= 0 || tolerance >= 1

                        || powerRating <= 0) {

                  /**

                  * Any of the values are out of range

                  */

                  throw new Exception(

                              "Resistance and power rating must be greater than 0"

                                          + " and tolerance must be between 0-1");

            }

            this.resistance = resistance;

            this.tolerance = tolerance;

            this.powerRating = powerRating;

      }

      /**

      * getter method for resistance

      */

      public double getResistance() {

            return resistance;

      }

      /**

      * method to find the minimum resistance

      */

      public double minResistance() {

            return resistance * (1.0 - tolerance);

      }

      /**

      * method to find the maximum resistance

      */

      public double maxResistance() {

            return resistance * (1.0 + tolerance);

      }

      /**

      * A method to return a string containing all 3 values

      */

      @Override

      public String toString() {

            return "Resistance: " + resistance + ", Tolerance: " + tolerance

                        + ", Power Rating: " + powerRating;

      }

     

      public double powerFromCurrent(double current) throws Exception {

            double pd=0;

            /**

            * Calculating the power dissipation

            */

            pd=current*current*maxResistance();

            if(pd>powerRating){

                  /**

                  * calculated power dissipation exceeds the resistor’s power rating

                  */

                  throw new Exception("power dissipation exceeds the resistor’s power rating");

            }

            return pd;

      }

}

//VariableResistor.java class

public class VariableResistor extends Resistor {

      /**

      * Class to represent a Variable Resistor component with a control setting value

      */

      private double controlSetting;

      public VariableResistor(double resistance, double tolerance,

                  int powerRating, double controlSetting) throws Exception {

            /**

            * passing values to the super class constructor

            */

            super(resistance, tolerance, powerRating);

            if (controlSetting < 0 || controlSetting > 1) {

                  throw new Exception(

                              "Invalid control setting value, it should be between 0-1 [inclusive]");

            }

            this.controlSetting=controlSetting;

      }

      /**

      * setter method to set control setting

      * @throws IllegalArgumentException in case of invalid values

      */

      public void setControlSetting(double controlSetting) throws IllegalArgumentException{

            if (controlSetting < 0 || controlSetting > 1) {

                  throw new IllegalArgumentException(

                              "Invalid control setting value, it should be between 0-1 [inclusive]");

            }

            this.controlSetting = controlSetting;

      }

      /**

      * overridden getResistance() method

      */

      @Override

      public double getResistance() {

            return super.getResistance()*controlSetting;

      }

      /**

      * overridden maxResistance() method

      */

      @Override

      public double maxResistance() {

            return super.maxResistance()*controlSetting;

      }

      /**

      * overridden minResistance() method

      */

      @Override

      public double minResistance() {

            return super.minResistance()*controlSetting;

      }

     

      @Override

      public String toString() {

            return super.toString()+", Control Setting: "+controlSetting;

      }

      @Override

      public double powerFromCurrent(double current) throws Exception {

            double pd=current*current*this.maxResistance();

            if(pd>powerRating){

                  /**

                  * calculated power dissipation exceeds the resistor’s power rating

                  */

                  throw new Exception("power dissipation exceeds the resistor’s power rating");

            }

            return pd;

      }

}

//TestDriver.java class

public class TestDriver {

      public static void main(String[] args) {

            try {

                  Resistor resistor=new Resistor(100, 0.1, 30000);

                  VariableResistor variableResistor=new VariableResistor(100, 0.1, 30000, 0.5);

                  System.out.println("Resistor: "+resistor);

                  System.out.println("Max Resistance: "+resistor.maxResistance());

                  System.out.println("Min Resistance: "+resistor.minResistance());

                  System.out.println("Power rating for current value 5: "+resistor.powerFromCurrent(5));

                 

                  /**

                  * The following line of code will cause an exception

                  * and will display a message 'power dissipation exceeds the resistor’s power rating'

                  * if uncommented.

                  *

                  * System.out.println(resistor.powerFromCurrent(100));

                  */

                 

                 

                  System.out.println(" Variable Resistor: "+variableResistor);

                  System.out.println("Max Resistance: "+variableResistor.maxResistance());

                  System.out.println("Min Resistance: "+variableResistor.minResistance());

                  System.out.println("Power rating for current value 5: "+variableResistor.powerFromCurrent(5));

                  /**

                  * The following lines of code will cause Exceptios.

                  * Uncomment it if you want to test one by one.

                  *

                  * variableResistor.setControlSetting(2);

                  *

                  * Resistor resistor2=new Resistor(100, 20, -5);

                  *

                  * VariableResistor variableResistor2=new VariableResistor(100, .6, 15, 6);

                  */

                 

            } catch (Exception e) {

                  System.err.println(e.getMessage());

            }

      }

}

/*OUTPUT*/

Resistor:

Resistance: 100.0, Tolerance: 0.1, Power Rating: 30000

Max Resistance: 110.00000000000001

Min Resistance: 90.0

Power rating for current value 5: 2750.0000000000005

Variable Resistor:

Resistance: 100.0, Tolerance: 0.1, Power Rating: 30000, Control Setting: 0.5

Max Resistance: 55.00000000000001

Min Resistance: 45.0

Power rating for current value 5: 1375.0000000000002