Newer
Older
BigDecimal result = zero_dot_99.
subtract(zero_dot_1).
subtract(zero_dot_1).
subtract(zero_dot_1);
System.out.println(result);</programlisting>
<screen>0.69</screen>
</figure>
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
<qandaset defaultlabel="qanda"
xml:id="sd1_workingWithNumbers_BigDecimalChaining">
<title>Chaining subtract method calls</title>
<qandadiv>
<qandaentry>
<question>
<para>Explain the chaining mechanism implementing three successive
subtractions in <xref
linkend="sd1_numbers_fig_usingBigDecimalChaining"/>.</para>
</question>
<answer>
<para>We may re-write the statement in question:</para>
<programlisting language="none">result = zero_dot_99.subtract(zero_dot_1).subtract(zero_dot_1).subtract(zero_dot_1);
\ / / /
\ / / /
\ / / /
0.99 - 0.1 / /
\ / /
\ / /
\ / /
0.98 - 0.1 /
\ /
\ /
0.97 - 0.1
== 0.96</programlisting>
<para>Each <methodname>subtract(...)</methodname> call returns a
new result instance of class <classname
xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html">BigDecimal</classname>.
We can thus chain a subsequent
<methodname>subtract(...)</methodname> call using this returned
instance.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<figure xml:id="sd1_numbers_fig_usingBigDecimalFeatures">
<title><classname
xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html">BigDecimal</classname>
features</title>
<itemizedlist>
<listitem>
<para>Higher memory allocation hosting higher precision.</para>
</listitem>
<listitem>
<para>Immutable instances</para>
</listitem>
<listitem>
<para>Calculation performance penalty.</para>
</listitem>
<listitem>
<para>Clumsy interface.</para>
</listitem>
</itemizedlist>
</figure>
</section>
<section xml:id="sd1_numbers_sect_GeneratingRandomNumbers">
<title>Generating Random Numbers</title>
<figure xml:id="sd1_numbers_fig_random">
<title>Using <methodname
xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html#random()">static
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
double random()</methodname></title>
<programlisting language="java">for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}</programlisting>
<screen>0.4754286988826202
0.0060114391743414375
...
0.9047785351372987
0.2516070321935864</screen>
</figure>
<figure xml:id="sd1_numbers_fig_randomSeed">
<title>Seeding a pseudo random generator</title>
<programlisting language="java">try(final Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter an integer seed:");
final long seed = scanner.nextLong();
Random generator = new Random(seed);
for (int i = 0; i < 10; i++) {
System.out.print(generator.nextBoolean() + " ");
}
}</programlisting>
<screen>Enter an integer seed:4237549835735
false true true true false false false true false true</screen>