From 1a6e9bca9e19e02fcb11aec5365e27bc470e3dc2 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Wed, 16 Apr 2014 00:18:20 +0200 Subject: [PATCH] various exercises --- Sd1/swd1.xml | 271 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 265 insertions(+), 6 deletions(-) diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index 297905daa..40efb9d9a 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -1766,6 +1766,10 @@ public void writeSvg() { </glossdef> </glossentry> </glosslist> + + <para>For both categories see <xref linkend="bibHorton2011"/>, + chapter 5, <quote>Fields in a Class Definition</quote> and + <quote>Methods in a Class Definition</quote>.</para> </glossdef> </glossentry> @@ -1793,7 +1797,7 @@ public void writeSvg() { <programlisting language="java">public class Konto { ... - private double stand; <emphasis role="bold">// variable stand being shadowed inside setStand(...)</emphasis> + private double stand; <emphasis role="bold">// variable "stand" being shadowed inside body of setStand(...)</emphasis> ... public void setStand(double stand) { if (stand <= 10000) { @@ -1807,22 +1811,277 @@ public void writeSvg() { }</programlisting> </glossdef> </glossentry> + + <glossentry> + <glossterm>Access restrictions public / private / protected to + attributes and methods</glossterm> + + <glossdef> + <programlisting>public class Konto { + + private static double // Common interestrate for all accounts being declared + guthabenZinssatz = 1.5; // once per class rather than per instance. +... + public void verzinsung() { + stand = stand * (1 + guthabenZinssatz / 100); + } +...</programlisting> + + <para>See <xref linkend="bibHorton2011"/>, chapter 5, + <quote>CONTROLLING ACCESS TO CLASS MEMBERS</quote>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Method overloading</glossterm> + + <glossdef> + <para>Example:</para> + + <programlisting>public class Konto { + + public Konto() { // Default constructor without any parameter + setStand(0); + } +... + public Konto(double stand) { // Non-default constructor defining an account's + setStand(stand); // initial value. + } +... + public void verzinsung() { // Just one year. + stand = stand * (1 + guthabenZinssatz / 100); + } +... + public void verzinsung(int jahre) { // Arbitrary number of years. + stand = stand * Math.pow((1 + guthabenZinssatz / 100), jahre) ; // raised to the power of jahre, see javadoc comment. + } +... +}</programlisting> + + <para>See <xref linkend="bibHorton2011"/>, chapter 5, <quote>METHOD + OVERLOADING</quote>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Use of standard mathematical functions</glossterm> + + <glossdef> + <programlisting>Math.pow((1 + guthabenZinssatz / 100), jahre)</programlisting> + + <para>See <xref linkend="bibHorton2011"/>, chapter 2, + <quote>MATHEMATICAL FUNCTIONS AND CONSTANTS</quote>.</para> + </glossdef> + </glossentry> </glosslist> </chapter> - <chapter xml:id="sd1CrabsFinish"> - <title>Lecture 6 - Finishing the crab scenario</title> + <chapter xml:id="sd1Variables"> + <title>Lecture 6 - Variables</title> <itemizedlist> <listitem> - <para>Read chapter 3 of <xref linkend="bibKoelling2010Ger"/>.</para> + <para>Read Chapter 2 from <xref linkend="bibHorton2011"/> till + <quote>Mathematical Functions and constants</quote>.</para> </listitem> <listitem> - <para>Read Chapter 2 from <xref linkend="bibHorton2011"/> till - <quote>Mathematical Functions and constants</quote>.</para> + <para>Read chapter 3 of <xref linkend="bibKoelling2010Ger"/>.</para> </listitem> </itemizedlist> + + <section xml:id="sd1VariableExercises"> + <title>Exercises</title> + + <qandaset defaultlabel="qanda" xml:id="sd1VariableComplexExpression"> + <title>Programmers favourite expression</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code fragment:</para> + + <programlisting> int a = 6, + b = 7, + c = -3, + result = 0; + + result += ++a - b++ + --c;</programlisting> + + <para>Rewrite this code by decomposing the last line into + several lines to make the code easier to understand. Hint: After + execution all variable shall have identical values.</para> + </question> + + <answer> + <para>Incrementing <code>a</code> and decrementing + <code>c</code> happens prior to adding their values to the + variable <code>result</code>. The increment operation of + <code>b</code> in contrast happens after being being subtracted + from result. This determines a possible decomposition:</para> + + <programlisting> int a = 6, + b = 7, + c = -3, + result = 0; + ++a; + --c; + result += a - b + c; + b++;</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaIntOverflow"> + <title>Integer value considerations.</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following piece of code:</para> + + <programlisting>int a = ..., b = ...; +...// some calculations being omitted +int sum = a + b;</programlisting> + + <para>Which problem may arise here? May you supply a + solution?</para> + </question> + + <answer> + <para>The sum of a and b may either exceed + <code>java.lang.Integer.MAX_VALUE</code> or in turn may be less + than <code>java.lang.Integer.MIN_VALUE</code>. To avoid this + type of overflow error our variable <code>sum</code> may be + declared as long:</para> + + <programlisting>int a = ..., b = ...; +...// some calculations being omitted +long sum = a + b;</programlisting> + + <para>Unfortunately this does not (yet) help at all: Since both + operands <code>a</code> and <code>b</code> are of type + <code>int</code> the expression <code>a + b</code> is also of + type int. The only way to solve this problem is by casting at + least one operand to <code>long</code>:</para> + + <programlisting>int a = ..., b = ...; +...// some calculations being omitted +long sum = (long)a + b;</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sde1QandaFraction"> + <title>A class representing fractions</title> + + <qandadiv> + <qandaentry> + <question> + <para>Implement a class representing fractions. The desired + interface is being described in the subfolder + Fraction/index.html below the <link + xlink:href="https://cloud.mi.hdm-stuttgart.de/owncloud/public.php?service=files&t=df9f296af3298f96361a15a679390e59">set + of additional lecture resources</link>. The following code + snippet illustrates the possible usage:</para> + + <programlisting> Fraction twoThird = new Fraction(2, 3), // Construct a fraction object (2/3) + threeSeven = new Fraction(3, 7); // Construct a fraction object (3/7) + + Fraction sum = twoThird.add(threeSeven), // (2/3) + (3/7) + product = twoThird.mult(threeSeven); // (2/3) * (3/7) + + System.out.println("Sum:" + sum.getValue()); // Expected to be (23/21) + System.out.println("Product:" + product.getValue()); // Expected to be (2/7)</programlisting> + </question> + + <answer> + <programlisting>package math; + +public class Fraction { + + long numerator,denominator; + + /** + * @param numerator + * @param denominator + */ + public Fraction(long numerator, long denominator) { + setNumerator(numerator); + setDenominator(denominator); + } + /** + * @return + * The fraction's decimal value e.g. 1/3 = 0.3333... + */ + public double getValue(){ + return (double) numerator / denominator; + } + /** + * @return + * The fraction's numerator + */ + public long getNumerator() { + return numerator; + } + /** + * Set the fraction's numerator. + * @param numerator + */ + public void setNumerator(long numerator) { + this.numerator = numerator; + } + /** + * @return + * The fraction's denominator value + */ + public long getDenominator() { + return denominator; + } + /** + * Set the fraction's denominator + * @param denominator + */ + public void setDenominator(long denominator) { + this.denominator = denominator; + } + + /** + * Add a second fraction to the current instance. + * + * @param f + * + * @return + * The sum of the current instance and f. + */ + public Fraction add(Fraction f) { + return new Fraction( numerator * f.denominator + denominator * f.numerator, + denominator * f.denominator); + } + /** + * Multiply a second fraction by this instance + * @param f + * @return + * The result of multiplying the current instance by f. + */ + public Fraction mult(Fraction f) { + return new Fraction(numerator * f.numerator, + denominator * f.denominator); + } +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + </chapter> + + <chapter xml:id="sd1L7"> + <title>Lecture 7</title> + + <para/> </chapter> <chapter xml:id="ideas"> -- GitLab