Skip to content

Commit c25d77c

Browse files
nshizirungudieumerci@gmail.comnshizirungudieumerci@gmail.com
authored andcommitted
added a parsons problem to the inheritance section
1 parent ae339d6 commit c25d77c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎source/ch6_definingclasses.ptx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,38 @@ public void test(Number a, Number b) {
961961
You will <em>still get this error</em> even if all your code that calls this <c>test</c> method passes two <c>Fractions</c> as parameters (remember that <c>Fraction</c> does implement <c>add</c>).
962962
</p>
963963
</subsection>
964+
965+
<exercise xml:id="java-inheritance-parsons-exercise" label="java-inheritance-parsons">
966+
<statement>
967+
<p>
968+
Construct the <c>toString</c> method for the <c>Fraction</c> class so that printing a
969+
fraction shows it in the form numerator/denominator. Drag the blocks into the correct order on the right.
970+
</p>
971+
</statement>
972+
<blocks>
973+
<block order="1">
974+
<choice correct="yes">
975+
<cline>public String toString() {</cline>
976+
</choice>
977+
<choice correct="no">
978+
<cline>public void toString() {</cline>
979+
</choice>
980+
</block>
981+
982+
<block order="2">
983+
<choice correct="yes">
984+
<cline> return numerator.toString() + "/" + denominator.toString();</cline>
985+
</choice>
986+
<choice correct="no">
987+
<cline> numerator.toString() + "/" + denominator.toString();</cline>
988+
</choice>
989+
</block>
990+
991+
<block order="3">
992+
<cline>}</cline>
993+
</block>
994+
</blocks>
995+
</exercise>
964996
</section>
965997

966998
<section xml:id="interfaces">

0 commit comments

Comments
 (0)