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

For today\'s lab we will be completing Exercise P9.5 from the book. IMPORTANT!!

ID: 3753481 • Letter: F

Question

For today's lab we will be completing Exercise P9.5 from the book.

IMPORTANT!! All of your method/class names need to match what is shown in this document. This is the public interface of your class. The classes and methods you need to implement are as follows:

Circuit – single method getResistance. Note that this is simply an empty circuit with no resistors, and therefore the resistance is always 0 for objects of this class. This is the parent class.

Resistor – subclass of Circuit. Represents a single resistor. Needs to override the getResistance method from the superclass Circuit. To compute the value of the Resistor object you simply return the resistance value.

Serial – subclass of Circuit. Contains an ArrayList<Circuit> instance variable. Resistance is computed by the formula given above. Must override inherited getResistance method.

Parallel – subclass of Circuit. Contains an ArrayList<Circuit> instance variable. Resistance is computed by the formula given above. Must override inherited getResistance method.

Choice 1: You should add multiple circuits to the CircuitDemo.java file to verify that your program works as expected. Take screen-shots or capture the output text and include this in your project folder. Sample output from a working project using the given CircuitDemo.java file is shown below.

Combined resistance: 75.0

Expected: 75.0

public class CircuitDemo

{ /**

method that implements tests for Circuit class and sublclasses

@param args - Not Used.

*/

public static void main(String[] args)

{

Parallel circuit1 = new Parallel();

circuit1.add(new Resistor(100));

Serial circuit2 = new Serial();

circuit2.add(new Resistor(100));

circuit2.add(new Resistor(200));

circuit1.add(circuit2);

System.out.println("Combined resistance: " + circuit1.getResistance());

System.out.println("Expected: 75.0");

}

}

public class CircuitTester01

{

public static void main(String[] args)

{

Serial c1 = new Serial();

c1.add(new Resistor(100));

c1.add(new Resistor(200));

c1.add(new Resistor(150));

System.out.println("c1 total R = " + c1.getResistance());

Parallel c2 = new Parallel();

c2.add(new Resistor(100));

c2.add(new Resistor(100));

c2.add(new Resistor(100));

System.out.println("c2 total R = " + c2.getResistance());

Serial c3 = new Serial();

c3.add(c1);

c3.add(c2);

System.out.println("c3 total R = " + c3.getResistance());

Parallel c4 = new Parallel();

c4.add(c1);

c4.add(c2);

System.out.println("c4 total R = " + c4.getResistance());

}

}

Explanation / Answer

Solution:

##Circuit.java

//Circuit class
public class Circuit {

   //resistance variable is assigned to 0
   double resistance = 0;

   //Method is used to get the resistance
   public double getResistance(){
       return 0;
   }

  
}


###Resistor.java

//Resistor class extending Circuit class
public class Resistor extends Circuit {

   //It has its own resistance
   double resistance;
  
   //Parameterized constructor assign the value to its field while object creation
    public Resistor(double R1){
        resistance = R1;
    }

    //Overriding parent's getResistance method
    @Override
    public double getResistance(){
        return resistance;
    }
}

####Serial.java

import java.util.ArrayList;

//Serial class extending Circuit
public class Serial extends Circuit{

//   Creating circuit arraylist to store circuit
   ArrayList<Circuit> circuit;

   //Default Constructor instantiate a new circuit
   public Serial(){
       circuit = new ArrayList<Circuit>();
   }

//   adding circuit to the arraylist
   public void add(Circuit r){
       circuit.add(r);
   }

  
//   Overriding Parent's class getResistance with Serial getResistance because
//   it has different behavior of getting resistance
   @Override
   public double getResistance(){
//      Initially resistance is 0
       double resistance = 0;

//       Total resistance = r1 + r2 +r3 + ..
       for(Circuit c: circuit) {

           resistance += c.getResistance(); //Calculating resistance
       }
//       returning total resistance
       return resistance;

   }
}


####Parallel.java

import java.util.ArrayList;

//Parallel class extending circuit class
public class Parallel extends Circuit {
  
//   Creating circuit ArrayList to store circuit
   ArrayList<Circuit> circuit;

//   Default constructor instantiate new circuit
   public Parallel(){

       circuit = new ArrayList<Circuit>();

   }

//   adding circuit to the arraylist
   public void add(Circuit r){

       circuit.add(r);

   }

//   Overriding Parent's class getResistance with Serial getResistance because
//   it has different behavior of getting resistance
   @Override
   public double getResistance(){
//       Initially resistance is 0
       double resistance = 0;

//       1/Total resistance = 1/r1 + 1/r2 + 1/r3 + ..
       for(Circuit c: circuit){

           resistance +=1/c.getResistance();//Calculating resistance in parallel

       }
//returning total resistance
       return 1/resistance;

   }
}


##CircuitDemo.java

//Driver class 1
public class CircuitDemo

{ /**

method that implements tests for Circuit class and subclasses

@param args - Not Used.

*/
//Main method to check whether our classes and subclasses are working as expected or not
   public static void main(String[] args)

   {

       Parallel circuit1 = new Parallel();

       circuit1.add(new Resistor(100));

       Serial circuit2 = new Serial();

       circuit2.add(new Resistor(100));

       circuit2.add(new Resistor(200));

       circuit1.add(circuit2);

       System.out.println("Combined resistance: " + circuit1.getResistance());

       System.out.println("Expected: 75.0");

   }

}

######CircuitTester01.java

public class CircuitTester01

{
   //Main method to check whether our classes and subclasses are working as expected or not
   public static void main(String[] args)

   {

       Serial c1 = new Serial();

       c1.add(new Resistor(100));

       c1.add(new Resistor(200));

       c1.add(new Resistor(150));

       System.out.println("c1 total R = " + c1.getResistance());

       Parallel c2 = new Parallel();

       c2.add(new Resistor(100));

       c2.add(new Resistor(100));

       c2.add(new Resistor(100));

       System.out.println("c2 total R = " + c2.getResistance());

       Serial c3 = new Serial();

       c3.add(c1);

       c3.add(c2);

       System.out.println("c3 total R = " + c3.getResistance());

       Parallel c4 = new Parallel();

       c4.add(c1);

       c4.add(c2);

       System.out.println("c4 total R = " + c4.getResistance());

   }

}

Sample Run of CircuitDemo:

Combined resistance: 75.0
Expected: 75.0

Sample Run of CircuitTester01:

c1 total R = 450.0
c2 total R = 33.333333333333336
c3 total R = 483.3333333333333
c4 total R = 31.03448275862069


Note: If you have any doubt please do comment below, before giving any negative feedback, I will be glad to help you out. thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote