diff --git a/source/ap-java-cheatsheet.ptx b/source/ap-java-cheatsheet.ptx index f272981..7e8f94a 100644 --- a/source/ap-java-cheatsheet.ptx +++ b/source/ap-java-cheatsheet.ptx @@ -10,7 +10,7 @@ The following is intended to be useful in better understanding Java functions coming from a Python background.

- + Function/Method Equivalents: Python to Java @@ -201,5 +201,4 @@

- \ No newline at end of file diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 4adb2ff..5042038 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -628,6 +628,24 @@ public class Fraction { +

+ If you ran , you probably noticed that the output is not very satisfying. Chances are your output looked something like . +

+ + + + +Fraction@6ff3c5b5 + + + + +

+ The reason is that we have not yet provided a friendly string representation for our Fraction objects. + Just like in Python, whenever an object is printed by the println method it must be converted to string format. + In Python you can control how that looks by writing an __str__ method for your class. + If you do not then you will get the default, which looks something like . We will see how to provide a friendly string representation for our Fraction class in . +

@@ -672,35 +690,11 @@ public class Fraction { - -
Inheritance - -

- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like . -

- - - - -Fraction@6ff3c5b5 - - - - -

- The reason is that we have not yet provided a friendly string representation for our Fraction objects. - Just like in Python, whenever an object is printed by the println method it must be converted to string format. - In Python you can control how that looks by writing an __str__ method for your class. - If you do not then you will get the default, which looks something like the above. -

-
- - The <c>Object</c> Class @@ -961,6 +955,38 @@ public void test(Number a, Number b) { You will still get this error even if all your code that calls this test method passes two Fractions as parameters (remember that Fraction does implement add).

+ + + +

+ Construct the toString method for the Fraction class so that printing a + fraction shows it in the form numerator/denominator. Drag the blocks into the correct order on the right. +

+
+ + + + public String toString() { + + + public void toString() { + + + + + + return numerator.toString() + "/" + denominator.toString(); + + + numerator.toString() + "/" + denominator.toString(); + + + + + } + + +