To realize the subtraction of rational class, it is to realize its comparability, to overwrite the ToString () method, to realize the conversion of different data types, etc.
1 PackageChapter14;2 3 Public classRationalextendsNumberImplementsComparable {4 Private LongNumerator=0;5 Private LongDenominator=1;6 7 PublicRational () {8 This(0,1);9 }Ten PublicRational (LongNumerator,Longdenominator) { One //TODO auto-generated Constructor stub A LongGcd=gcd (numerator,denominator); - This. numerator= ((denominator>0)? 1:-1) *numerator/gcd; - This. Denominator=math.abs (Denominator)/gcd; the } - - Private Static LonggcdLongNLongd) { - //TODO auto-generated Method Stub + Longn1=Math.Abs (n); - LongN2=Math.Abs (d); + intGcd=1; A at for(intk=1;k<=n1&&k<=n2;k++){ - if(n1%k==0&&n2%k==0) -Gcd=K; - } - returngcd; - } in - Public LongGetnumerator () { to returnnumerator; + } - Public LongGetdenominator () { the returndenominator; * } $ Panax Notoginseng PublicRational Add (rational secondrational) { - LongN=numerator*secondrational.getdenominator () + thedenominator*secondrational.getnumerator (); + Longd=denominator*secondrational.getdenominator (); A return NewRational (n,d); the } + - PublicRational Subtract (rational secondrational) { $ LongN=numerator*secondrational.getdenominator ()- $denominator*secondrational.getnumerator (); - Longd=denominator*secondrational.getdenominator (); - return NewRational (n,d); the } - Wuyi PublicRational Multiply (rational SR) { the Longn=numerator*sr.getnumerator (); - Longd=denominator*sr.getdenominator (); Wu return NewRational (n,d); - } About $ Publicrational Divide (rational SR) { - Longn=numerator*Sr.denominator; - Longd=denominator*Sr.numerator; - return NewRational (n,d); A } + the PublicString toString () { - if(denominator==1) $ returnNumerator+ ""; the Else the returnnumerator+ "/" +denominator; the } the - Public Booleanequals (Object parm1) { in if(( This. Subtract (Rational) (Parm1)). Getnumerator () ==0) the return true; the Else About return false; the } the the + - @Override the Public intcompareTo (Object o) {Bayi //TODO auto-generated Method Stub the if(( This. Subtract (Rational) o). Getnumerator () >0) the return1; - Else if(( This. Subtract (Rational) o). Getnumerator () <0) - return-1; the Else the return0; the } the - @Override the Public intintvalue () { the //TODO auto-generated Method Stub the return(int) Doublevalue ();94 } the the @Override the Public LongLongvalue () {98 //TODO auto-generated Method Stub About return(Long) Doublevalue (); - }101 102 @Override103 Public floatFloatvalue () {104 //TODO auto-generated Method Stub the return(float) Doublevalue ();106 }107 108 @Override109 Public DoubleDoublevalue () { the //TODO auto-generated Method Stub111 returnnumerator*1.0/denominator; the }113 the the the 117}
Rational numbers are encapsulated in rational objects. Within the machine, the rational number is always expressed as its simplest form, the numerator determines the symbol of the rational number, and the denominator is always positive.
The GCD () method is private static.
The ToString method and the Equals method in the object class are overridden in the rational class. The ToString () method returns a string representation of a rational object in the form of Numerator/denominator.
Design and implementation of rational class of rational number class in Java