diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 16547f03284a2334c1798376827632f3b190fb30..2b6860ed0dc76f04617869d92ffe23cf1534e051 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -1588,34 +1588,117 @@ public class SetterAccess { <informaltable border="1"> <tr> - <td valign="top"><programlisting language="java">package only_minute.direct; + <td valign="top"><programlisting language="java">package direct.access; public class Time { // Minutes since 00:00 public int minute; -}</programlisting><programlisting language="java">package only_minute; -import only_minute.direct.Time; +}</programlisting><programlisting language="java">import direct.access.Time; public class DirectAccess { - Time time = new Time(); + final Time time = new Time(); void init() { - <emphasis role="red">time.minute = 1065;</emphasis>// 17:45 + <emphasis role="red">time.minute = 720;</emphasis>// 12:00 } }</programlisting></td> - <td valign="top"><programlisting language="java">package only_minute.setter; + <td valign="top"><programlisting language="java">package access.by.setter; public class Time { - private int minute; - public void setTime(int h, int m) { + private int minute; // Minutes since 00:00 + public void set(int h, int m) { minute = m + 60 * h; } -}</programlisting><programlisting language="java">package only_minute; -import only_minute.setter.Time; + public int getMinute(){...} // 0 to 59 + public int getHour(){...} // 0 to 23 +}</programlisting><programlisting language="java">import access.by.setter.Time; public class SetterAccess { - Time time = new Time(); - void init() { <emphasis role="red">time.setTime(0, 0);</emphasis>} + final Time time = new Time(); + void init() { <emphasis role="red">time.setTime(12, 0);</emphasis>} }</programlisting></td> </tr> </informaltable> </figure> + + <qandaset defaultlabel="qanda" + xml:id="sd1_sect_getterSetter_qandaTimeDirectgetterSetter"> + <title>Implementing getter methods</title> + + <qandadiv> + <qandaentry> + <question> + <para>We reconsider the getter/setter based implementation in + <xref linkend="sd1_fig_implementJustSeconds"/>:</para> + + <programlisting language="java">public class Time { + private int minute; // Minutes since 00:00 + public void set(int h, int m) { + minute = m + 60 * h; + } + + /** + * Getting the current time's minute portion. Example: At 17:45 a value of + * 45 will be returned. + * + * @return The current time's minute portion. + */ + public int getMinute(){ + ... // implement me + return ...; + } + + /** + * Getting the current time's hour portion. Example: At 17:45 a value of + * 17 will be returned. + * + * @return The current time's hour portion. + */ + public int getHour(){ + ... // implement me + return ...; + } +}</programlisting> + + <para>Complete the two getter methods' implementation.</para> + + <tip> + <para>Keep in mind a day's duration being 86400 the <code + language="java">private</code> variable <code + language="java">minute</code> is ranging from 0 to + 86399.</para> + </tip> + </question> + + <answer> + <programlisting language="java">package access.by.setter; + +public class Time { + private int minute; // Minutes since 00:00 + public void set(int h, int m) { + minute = m + 60 * h; + } + + /** + * Getting the current time's minute portion. Example: At 17:45 a value of + * 45 will be returned. + * + * @return The current time's minute portion. + */ + public int getMinute(){ + return minute % 60; // 1 hour equals 60 minutes + } + + /** + * Getting the current time's hour portion. Example: At 17:45 a value of + * 17 will be returned. + * + * @return The current time's hour portion. + */ + public int getHour(){ + return minute / 60; // 1 hour equals 60 minutes + } +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </section> <section xml:id="sd1_typeSignature">