# 3.1-有理数类的设计

## 有理数设计

### 一. 有理数类代码

package rationalNumberDesign;

public class Rational {

private int numberator;//分子
private int denominator;//分母

public Rational(int numberator,int denominator) {//构造
int gcd = gcd(numberator,denominator);
this.numberator = numberator/gcd;
this.denominator = denominator/gcd;
}
public int getNumberator() {//获取分子
return  this.numberator;
}
public int getDeminator() {//获取分母
return this.denominator;
}

private static int gcd(int numberator,int denominator) {//碾转相除法求最大公约数
int n1 = Math.abs(numberator);
int n2 = Math.abs(denominator);
int temp;
while (n2 != 0) {
temp = n1%n2;
n1 = n2;
n2 = temp;
}
return n1;
}

public Rational add(Rational first,Rational second) {//加法
int num = first.numberator * second.denominator + second.numberator * first.denominator;
int den = first.denominator * second.denominator;

return new Rational(num,den);
}

public Rational subtract(Rational first,Rational second) {//减法
int num = first.numberator * second.denominator - second.numberator * first.denominator;
int den = first.denominator * second.denominator;

return new Rational(num,den);
}

public Rational multiply(Rational first,Rational second) {//乘法
int num = first.numberator * second.numberator;
int den = first.denominator * second.denominator;

return new Rational(num,den);
}

public Rational divide(Rational first,Rational second) {//除法
int num = first.numberator * second.denominator;
int den = first.denominator * second.numberator;
if(num<0&&den<0) {
num = Math.abs(num);
den = Math.abs(den);
}

return new Rational(num,den);
}

public String toString() {//转换为字符串类型
if(this.denominator == 1) {
return this.numberator+"";
}
else {
return this.numberator+"/"+this.denominator;
}
}

public boolean equal(Rational first,Rational second) {//比较是否相等
if((first.numberator == second.numberator)&&(first.denominator == second.denominator)) {
return true;
}
else {
return false;
}
}

public int intValue() {//转换为int型
return (int)doubleValue();
}

public long longValue() { //转换为long型

return (long)doubleValue();
}
public float floatValue() {  //转换为float型
return (float)doubleValue();
}

public double doubleValue() {//转换为double型
return this.numberator*1.0/this.denominator;
}
}



### 二. 测试代码

package rationalNumberDesign;

import rationalNumberDesign.Rational;
import java.util.Scanner;
public class rationalTest {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt();
Rational R1 = new Rational(a,b),R2 = new Rational(c,d);
System.out.println("减法有理数结果为:"+R1.subtract(R1, R2));
System.out.println("乘法有理数结果为:"+R1.multiply(R1, R2));
System.out.println("除法有理数结果为:"+R1.divide(R1, R2));
System.out.println("二者比较结果：  "+R1.equal(R1, R2));
System.out.println(R1.intValue());
System.out.println(R1.longValue());
System.out.println(R1.floatValue());
System.out.println(R1.doubleValue());
}
sc.close();
}

}



###### 合适。有两种情况下可设置为private：a.当一个方法希望只被本类所使用；b.与本类的内部状态无关。设置成private后对别人相当于隐藏的不可见的，保证数据的安全性。
posted @ 2020-10-04 09:24  黎里  阅读(121)  评论(0编辑  收藏  举报