I'm supposed to design a class hierarchy that can manage and resolve arbitrary resistive circuits of serial/parallel combinations. These classes in the hierarchy must be able to calculate the equivalent resistance and intensities in all parts of the circuit.
Here's what I have so far, but I don't know if I'm on the right track or not:
public class circuit{
public int resistance();
}
public class basicCircuit extends circuit {
string name;
int res;
//constructor
public basicCircuit (string name, int res)
{
this.name = name;
this.res = res;
}
//redefine or define resistance
public int resistance ()
{
return res; //resistance is constant, doesn't need to calculate anything.
}
}
public class serialCircuit extends circuit{
List<circuit> circuits;
//constructor
public serialCircuit(List<circuit> cir){
this.circuits = cir;
}
//define resistance
public int resistance()
{
int res = 0;
foreach (circuit c in circuits)
//the sum of all the resistances of its subcircuits
{ res= res + c.resistance(); }
return res;
}
}
I'm not really sure if this answers the question or not and I'm really unsure about the usage of List, foreach and "in".
Question
Conciente
Hello, I need some help in this.
I'm supposed to design a class hierarchy that can manage and resolve arbitrary resistive circuits of serial/parallel combinations. These classes in the hierarchy must be able to calculate the equivalent resistance and intensities in all parts of the circuit.
Here's what I have so far, but I don't know if I'm on the right track or not:
public class circuit{ public int resistance(); } public class basicCircuit extends circuit { string name; int res; //constructor public basicCircuit (string name, int res) { this.name = name; this.res = res; } //redefine or define resistance public int resistance () { return res; //resistance is constant, doesn't need to calculate anything. } } public class serialCircuit extends circuit{ List<circuit> circuits; //constructor public serialCircuit(List<circuit> cir){ this.circuits = cir; } //define resistance public int resistance() { int res = 0; foreach (circuit c in circuits) //the sum of all the resistances of its subcircuits { res= res + c.resistance(); } return res; } }I'm not really sure if this answers the question or not and I'm really unsure about the usage of List, foreach and "in".
Any help would be greatly appreciated.
Link to comment
Share on other sites
3 answers to this question
Recommended Posts