Section 13.9 Check Point Questions7 questions
13.9.1
Show the output of the following code?
Rational r1 = new Rational(-2, 6); System.out.println(r1.getNumerator()); System.out.println(r1.getDenominator()); System.out.println(r1.intValue()); System.out.println(r1.doubleValue());
13.9.2
Why is the following code wrong?
Rational r1 = new Rational(-2, 6); Object r2 = new Rational(1, 45); System.out.println(r2.compareTo(r1));
13.9.3
Why is the following code wrong?
Object r1 = new Rational(-2, 6); Rational r2 = new Rational(1, 45); System.out.println(r2.compareTo(r1));
13.9.4
Simplify the code in lines 82-85 in
LiveExample 13.13, Rational.java, using one line of code without
using the if statement. Simplify the code in lines 110-115 using a conditional operator.
13.9.5
Trace the program carefully and show the output of the following code.
Rational r1 = new Rational(1, 2); Rational r2 = new Rational(1, -2); System.out.println(r1.add(r2));
13.9.6
The preceding question shows a bug in the toString method.
Revise the toString() method to fix the error.
13.9.7
What happens if you create a Rational using new Rational(1, 0)?
Discuss the appropriate ways for handling this case.