Last active
December 21, 2015 14:26
-
-
Save cobratbq/9a5388f1a1f6fa54601a to your computer and use it in GitHub Desktop.
Example 3: Third component using expectations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package thirdcomponent; | |
import java.math.BigDecimal; | |
import java.math.MathContext; | |
public class ExpectationsExample { | |
public static void main(String[] args) { | |
final BigDecimal one = BigDecimal.ONE; | |
final BigDecimal three = BigDecimal.valueOf(3L); | |
final ExpectationsExample comp = new ExpectationsExample( | |
MathContext.DECIMAL32); | |
System.out.println(comp.calculate(one, three)); | |
// prints 0.3333333 | |
} | |
// provided, external choice | |
// final as we don't allow ad hoc changes | |
// value is dictated by environment, not managed by instance | |
private final MathContext c; | |
public ExpectationsExample(MathContext mc) { | |
this.c = mc; | |
} | |
public BigDecimal calculate(BigDecimal x, BigDecimal y) { | |
return x.divide(y, c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment