diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 2c10a085463ca1f08e35127f5fc4fd103fb9dab4..1490b4c16c224eb50cab08ff4d4f868de5c16219 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -1560,19 +1560,19 @@ public void writeSvg() { }</programlisting></td> </tr> </informaltable> + </figure> - <calloutlist> - <callout arearefs="sd1_callout_publicPrivateInterface-1-co" - xml:id="sd1_callout_publicPrivateInterface-1"> - <para>Private attributes only accessible inside class.</para> - </callout> + <calloutlist> + <callout arearefs="sd1_callout_publicPrivateInterface-1-co" + xml:id="sd1_callout_publicPrivateInterface-1"> + <para>Private attributes only accessible inside class.</para> + </callout> - <callout arearefs="sd1_callout_publicPrivateInterface-2-co" - xml:id="sd1_callout_publicPrivateInterface-2"> - <para>Public interface accessible by arbitrary classes.</para> - </callout> - </calloutlist> - </figure> + <callout arearefs="sd1_callout_publicPrivateInterface-2-co" + xml:id="sd1_callout_publicPrivateInterface-2"> + <para>Public interface accessible by arbitrary classes.</para> + </callout> + </calloutlist> <figure xml:id="sd1_fig_timeAccessViolation"> <title>Access violation</title> @@ -1609,11 +1609,11 @@ public void writeSvg() { <td>Other package</td> + <td>Child class</td> + <td>Same package</td> <td>Same class</td> - - <td>Child class</td> </tr> <tr> @@ -1645,11 +1645,11 @@ public void writeSvg() { <td><emphasis role="red">no</emphasis></td> - <td>yes</td> + <td><emphasis role="red">no</emphasis></td> <td>yes</td> - <td><emphasis role="red">no</emphasis></td> + <td>yes</td> </tr> <tr> @@ -1659,9 +1659,9 @@ public void writeSvg() { <td><emphasis role="red">no</emphasis></td> - <td>yes</td> - <td><emphasis role="red">no</emphasis></td> + + <td>yes</td> </tr> </informaltable> </figure> @@ -1683,8 +1683,8 @@ public void writeSvg() { <listitem> <para>Avoid public fields except for constants. Public fields tend - to link you to a particular implementation and limit your - flexibility in changing your code.</para> + linking to a particular implementation and limit your flexibility + in changing your code.</para> </listitem> </itemizedlist> </figure> @@ -2201,7 +2201,7 @@ public class SetterAccess { <figure xml:id="sd1_fig_overloadingPrinciple"> <title>What is overloading?</title> - <para>Identical method name, differing argument signature:</para> + <para>Identical method name, differing method signature:</para> <informaltable border="0"> <tr> @@ -2311,7 +2311,8 @@ void main(void) { </tr> </informaltable> - <para>Return type irrelevant with respect to method signatures.</para> + <para>Return type irrelevant, only method signatures in <xref + linkend="glo_Java"/>.</para> </figure> <figure xml:id="sd1_fig_methodSignatureRatio"> @@ -2337,7 +2338,7 @@ void main(void) { </figure> <figure xml:id="sd1_fig_sysoutPrintlnOverload"> - <title>System.out.print(...)</title> + <title>Example: System.out.print(...)</title> <informaltable border="0"> <tr> @@ -2433,7 +2434,7 @@ a = 33;</programlisting></td> public <co linkends="sd1_callout_defineConstructor-1" xml:id="sd1_callout_defineConstructor-1-co"/> Rectangle <co linkends="sd1_callout_defineConstructor-2" - xml:id="sd1_callout_defineConstructor-2-co"/> (int width, int height, boolean hasSolidBorder){ + xml:id="sd1_callout_defineConstructor-2-co"/> (int width, int height, boolean hasSolidBorder){ this.width = width; this.height = height; this.hasSolidBorder = hasSolidBorder; @@ -2539,7 +2540,7 @@ a = 33;</programlisting></td> <tr> <td valign="top"><programlisting language="java">Rectangle standard = new Rectangle(); // 1 x 1 Rectangle sqare = new Rectangle(2); // 2 x 2 -Rectangle individual = new Rectangle(2, 7); // 2 x 7 </programlisting></td> +Rectangle individual = new Rectangle(2, 7); // 2 x 7 </programlisting></td> <td valign="top"><mediaobject> <imageobject> @@ -3099,7 +3100,7 @@ public class Driver { </listitem> <listitem> - <para>The club's member count needs to be accounted + <para>The overall club's member count needs to be accounted for.</para> </listitem> </itemizedlist> @@ -3107,19 +3108,27 @@ public class Driver { </glossentry> <glossentry> - <glossterm>Solution</glossterm> + <glossterm>Solution:</glossterm> <glossdef> - <itemizedlist> - <listitem> - <para>Keep track of the overall member count.</para> - </listitem> + <glosslist> + <glossentry> + <glossterm>Class level:</glossterm> - <listitem> - <para>Assign the current member count to each new - instance.</para> - </listitem> - </itemizedlist> + <glossdef> + <para>Keep track of the overall member count.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Instance level:</glossterm> + + <glossdef> + <para>Assign the current member count to each new + instance.</para> + </glossdef> + </glossentry> + </glosslist> </glossdef> </glossentry> </glosslist> diff --git a/P/Sd1/Binomial/V1/.gitignore b/P/Sd1/Binomial/V1/.gitignore index cf5fd6bb26d6450c8c63fb34295192784943cf5e..39b3065c8a91f71209ee92289bff47091449a298 100644 --- a/P/Sd1/Binomial/V1/.gitignore +++ b/P/Sd1/Binomial/V1/.gitignore @@ -3,3 +3,5 @@ /.classpath /.project /A1.log +/.idea +/*.iml diff --git a/P/Sd1/Binomial/V1/src/main/java/de/hdm_stuttgart/mi/sd1/binomial/Binomial.java b/P/Sd1/Binomial/V1/src/main/java/de/hdm_stuttgart/mi/sd1/binomial/Binomial.java index e91f421480fb750738a1879f7bdc8317afd3dc46..7f12c9156405c40f3382de283aff5296918c2f83 100644 --- a/P/Sd1/Binomial/V1/src/main/java/de/hdm_stuttgart/mi/sd1/binomial/Binomial.java +++ b/P/Sd1/Binomial/V1/src/main/java/de/hdm_stuttgart/mi/sd1/binomial/Binomial.java @@ -1,32 +1,50 @@ package de.hdm_stuttgart.mi.sd1.binomial; +/** + * Calculating <a href="https://en.wikipedia.org/wiki/Binomial">binomials</a>. + */ public class Binomial { + public final static int ILLEGAL_ARGUMENTS = -1; + + /** + * Calculating binomials. Example: + * + * 5 5! 5*4*3*2*1 + * ( ) = ------------ = --------------------- = 10 + * 2 2! (5 - 2)! 3 * 2 * 1 * 2 * 1 + * + * Preconditons: 0 <= k and k <= n + * + * @param n + * @param k + * @return The binomial's value or {@link #ILLEGAL_ARGUMENTS} if preconditions are not fulfilled. + */ + public static long binomial(int n, int k) { + + // Handle preconditions / illegal arguments + if (k < 0 || n < k ) { + return ILLEGAL_ARGUMENTS; + } // Trying to avoid arithmetic overflow by making use of: // n n // ( ) = ( ) // k n-k // - if (n / 2 < k) { + if (n - k < k) { k = n - k; } - // Numerator product n (n - 1) ... (n - k + 1) - long numerator = 1; + // Numerator product (n - k + 1) ... (n - 1) n + long result = 1; for (int i = n - k + 1; i <= n; i++) { - numerator *= i; + result *= i; } - return numerator / factorial(k); - } - - public static long factorial(int n) { - long ret = 1; - - for (int i = 2; i <= n; i++) { - ret *= i; + for (int i = 2; i <= k; i++) { + result /= i; } - return ret; + return result; } }