1.2.16 rational quantity. A variable data type rational is implemented for rational numbers and supports addition, subtraction, multiplication, and Division operations. No need to test overflow (see exercise 1.2.17). You only need to use two long instance variables to indicate the numerator and denominator to control the possibility of overflow. Euclidean algorithms are used to ensure that there is no common factor between the numerator and denominator. Write a test case to test all your methods.
Public class rational
Rational (INT Numerator. Int denominator)
The sum of the number and B of rational plus (Rational B)
The difference between rational minus (Rational B) and B
Rational Times (Rational B) the product of this number and B
Rational divides (Rational B) vendors of this number and B
Boolean equals (rational that) is the same as that?
String tostring () String Representation of the object
A:
Public class rational
{
Private Final long mynumerator;
Private Final long mydenominator;
Private long gcd (long P, Long Q)
{
If (q = 0) return P;
Return gcd (Q, P % Q );
}
Public rational (long numerator, long denominator)
{
Long gcdvalue = gcd (numerator, denominator );
Mynumerator = numerator/gcdvalue;
Mydenominator = Denominator/gcdvalue;
}
Public long numberator ()
{
Return mynumerator;
}
Public long denominator ()
{
Return mydenominator;
}
Public rational plus (Rational B)
{
Long gcdvalue = gcd (this. denominator (), B. denominator ());
Long n = This. numberator () * B. denominator ()/gcdvalue + B. numberator () * This. denominator ()/gcdvalue;
Long d = This. denominator () * B. denominator ()/gcdvalue;
//
Gcdvalue = gcd (d, n );
N = N/gcdvalue;
D = D/gcdvalue;
Return New Rational (n, d );
}
Public rational minus (Rational B)
{
Long gcdvalue = gcd (this. denominator (), B. denominator ());
Long n = This. numberator () * B. denominator ()/gcdValue-b.Numberator () * This. denominator ()/gcdvalue;
Long d = This. denominator () * B. denominator ()/gcdvalue;
//
Gcdvalue = gcd (d, n );
N = N/gcdvalue;
D = D/gcdvalue;
Return New Rational (n, d );
}
Public Rational Times (Rational B)
{
Long gcdvalue1 = gcd (this. numberator (), B. denominator ());
Long gcdvalue2 = gcd (this. denominator (), B. numberator ());
//
Long n = This. numberator ()/gcdvalue1 * B. numberator ()/gcdvalue2;
Long d = This. denominator ()/gcdvalue2 * B. denominator ()/gcdvalue1;
Return New Rational (n, d );
}
Public rational divides (Rational B)
{
Long gcdvalue1 = gcd (this. numberator (), B. numberator ());
Long gcdvalue2 = gcd (this. denominator (), B. denominator ());
//
Long n = This. numberator ()/gcdvalue1 * B. denominator ()/gcdvalue2;
Long d = This. denominator ()/gcdvalue2 * B. numberator ()/gcdvalue1;
Return New Rational (n, d );
}
Public Boolean equals (rational that)
{
If (this = That) return true;
If (that = NULL) return false;
If (this. numberator ()! = That. numberator () return false;
If (this. denominator ()! = That. denominator () return false;
Return true;
}
Public String tostring ()
{
Return this. numberator () + "/" + this. denominator ();
}
Public static void main (string [] ARGs)
{
Long numberator = long. parselong (ARGs [0]);
Long Denominator = long. parselong (ARGs [1]);
Rational R1 = New Rational (numberator, denominator );
Rational r2 = New Rational (numberator, denominator );
// =
Stdout. printf ("R1 = %-7 s r2 = %-7 s R1 = rs2 is: % s \ n", r1.tostring (), r2.tostring (), r1.equals (R2 ));
// +
Stdout. printf ("R1 = %-7 s r2 = %-7 s R1 + rs2 = %-7s \ n", r1.tostring (), r2.tostring (), r1.plus (R2 ));
//-
Stdout. printf ("R1 = %-7 s r2 = %-7 s r1-rs2 = %-7s \ n", r1.tostring (), r2.tostring (), r1.minus (R2 ));
//*
Stdout. printf ("R1 = %-7 s r2 = %-7 s R1 * rs2 = %-7s \ n", r1.tostring (), r2.tostring (), r1.times (R2 ));
///
Stdout. printf ("R1 = %-7 s r2 = %-7 s R1/rs2 = %-7s \ n", r1.tostring (), r2.tostring (), r1.divides (R2 ));
}
}
Number of rational Algs4-1.2.16