Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Java] Class Hierarchy Design
#1
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:

Code:
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". This is due for today and it's driving me nuts.

Any help would be greatly appreciated.
Reply
#2
i dont see any errors in this for as far as i know anything about programming
guess your going good so far
Reply
#3
Let's start off with some basic maths with circuits:

If the resistances are in a series, they have:
R[SIZE="1"]s[/SIZE] = R[SIZE="1"]1[/SIZE] + R[SIZE="1"]2[/SIZE] + R[SIZE="1"]3[/SIZE] + ... + R[SIZE="1"]n[/SIZE]

 Series

If they are in a parallel, they are:
1/R[SIZE="1"]p[/SIZE] = 1/R[SIZE="1"]1[/SIZE] + 1/R[SIZE="1"]2[/SIZE] + 1/R[SIZE="1"]3[/SIZE] + ... + 1/R[SIZE="1"]n[/SIZE]

I'd really recommend something here. Get a fraction-class to do the parallel.

 Parallel

Well, theory's okay. Right? I'd suppose this problem occurs for your code:

 Combination

How to create a function which first calculates the parallel and then the parallel's value + the other one?

Well, parallel is first off, summing the reciprocals of the resistances in the parallel together, then take the reciprocal of that value.

Now, take that reciprocal and add it with the other resistance, then you get the total resistance, ya?

Hmm. What I'm thinking about, is making the thing like this:
Make a list with the resistances. To get parallels and series together, you'll most likely use a 2-dimensional list.
Example:
We have a combination like this:
(1, 4, 9)
(5, 4)
(6)

Where the parentheses are in series, and the rows are in parallel. A simple function to do this, can be made up by this:
Code:
//list[x][y];
sum = 0;
for (x = 0; x < length(list); x++){
    xsum = 0;
    for (y in list[x]{
        xsum += y;
    }
    sum += reciprocal(xsum);
}
return reciprocal(sum);

So far, so good.
Now, let's say there's a parallel within the series, like this:
(1, x, 9)
(5, 4)
(6)
where x is:
(2 + 3)
(5)
(7)
(11)

Now, we have to calculate x whenever we come to that value. Do that by the following:

Code:
//list[x][y];
function calculate_resistance(list){
sum = 0;
for (x = 0; x < length(list); x++){
    xsum = 0;
    for (y in list[x]{
        if (is_list(y))
            xsum += calculate_resistance(y);
        else
            xsum += y;
    }
    sum += reciprocal(xsum);
}
return reciprocal(sum);
}

I'm unsure on how to apply this to Java however. Can an array/ArrayList contain two different elements?

Edit: Interesting work though. I'll check it out how I'd fix this in CLISP, then add it in a spoiler when done. If you know CLISP, that is.
Reply
#4
Really good explanations, thanks. But... I don't need to code any functions. The ONLY thing that I need to do is design the class hierarchy and its methods.
Reply
#5
Conciente Wrote:Really good explanations, thanks. But... I don't need to code any functions. The ONLY thing that I need to do is design the class hierarchy and its methods.

Okay. Flashback, and I looked through your code properly with my lost knowledge. I'm unsure if the circuit-class should be defined as abstract or not. I think it should, as the resistance();-function is abstract, right?

That's the only "major" error I saw here. Apart from that, indentation, capital letter at start of classnames and similar are just minor errors that shouldn't make changes in the code, though it would be nice to have it looking pretty. Here's what I would change to the code:
 Spoiler

Edit:
 LISP
Reply
#6
I've never heard of LISP before... lol. I guess you did my homework without any trouble. <_<

B > your knowledge.
Reply
#7
Conciente Wrote:I've never heard of LISP before... lol. I guess you did my homework without any trouble. <_<

B > your knowledge.

LISP is basically, uh, old programming language. Some says you can compare it to Latin, though it may seem like Greek to others. Very easy to recognize due to its insane amount of parentheses. (((((((((())))))))))

And ugh, I try to avoid doing hw for others Eek At least I hope you understood what I tried to explain and such.
Reply
#8
I think I'd stick to 2-element or basic resistors.

Eg.

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;
}
}

I think that's what the question is asking for. A circuit would just be an instance of a series resistor, possibly with 1 element.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)