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

# Important concepts

## Rational number class code.

package number; public class Rationalnumber { private double number = 0; private long denominator = 1; public double getNumber() { return number; } public void setNumber(long number) { this.number = number; } public void setDenominator(long denominator) { this.denominator = denominator; } public double getNumerator() { return number; } public double getDenominator() { return denominator; } //String output @Override public String toString() { return "Rationalnumber [number=" + number + "]"; } //Object single addition public void add(double number) { this.number += number; } //Object single subtraction public void subtract(double number) { this.number -= number; } //Object single multiplication public void multiply(double number) { this.number *= number; } //Object single division public void divide(double number) { if (number == 0) { System.out.println("false,you can't divide by 0"); } else { this.number /= number; } } //Class two addition methods public static Rationalnumber twoadd(long v1, long v2) { Rationalnumber b1 = new Rationalnumber(); b1.setNumber(v1); Rationalnumber b2 = new Rationalnumber(); b2.setNumber(v2); b1.add(b2.number); return b1; } //Class two subtraction methods public static Rationalnumber twosubtract(long v1, long v2) { Rationalnumber b1 = new Rationalnumber(); b1.setNumber(v1); Rationalnumber b2 = new Rationalnumber(); b2.setNumber(v2); b1.subtract(b2.number); return b1; } //Class two multiplication methods public static Rationalnumber twomultiply(long v1, long v2) { Rationalnumber b1 = new Rationalnumber(); b1.setNumber(v1); Rationalnumber b2 = new Rationalnumber(); b2.setNumber(v2); b1.multiply(b2.number); return b1; } //Class two division methods public static Rationalnumber twodivide(long v1, long v2) { Rationalnumber b1 = new Rationalnumber(); b1.setNumber(v1); Rationalnumber b2 = new Rationalnumber(); b2.setNumber(v2); b1.divide(b2.number); return b1; } //Object attribute int conversion public int intValue() { return (int) doubleValue(); } //Object attribute long conversion public long longValue() { return (long) doubleValue(); } //Object attribute float type conversion public float floatValue() { return (float) doubleValue(); } //Object attribute double conversion public double doubleValue() { return number * 1.0 / denominator; } //Output object properties by a certain number of decimal places public void round(int dec) { System.out.println(String.format("%."+dec+"f", number)); } //Judge whether the object attribute is equal to other values public boolean equals(double number1) { if ((number - number1) == 0) return true; else return false; } //Find the minimum common divisor inside the class private static long gcd(double n, double d) { double n1 = Math.abs(n); double n2 = Math.abs(d); int gcd = 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; } return gcd; } }

### Give your test code. To call a rational number class in a different package than a rational number class

package java201921123090; import number.Rationalnumber; public class Main { public static void main(String[] args) { Rationalnumber number1 = new Rationalnumber(); number1.setNumber(2); System.out.println(number1); number1.add(5); System.out.println(number1); number1.subtract(4); System.out.println(number1); number1.multiply(3); System.out.println(number1); number1.divide(2); System.out.println(number1); System.out.println(number1.intValue()); System.out.println(number1.longValue()); System.out.println(number1.floatValue()); System.out.println(number1.doubleValue()); number1=Rationalnumber.twoadd(1, 2); System.out.println(number1); number1=Rationalnumber.twosubtract(10, 1); System.out.println(number1); number1=Rationalnumber.twomultiply(12, 15); System.out.println(number1); number1=Rationalnumber.twodivide(3, 2); System.out.println(number1); number1.round(2); System.out.println(number1.equals(2)); } }

# Difficult problems and Solutions

### Try to describe how to compare with the rational number code of c language, and why the class you design is more object-oriented?

Answer:

- C language rational number code uses function (or process) to describe the operation of data, but separates the function from the data it operates; The class designed by java encapsulates the data and the operation on the data and processes it as a whole. It can better use the properties and methods of the object and is not easy to make mistakes.
- C language separates functions, while classes put functions and attributes together and are object-oriented, which makes them more stable and easier to maintain.
- The process oriented control flow is determined by the predetermined sequence in the program; The control flow of object-oriented program is triggered by the actual occurrence of various events at runtime, rather than determined by the predetermined sequence, which is more accurate.

### Try to describe your rational number class from the perspective of code reuse. From several aspects.

### How do others reuse your code?

Answer:

Composition or inheritance. If the new class and the existing class need to have some similar methods and properties, the form of inheritance is adopted; If the new class is just to borrow some methods and attributes of the existing class, and the two do not have many similarities, it needs to adopt the form of combination.

### Does someone else's code depend on the 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?

Answer:

No dependency. When the attribute changed to rational number is another rational number type, the code of the whole rational number class will not be affected. If it is changed to another type of attribute, the code will be wrong.

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

Answer:

The setting is appropriate. Because some methods do not want or need to be called from outside, they are set to private and can only be used internally.