Problem

(Use BigIntegerfor the Rationalclass) Redesign and implement the Rational class in Listing...

(Use BigIntegerfor the Rationalclass) Redesign and implement the Rational class in Listing 1 using BigInteger for the numerator and denominator.

LISTING 1 Rational.java

1 public class Rational extends Number implements Comparable {

2 // Data fields for numerator and denominator

3 private long numerator = 0;

4 private long denominator = 1;

5

6 /** Construct a rational with default properties */

7 public Rational() {

8 this(0, 1);

9 }

10

11 /** Construct a rational with specified numerator and denominator */

12 public Rational(long numerator, long denominator) {

13 long gcd = gcd(numerator, denominator);

14 this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd;

15 this.denominator = Math.abs(denominator) / gcd;

16 }

17

18 /** Find GCD of two numbers */

19 private static long gcd(long n, long d) {

20 long n1 = Math.abs(n);

21 long n2 = Math.abs(d);

22 int gcd = 1;

23

24 for (int k = 1; k <= n1 && k <= n2; k++) {

25 if (n1 % k == 0 && n2 % k == 0)

26 gcd = k;

27 }

28

29 return gcd;

30 }

31

32 /** Return numerator */

33 public long getNumerator() {

34 return numerator;

35 }

36

37 /** Return denominator */

38 public long getDenominator() {

39 return denominator;

40 }

41

42 /** Add a rational number to this rational */

43 public Rational add(Rational secondRational) {

44 long n = numerator * secondRational.getDenominator() +

45 denominator * secondRational.getNumerator();

46 long d = denominator * secondRational.getDenominator();

47 return new Rational(n, d);

48 }

49

50 /** Subtract a rational number from this rational */

51 public Rational subtract(Rational secondRational) {

52 long n = numerator * secondRational.getDenominator()

53 - denominator * secondRational.getNumerator();

54 long d = denominator * secondRational.getDenominator();

55 return new Rational(n, d);

56 }

57

58 /** Multiply a rational number by this rational */

59 public Rational multiply(Rational secondRational) {

60 long n = numerator * secondRational.getNumerator();

61 long d = denominator * secondRational.getDenominator();

62 return new Rational(n, d);

63 }

64

65 /** Divide a rational number by this rational */

66 public Rational divide(Rational secondRational) {

67 long n = numerator * secondRational.getDenominator();

68 long d = denominator * secondRational.numerator;

69 return new Rational(n, d);

70 }

71

72 @Override

73 public String toString() {

74 if (denominator == 1)

75 return numerator + "";

76 else

77 return numerator + "/" + denominator;

78 }

79

80 @Override // Override the equals method in the Object class

81 public boolean equals(Object other) {

82 if ((this.subtract((Rational)(other))).getNumerator() == 0)

83 return true;

84 else

85 return false;

86 }

87

88 @Override // Implement the abstract intValue method in Number

89 public int intValue() {

90 return (int)doubleValue();

91 }

92

93 @Override // Implement the abstract floatValue method in Number

94 public float floatValue() {

95 return (float)doubleValue();

96 }

97

98 @Override // Implement the doubleValue method in Number

99 public double doubleValue() {

100 return numerator * 1.0 / denominator;

101 }

102

103 @Override // Implement the abstract longValue method in Number

104 public long longValue() {

105 return (long)doubleValue();

106 }

107

108 @Override // Implement the compareTo method in Comparable

109 public int compareTo(Rational o) {

110 if (this.subtract(o).getNumerator() > 0)

111 return 1;

112 else if (this.subtract(o).getNumerator()<0)

113 return -1;

114 else

115 return 0;

116 }

117 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT