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


Messages In This Thread
[Java] Class Hierarchy Design - by Conciente - 2009-01-30, 10:31 AM
[Java] Class Hierarchy Design - by ShadowFusion - 2009-01-30, 12:01 PM
[Java] Class Hierarchy Design - by Nikkey - 2009-01-30, 12:22 PM
[Java] Class Hierarchy Design - by Conciente - 2009-01-30, 01:02 PM
[Java] Class Hierarchy Design - by Nikkey - 2009-01-30, 02:16 PM
[Java] Class Hierarchy Design - by Conciente - 2009-01-30, 04:34 PM
[Java] Class Hierarchy Design - by Nikkey - 2009-01-30, 06:39 PM
[Java] Class Hierarchy Design - by Stereo - 2009-01-30, 09:23 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)