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 c = new ArrayList(); double total=0; public Serial(Circuit a, Circuit b) { c.add(a); c.add(b); } public double getResistance() { for(int i=0; i < c.size(); i++) { total = c.get(i).getResistance()+total; } return total; } }

import java.util.*; public class Parallel extends Circuit { private ArrayList c = new ArrayList(); double total = 0; public Parallel(Circuit a, Circuit b) { c.add(a); c.add(b); } public double getResistance() { for(int i=0; i < c.size(); i++) { total = c.get(i).getResistance();

} 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!