Reputation: 29
I have constructed a class that will compute an equation based on private instance variables.
These variables may be updated using getter and setter methods, the latter of which are called and changed from a separate control class within the project (I am using Eclipse if that is relavant)
I need to ensure that the constructor uses the updated values when passed from the separate control class to the setter methods in computing variable c.
Here is snipets of my code.
instance variables
private double a;
private double b;
private double c;
constructor
public Prob(){
a = 4; //must be first initialized with these values then later changed from the control class
b = 10;
setA(a);
setB(b);
setC();
setter methods
public void setA(double a){
if(isValidA(a))
this.a = a;
else this.a = 100; // 100 is meaningless and allows execution of "invalid" string
}
public void setB(double b){
if(isValidB(b))
this.b = b;
else this.b = -1; // -1 is meaningless, allows execution "invalid entry" code in string
}
private void setC(){
if(a<=5 && b>=2)
c = (int)(5 + 0.5*a - 5*(Math.pow(b, 0.2)) + 0.5*(a)*b;
else c = a;
}
I can update variable a or variable b from the control class but how can I get variable c to update as well based on the current a and b?
Upvotes: 0
Views: 5169
Reputation: 393956
If c depends on a and b, you should either call setC
from setA
and setB
, or call setC
after each time you call setA
or setB
.
The former would be cleaner to implement, but may cause redundant calculations (if you update both a and b at the same time, it would call setC twice).
The latter would be more efficient (since if you call both setA
and setB
, you can explicitelly call setC
once after these two calls), but more bug prone (since you might forget to call setC
in some of the places you call setA
or setB
).
Another option, which I think is the best, is to calculate c on demand in getC, and cache the result (so that following calls to getC
won't have to recalculate its value, unless a or b are changed). In this case you don't need setC
. You'll need to add a boolean member cIsValid
that would be an indicator of whether the cached value of c is valid.
public double getC ()
{
if (!cIsValid) {
if(a<=5 && b>=2)
c = (int)(5 + 0.5*a - 5*(Math.pow(b, 0.2)) + 0.5*(a)*b;
else
c = a;
}
cIsValid = true;
return c;
}
And in setA
and setB
you invalidate the cache by setting cIsValid to false :
public void setA(double a)
{
if(isValidA(a))
this.a = a;
else
this.a = 100; // 100 is meaningless and allows execution of "invalid" string
cIsValid = false;
}
public void setB(double b)
{
if(isValidB(b))
this.b = b;
else
this.a = -1; // -1 is meaningless, allows execution "invalid entry" code in string
cIsValid = false;
}
Upvotes: 0
Reputation: 201497
If I understand your question, then you could setA
and setB
like so
public void setA(double a) {
this.a = (isValidA(a)) ? a : 100;
setC(); // <-- call setC();
}
public void setB(double b) {
this.b = (isValidB(b)) ? b : -1;
setC(); // <-- call setC();
}
Upvotes: 2