15-110 Sections M-Q / Fall 2009 / Quiz 7
7 Questions / 20 Minutes
·
Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.
·
The “Fraction” class refers to the one we wrote in class. It
has two instance variables – num and den. You may also assume that the gcd
helper method is written and works properly.
- In just a few words, explain how the Parity Card Trick
works.
- Write a constructor for our Fraction class that takes
a numerator and a denominator.
- In just a few words, why do we have public mutators
rather than public instance variables?
- In just a few words, explain the difference between
“num” and “this.num”.
- In just a few words, explain why we had to write our
own equals method for the Fraction class.
- Write a “toString” method for our Fraction class.
- Write a “times” method for our Fraction class – it
takes another Fraction and returns the product of the two Fractions.
- Bonus/Optional: Java includes an Arrays.sort method
that works over arrays of arbitrary Objects. Even so, as we wrote the
Fraction class, an array of Fraction objects would not be properly sorted by
this method. In fact, an exception would be thrown if we tried. Speculate
what the cause of that exception might be and how we might go about fixing
it. (You do not have to be exactly correct to receive some bonus points.)