1、有理数类代码
package chaoxing;
import java.util.Objects;
public class RationalNum extends Number{
private int molecule;
private int denominator; // 有理数都能用分数表示
public RationalNum(int mol, int den) {
// 考虑负负得正
if(mol < 0 && den < 0) {
mol = -mol;
den = -den;
}
this.molecule = mol;
this.denominator = den;
//求gcd
mol = Math.abs(mol);
den = Math.abs(den);
int mod;
while(den > 0) {
mod = mol % den;
mol = den;
den = mod;
}
int gcd = mol;
//同除gcd
this.molecule = this.molecule / gcd;
this.denominator = this.denominator / gcd;
}
static public void abs(RationalNum other) {
if(other.molecule < 0)
{
other.molecule = -other.molecule;
}
if(other.denominator < 0)
{
other.denominator = -other.denominator;
}
}
public RationalNum plus(RationalNum other) {
int fenzi = this.molecule * other.denominator + other.molecule * this.denominator;
int fenmu = this.denominator * other.denominator;
return new RationalNum(fenzi, fenmu);
}
public RationalNum subtract(RationalNum other) {
int fenzi = this.molecule * other.denominator - other.molecule * this.denominator;
int fenmu = this.denominator * other.denominator;
return new RationalNum(fenzi, fenmu);
}
public RationalNum multiply(RationalNum other) {
int fenzi = this.molecule * other.molecule;
int fenmu = this.denominator * other.denominator;
return new RationalNum(fenzi, fenmu);
}
public RationalNum divide(RationalNum other) {
int fenzi = this.molecule * other.denominator;
int fenmu = this.denominator * other.molecule;
return new RationalNum(fenzi, fenmu);
}
@Override
public int intValue() {
return molecule / denominator;
}
@Override
public long longValue() {
return (long)molecule / denominator;
}
@Override
public float floatValue() {
return (float) molecule / denominator;
}
@Override
public double doubleValue() {
return (double) molecule / denominator;
}
@Override
public String toString() {
if(molecule == 0) {
return "0";
}
else if(molecule == denominator) {
return "1";
}
return molecule+"/"+denominator;
}
@Override
public boolean equals(Object obj) {
RationalNum other = (RationalNum)obj;
if(this.molecule == other.molecule && this.denominator == other.denominator) {
return true;
}
else {
return false;
}
}
}
2、测试代码
package hello;
import chaoxing.RationalNum;
public class Main {
public static void main(String[] args) {
RationalNum num1 = new RationalNum(1, 2);
RationalNum num2 = new RationalNum(1, 2);
//加减乘除
System.out.println(num1.plus(num2).toString());
System.out.println(num1.subtract(num2).toString());
System.out.println(num1.multiply(num2).toString());
System.out.println(num1.divide(num2).toString());
// equal
if(num1.equals(num2)) {
System.out.println("相等");
}
// abs,各种值
RationalNum n = new RationalNum(-1, 2);
RationalNum.abs(n);
System.out.println(n.toString());
System.out.println(n.doubleValue());
System.out.println(n.floatValue());
System.out.println(n.intValue());
System.out.println(n.longValue());
}
}
3、与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
- 除了有数据组成,还有相对应的方法
- 每个有理数都是一个独立的个体
4、代码复用
-
别人如何复用代码?
导入我导出的jar文件 -
别人的代码是否依赖你的有理数类的内部属性?当你升级了你的有理数类,将其的属性名改变时。是否会影响他人以前编写的调用你有理数类的代码(假设他将使用了你升级后有理数类)?
本人自己设计的有理数类的属性是private不对外公开的,外部调用的代码对本人设计的类依赖性不强 -
有理数类的public方法是否设置合适?为什么有的方法设置为private?
- 设置public的方法是为了给其他使用该类的使用者使用的
- 某些设置成private的方法是用来实现public方法的,并不希望被外界所调用
-
你的类里面有static属性或方法吗?如果有,为什么要设置为static的?
有"abs()"方法,便于理解所设置的,参考了Math.abs()