Answer to Question #162351 in Java | JSP | JSF for Yeng

Question #162351

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-10T10:31:53-0500
public class CIRCUIT {

   private double totalResistance;
   static int totalVoltage;
   double totalCurrent;

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

   public String getTotalCurrent() {
       totalCurrent = totalVoltage / totalResistance;
       return String.format("%.2f", totalCurrent);
   }

   /**
   * @return the totalResistance
   */
   public String getTotalResistance() {
       return String.format("%.2f", totalResistance);
   }

   /**
   * @param totalResistance the totalResistance to set
   */
   public void setTotalResistance(double totalResistance) {
       this.totalResistance = totalResistance;
   }

}

Series.java



public class Series extends CIRCUIT implements DisplayCircuitType {

   private int numres;

   public void computeTotalResistance(double r1, double r2) {
       numres = 2;
       setTotalResistance(r1 + r2);
   }

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

   public void disType() {
       System.out.println("SERIES CONNECTION with " + numres + " Resistors");
   }

}

Parallel.java



public class Parallel extends CIRCUIT implements DisplayCircuitType {
   private int numres;

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

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

   public void disType() {
       System.out.println("PARALLEL CONNECTION with " + numres + " Resistors");
   }
}

DisplayCircuitType.java

public interface DisplayCircuitType {
   void disType();
}

Driver.java



import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.print("Please Enter 3 resistance values: ");
       double r1, r2, r3;
       r1 = sc.nextDouble();
       r2 = sc.nextDouble();
       r3 = sc.nextDouble();

       Series s1 = new Series();
       Series s2 = new Series();
       Parallel p1 = new Parallel();
       Parallel p2 = new Parallel();
       CIRCUIT.setVoltageSource();

       s1.computeTotalResistance(r1, r2);
       s1.disType();
       System.out.println("Total Resistance in 24 (V) : " + s1.getTotalResistance());
       System.out.println("Total Current in 24 (V) : " + s1.getTotalCurrent());
       System.out.println(" -----------------------------------------------");
       s2.computeTotalResistance(r1, r2, r3);
       s2.disType();
       System.out.println("Total Resistance in 24 (V) : " + s2.getTotalResistance());
       System.out.println("Total Current in 24 (V) : " + s2.getTotalCurrent());
       System.out.println(" -----------------------------------------------");
       p1.computeTotalResistance(r1, r2);
       p1.disType();
       System.out.println("Total Resistance in 24 (V) : " + p1.getTotalResistance());
       System.out.println("Total Current in 24 (V) : " + p1.getTotalCurrent());
       System.out.println(" -----------------------------------------------");
       p2.computeTotalResistance(r1, r2, r3);
       p2.disType();
       System.out.println("Total Resistance in 24 (V) : " + p2.getTotalResistance());
       System.out.println("Total Current in 24 (V) : " + p2.getTotalCurrent());
       sc.close();
   }

}

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