Answer to Question #162622 in Java | JSP | JSF for aj

Question #162622

OOP Activity


1.

Create a class CIRCUIT with the following public attributes: total resistance, total voltage and total current. Assign proper variable type and name for each attribute. Total voltage must be static. (static double variablename)


2.

Create a method setVoltageSource() of circuit class to set a constant value of voltage to 24 volts. This value is true to all types of connection thus; it should be a static method.


3.

Create a public method getTotalCurrent() to compute and display total current in 2 decimal values.

Total current is equal to the ratio of voltage and total resistance. (I = V/R)


4.

Create a child class of class CIRCUITS named Series and Parallel


5.

Each class must have an integer attribute named numres. This variable holds the number of resistors in a circuit and will be used for display only.


6.

In series class, create a public method named computeTotalresistance(double r1, double r2). This method is used to accept two resistance values and computes the total resistance of a series connection with 2 resistors.

Total resistance is the summation of the two resistance values. Also, it sets numres equal to 2.

NOTE: store computed total resistance to the total resistance attribute of the parent class. A child class can use attributes and methods of a parent class

(Inheritance).


7.

Overload the method from the previous item (7) that accepts 3 resistance values and computes the total resistance of a series connection with 3 resistors. Total resistance is the summation of the three resistance values. Also, it sets numres equal to 3.

NOTE: store computed total resistance to the total resistance attribute of the parent class. A child class can use attributes and methods of a parent class

(Inheritance).


8.

Perform item 7 and 8 in parallel class.

a. Total resistance 2 resistor = 1/(1/resistor1 + 1/resistor2)

b. Total resistance 3 resistor = 1/(1/resistor1 + 1/resistor2 + 1/resistor3)

Set value of numres accordingly.


9.

Add new public methods in each class (series and parallel) named disType() to display the type of circuit connection and number of resistor (numres). See test case below.


10.

Implement encapsulation by setting up the total resistance attribute in both series and parallel class to private and creating public class to allow other class to access the total resistance value. This is an example of a read only function when using encapsulation.


11.

Create an interface class called DisplayCircuitType. It contains an abstract method of disType(). Modify the series class and parallel class to implement this interface class. A subclass (extends) can be also implement interface (implements). Syntax: public class childclassname extends parentclassname implements interfaceclassname


12.

Complete the program with the following steps:


a.

Use scanner to enter 3 values (resistor values). Create 3 variables to store each inputs


b.

Create 2 objects of Series Class and 2 objects of Parallel Class


c.

Call setvoltage() to set uniform voltage


d.

Work on with the first object of Series class with 2 resistors. Call computeTotalResistance and disType methods. Print the value of getTotalResistance. Call getTotalCurrent.

Note: In calling computeTotalResistance method you have to indicate number of parameters.


e.

Work on with the first object of Series class with 2 resistors. Call computeTotalResistance and disType methods. Print the value of getTotalResistance. Call getTotalCurrent.

Note: In calling computeTotalResistance method you have to indicate number of parameters.


f.

Test the program for the first circuit. If it gives the correct answer, work on the other circuits


.




1
Expert's answer
2021-02-15T00:07:34-0500
import java.util.Scanner;


class Circuit {
    public double totalResistance;
    static public double totalVoltage;
    public double totalCurrent;


    static public void setVoltageSource() {
        totalVoltage = 24;
    }


    public void getTotalCurrent() {
         totalCurrent = totalVoltage / totalResistance;
         System.out.println(String.format("total current = %.2f", totalCurrent));
    }
}


interface DisplayCircuitType {
    void disType();
}


class Series extends Circuit implements DisplayCircuitType {
     int numres;


     public void computeTotalresistance(double r1, double r2) {
         numres = 2;
         totalResistance = r1 + r2;
     }


     public void computeTotalresistance(double r1, double r2, double r3) {
         numres = 3;
         totalResistance = r1 + r2 + r3;
     }


     public void disType() {
         System.out.println("Series Circuit");
         System.out.println(String.format("number of resistors in a circuit = %d", numres));


     }


     public double getTotalResistance() {
          System.out.println(String.format("total resistance = %.2f", totalResistance));
          return totalResistance;
     }
}


class Parallel extends Circuit implements DisplayCircuitType  {
     int numres;


     public void computeTotalresistance(double r1, double r2) {
         numres = 2;
         totalResistance = 1/(1/r1 + 1/r2);
     }


     public void computeTotalresistance(double r1, double r2, double r3) {
         numres = 3;
         totalResistance = 1/(1/r1 + 1/r2 + 1/r3);
     }


     public void disType() {
         System.out.println("Parallel Circuit");
         System.out.println(String.format("number of resistors in a circuit = %d", numres));
     }


     public double getTotalResistance() {
          System.out.println(String.format("total resistance = %.2f", totalResistance));
          return totalResistance;
     }
}




public class Test {
    public static void main(String[] args) {
        double r1,r2,r3;


        Scanner in = new Scanner(System.in);
        System.out.print("Enter resistance 1 amount: ");
        r1 = in.nextDouble();
        System.out.print("Enter resistance 2 amount: ");
        r2 = in.nextDouble();
        System.out.print("Enter resistance 3 amount: ");
        r3 = in.nextDouble();


        Series series1 = new Series();
        Series series2 = new Series();
        Parallel parallel1 = new Parallel();
        Parallel parallel2 = new Parallel();


        Circuit.setVoltageSource();
        series1.computeTotalresistance(r1, r2);
        series1.disType();
        series1.getTotalResistance();
        series1.getTotalCurrent();
        series2.computeTotalresistance(r1, r2, r3);
        series2.disType();
        series2.getTotalResistance();
        series2.getTotalCurrent();


        parallel1.computeTotalresistance(r1, r2);
        parallel1.disType();
        parallel1.getTotalResistance();
        parallel1.getTotalCurrent();
        parallel2.computeTotalresistance(r1, r2, r3);
        parallel2.disType();
        parallel2.getTotalResistance();
        parallel2.getTotalCurrent();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog