Question: Implement the inheritance Resistor Serial-Parallel-Circuit example using ONLY interfaces. You may not use the extends keyword in any class. Code a test for each class
Implement the inheritance Resistor Serial-Parallel-Circuit example using ONLY interfaces. You may not use the extends keyword in any class. Code a test for each class that you implement. You must use the String.format method in each of the three toString methods to convert a value to a string.
Here is my code that needs to modified to only use interfaces:
public class Circuit { public double getResistance() { return 0; } }
public class Resistor extends Circuit { private double resistance;
public Resistor(double r) { resistance =r; } public double getResistance() { return resistance; }
}
import java.util.*; public class Serial extends Circuit { private ArrayList
import java.util.*; public class Parallel extends Circuit { private ArrayList
} return(1.0/total); }}
public class CircuitTester { public static void main(String[] args) { Circuit a = new Resistor(10.0); Circuit b = new Resistor(18.0); Circuit c = new Resistor(28.0); Circuit d = new Serial(a,b); Circuit f = new Parallel(c,d);
double R = f.getResistance();
System.out.println("Series resistors: "+a.getResistance()+", "+b.getResistance()); System.out.println("Parallel Resistors: "+c.getResistance()); System.out.printf(" total Resistance of the circcuit:%2f ohm", R); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
