参考如下:
Fraction类
public class Fraction {
private int fz;
private int fm;
public Fraction(int fz1,int fm1){
this.fz = fz1;
this.fm = fm1;
}
public int getFz() {
return fz;
}
public void setFz(int fz) {
this.fz = fz;
}
public int getFm() {
return fm;
}
public void setFm(int fm) {
this.fm = fm;
}
public double value(){
return (double)fz/(double)fm;
}
public boolean equal(Fraction b){
if(this.fz== b.getFz() && this.fm==b.getFm())
return true;
if(this.value() == b.value())
return true;
return false;
}
@Override
public String toString() {
return "Fraction [fz=" + fz + ", fm=" + fm + "]";
}
}
Test类:
public class Test {
public static void main(String[] args) {
Fraction f= new Fraction(2,4);
Fraction f2=new Fraction(3,6);
Fraction f3=new Fraction(4,5);
if(f.equal(f2)){
System.out.println(f+"等于"+f2);
}else
System.out.println(f+"不等于"+f2);
}
}