# Design of rational number classes

The rational number class is designed in an object-oriented manner following the BigDecimal class.

Rational number code:

```1,Rational number class source code:

public class Rational {
//attribute

private long numerator = 0; //molecule

private long denominator = 1; //denominator

public long getNumerator() {
return numerator;

}

public void setNumerator(long numerator) {
this.numerator = numerator;

}

public long getDenominator() {
return denominator;

}

public void setDenominator(long denominator) {
this.denominator = denominator;

}

//Constructor (no arguments)

public Rational(){//Numerator denominator initialization

this.numerator = 0;

this.denominator = 1;

}

//Constructor (with parameters)

public Rational(long numerator, long denominator){
this.numerator = numerator/gcd(Math.abs(numerator), Math.abs(denominator)); //Molecular reduction (divided by the maximum common divisor of numerator and denominator)

this.denominator = denominator/gcd(Math.abs(numerator), Math.abs(denominator));//Denominator reduction (divided by the maximum common divisor of numerator and denominator)

}

public static long gcd (long number1, long number2){//Maximum common divisor calculation

while (number1 != number2)

{
if (number1 > number2)

number1 = number1 - number2;

else

number2 = number2 - number1;

}

return number1;

}

public static Rational add (Rational num1, Rational num2) {//Sum of rational numbers

long tempNumerator = num1.numerator*num2.denominator + num1.denominator*num2.numerator;

long tempDenominator = num1.denominator*num2.denominator;

long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator));

Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd);

return rationalNumber;

}

public static Rational subtract (Rational num1, Rational num2) {//Rational number difference

long tempNumerator = num1.numerator*num2.denominator - num1.denominator*num2.numerator;

long tempDenominator = num1.denominator*num2.denominator;

long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator));

Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd);

return rationalNumber;

}

public static Rational multiply (Rational num1, Rational num2) {//Rational number quadrature

long tempNumerator = num1.numerator*num2.numerator;

long tempDenominator = num1.denominator*num2.denominator;

long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator));

Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd);

return rationalNumber;

}

public static Rational divide (Rational num1, Rational num2) {//Quotient of rational number

long tempNumerator = num1.numerator*num2.denominator;

long tempDenominator = num1.denominator*num2.numerator;

long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator));

Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd);

return rationalNumber;

}

}
```

Test code:

```2,Test code:

(stay RationalTest.java Medium test Rational.java)

//import java.util.Scanner;

public class RationalTest {
/*Scanner scanner = new Scanner(System.in);

int number = scanner.nextInt();

int nextNumber = scanner.nextInt();

*/

private static Rational num1=new Rational(1,2); //First score

private static Rational num2=new Rational(12,4); //Second score

private static double test = Double.parseDouble("3457367536")/(double)6769656;

public static void main(String[] args) {

System.out.println("Sum of rational numbers:");

System.out.println("(Fractional form): 1/2+12/4="+result.getNumerator()+"/"+result.getDenominator());

System.out.println("(decimal form ): 1/2+12/4="+(result.getNumerator()/(double)result.getDenominator()));

//Subtraction test

result=Rational.subtract(num1, num2);

System.out.println("Rational difference:");

System.out.println("(Fractional form): 1/2-12/4="+result.getNumerator()+"/"+result.getDenominator());

System.out.println("(decimal form ): 1/2-12/4="+(result.getNumerator()/(double)result.getDenominator()));

//Multiplication test

result=Rational.multiply(num1, num2);

System.out.println("(Fractional form): 1/2*12/4="+result.getNumerator()+"/"+result.getDenominator());

System.out.println("(decimal form ): 1/2*12/4="+(result.getNumerator()/(double)result.getDenominator()));

//Division test

result=Rational.divide(num1, num2);

System.out.println("Quotient of rational number:");

System.out.println("(Fractional form): 1/2-12/4="+result.getNumerator()+"/"+result.getDenominator());

System.out.println("(decimal form ): 1/2*12/4="+(result.getNumerator()/(double)result.getDenominator()));

}

}
```

Q: Try to answer why your class is more object-oriented than the rational number code in c language?

A: C language is process oriented programming, chestnut: put the elephant in the refrigerator. The steps of this process: open the refrigerator, put the elephant in the refrigerator and close the refrigerator door. C language encapsulates these three steps into three functions to solve the problem. In JAVA, there are two objects: refrigerator and door. The steps corresponding to each object are encapsulated into methods to solve the problem. Class is the blueprint of object. With class, we can design a series of objects and design a series of methods to solve them.

Q: Try to describe your rational number class from the perspective of code reuse. It is discussed from several aspects.

How do others reuse your code?

A: Download the package, import it, and solve the problem through the methods in the package.

Does someone else's code depend on the internal properties of your rational number class? When the properties of your rational number class are modified, will it affect others to call your rational number class code?

A: It has internal properties that depend on my rational number class. When the properties of the rational number class are modified, the code calling the rational number class will be affected.

Is the public method of rational number class properly set? Why are some methods set to private?

A: The public method of rational number class is set appropriately. If it is set to public, it has public access permission. When some methods do not need to be called by others, they can be set to private for internal calls only.

Do you have static properties or methods in your class? If yes, why set it to static?

A: Have static method; When creating multiple objects in a class, the method decorated with static can be called directly without creating objects. This is more convenient.

Tags: Java

Posted on Mon, 27 Sep 2021 09:10:35 -0400 by shadysaiyan