• 0

[Java] Class Hierarchy Design


Question

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

  • 0

You probably don't need lists because any series/parallel circuit can be recursively decomposed into a network of 2-element parts, so something like:

abstract class Circuit .... abstract public real getResistance(); ...

class Resistor extents Circuit ... public Resistor(real resistance) {...} getResitance(){return resitance;} ...

class SeriesCircuit extends Circuit .... public SeriesCircuit(Circuit firstElement, Circuit secondElement) ...

... getResistance(){return firstElement.getResistance() + secondElement.getResitance()); ...

class ParallelCircuit extends Circuit ... // like SeriesCircuit except for resistance calc.

// eg of use

Resistor r1 = new Resistor(1000.0);

Resistor r2 = new Resistor(2200.0);

SeriesCircuit s1 = new SeriesCircuit(r1,r2);

Resistor r3 = new Resistor(4700.0);

ParallelCircuit p1 = new ParallelCircuit(r3, s1);

System.out.println(p1.getResitance());

Link to comment
Share on other sites

  • 0

Now, I am confused. I don't really understand what the question is asking and the teacher does not allow for us to contact him or ask him for help. With that in mind, I wrote two designs but I don't know which to choose:

1.

public abstract class Circuit {
	public abstract int resistance();
}

public class BasicCircuit extends Circuit {
	string name;
	int res;

	public BasicCircuit (string name, int res) {//constructor
		this.name = name;
		this.res = res;
	}

	public int resistance() {//redefine  or define resistance
		return res; //resistance is constant, doesn't need to calculate anything.
	}
}

public class SerialCircuit extends Circuit{
	List<Circuit> circuits;

	public SerialCircuit(List<Circuit> circuits){ //constructor
		this.circuits = circuits;
	}

	public int resistance(){ //define resistance
		int res = 0;
		foreach (Circuit c in circuits){//the sum of all the resistances of its subcircuits
			res += c.resistance(); //res = res + c.resistance();
		}
	return res;
	}
}

2.

class Resistor() {
int resistance;

int resistance() {
return resistance;
}
}

class serialResistor extends Resistor() {
Resistor[] resistors;
int resistance() {
resistance = 0;
for (i = 0; i < resistors.length; i++) {
resistance += resistors[i].resistance();
}
return resistance;
}

class parallelResistor extends Resistor() {
Resistor[] resistors;
int resistance() {
double invr = 0;
for (i = 0; i < resistors.length; i++) {
invr += 1/resistors[i];
}
return 1/invr;
}
}

If anyone can walk me to the correct one, it would be greatly appreciated.

Link to comment
Share on other sites

  • 0

Quick intro to "foreach" loops: There is no such Java syntax as "foreach", that's just how it's spoken. The actual syntax is

for (Object o : somecollection), pronounced " for each object o in someCollection". Also List is an interface, not a class, so you can't just create one, you have to create an instance of a class (that implements List) such as Vector.

eg:

Vector<Resistor> resistors = new Vector<Resistor>();
resistors .add(resistor1); resistors .add(resistor2); ...
for (Resistor r : resistors) {
   // do something with r
}

As you may guess, I prefer your first solution, it won't take much to add parallel circuits.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.