Closed Thread Icon

Topic awaiting preservation: can you optimise this ? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12579" title="Pages that link to Topic awaiting preservation: can you optimise this ? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: can you optimise this ? <span class="small">(Page 1 of 1)</span>\

 
viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 01-16-2003 18:23

I have these three methods in my program, coded in Java:

code:
public boolean condition_1_AlwaysTrue() {
// 'y' remains true as long as 'c1' is true
boolean y = true;
for (int i = 0; i < a.length; i++)
y = y && a[i].c1();
return y;
}

public boolean conditions_1_Or_2_AlwaysTrue() {
// 'y' is true as long as 'c1' OR c2' is true
boolean y = true;
for (int i = 0; i < a.length; i++)
y = y && ( a[i].c1() &#0124; &#0124; a[i].c2() );
return y;
}

public boolean conditions_2_Or_3_AlwaysTrue() {
// 'y' is true as long as 'c2' OR 'c3' is true
boolean y = true;
for (int i = 0; i < a.length; i++)
y = y && ( a[i].c2() &#0124; &#0124; a[i].c3() );
return y;
}



I'd like to consolidate all these methods into a general one, using parameters, so, if I wanted to test for c1 only, I would call the consolidated method like this:

boolean b = consolidatedConditions(true, false, false);

or, if I wanted to test for conditions c2 and c3 I would use something like

boolean b = consolidatedConditions(false, true, true);

or any other way of using parameters that would make a consolidated method to work.

Does anyone know a smart way to do this consolidated method?
All the solutions I've been able to code are awkward and no better than having the three methods apart.
Note that this is NOT part of a homework that I'm asking someone else to do it for me! My Java course has already ended.

[This message has been edited by viol (edited 01-16-2003).]

tomeaglescz
Paranoid (IV) Inmate

From: Czech Republic via Bristol UK
Insane since: Feb 2002

posted posted 01-16-2003 18:29

wouldnt this be better in server side and coding??? likely to get more response there, hehe ini will be along he is your man for this stuff

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-16-2003 19:54

What and where is a?

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 01-16-2003 20:05

If I understood you problem right, this function should solve it. In this function a is an array holding some kind of object that has three functions (c1, c2, c3) that return boolean values.

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>
public static boolean consolidatedConditions(boolean b1, boolean b2, boolean b3) {
boolean y = true;

boolean c1, c2, c3;

for (int i = 0; i < a.length; i++) {
c1 = b1 ? a[i].c1() : b1;
c2 = b2 ? a[i].c2() : b2;
c3 = b3 ? a[i].c3() : b3;

y = y && ( c1

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 01-16-2003 21:07

WarMage - 'a' is an array of objects (some user-defined object). The objects that this array holds have these three methods c1() , c2() & c3() that return boolean values. So, each element of a[] can call any of these three methods.
This is just a snippet from a program that I'm coding.

Veneficuz - thanks. After reading your solution, I figured out that this is also possible:

code:
public static boolean cC(boolean b1, boolean b2, boolean b3) {
boolean y = true;
for (int i = 0; i < a.length; i++) {
y = y && ((a[i].c1() && b1) &#0124; &#0124; a[i].c2() && b2) &#0124; &#0124; a[i].c3() && b3));
}
return y;
}



Thanks again.


Nenhuma situação é tão complicada que uma mulher não possa piorar. (Tom Jobim)
"No situation is tough enough that a woman cannot make it worse."
-- Cell 963 --

« BackwardsOnwards »

Show Forum Drop Down Menu