Skip to content
Snippets Groups Projects
Commit e85b3b4e authored by Goik Martin's avatar Goik Martin
Browse files

Cosmetics

parent 1f8640cb
No related branches found
No related tags found
No related merge requests found
......@@ -334,7 +334,7 @@ public class CircleAreaCalculator {
<para>Representing the above string in a <xref
linkend="glo_Java"/> program may be cumbersome due to its
excessive length discouraging a single line representation.
You may favor decomposing it to a set of smaller string
You may favor assembling it from smaller string
literals:</para>
<programlisting language="java">final String input = "731671765 ... 94934"
......@@ -446,29 +446,6 @@ public class CircleAreaCalculator {
}
}</programlisting>
<para>Remark: Since both Unicode and <xref linkend="glo_ASCII"/>
represent digits by 10 successive integer values we may as well
choose the following implementation:</para>
<programlisting language="java"> private static int getDigitValue (final char digit) {
switch(digit) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default:
System.err.println("Character '" + digit + "' is no digit, exiting");
System.exit(1);
return 0;
}
}</programlisting>
<para>Next we must compute a given string's product of digits.
Taking the first value <code>7316717653133</code> from above we
are looking for 7 × 3 ×1 × 6 ×7 × 1 ×7 × 6 × 5 × 3 × 1 × 3 × 3 =
......@@ -509,7 +486,7 @@ public class CircleAreaCalculator {
return product;
}</programlisting>
<para>Putting these pieces together leaves us with the following
<para>Assembling these pieces leaves us with the following
<code>main(...)</code> method:</para>
<programlisting language="java"> public static void main(String[] args) {
......@@ -540,16 +517,17 @@ public class CircleAreaCalculator {
final int numOfDigits = 13; // The intended number of adjacent digits
long maximumDigitProduct = 0;
String maxDigitString = null;
for (int i = 0; i &lt; input.length() - numOfDigits + 1; i++) {
final String fourDigitWord = input.substring(i, i + numOfDigits);
final long productOfDigits = getDigitProduct(fourDigitWord);
final String digitWord = input.substring(i, i + numOfDigits);
final long productOfDigits = getDigitProduct(digitWord);
if (maximumDigitProduct &lt; productOfDigits) {
maximumDigitProduct = productOfDigits;
maxDigitString = digitWord;
}
}
System.out.println("The largest product of " + numOfDigits + " digits in
succession is " + maximumDigitProduct + ".");
System.out.println("The substring '" + maxDigitString +
"' yields the largest product value " + getDigitProduct(maxDigitString) + ".");
}</programlisting>
<para>The largest product of 13 successive digits stems from the
......@@ -558,7 +536,7 @@ public class CircleAreaCalculator {
digits in the fourth definition line of <code>String input =
...</code>.</para>
<programlisting language="none">The largest product of 13 digits in succession is 23514624000.</programlisting>
<programlisting language="none">The substring '5576689664895' yields the largest product value 23514624000.</programlisting>
</answer>
</qandaentry>
</qandadiv>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment