2008年5月19日 星期一

lab Fraction Addition

public class Fraction
{
private int mom;
private int son;

public void setFraction(int son ,int mom)
{
this.mom=mom;
this.son=son;
}
public Fraction add (Fraction newFraction)
{
Fraction result=new Fraction();
result.mom=newFraction.mom*mom;
result.son=newFraction.mom*son+mom*newFraction.son;
return result;
}
public String equals (Fraction newFraction)
{
if((float)son/(float)mom==(float)newFraction.son/(float)newFraction.mom)
{
return "they are equal";
}
else
{
return "they are not equal";
}
}

public String toString()
{
return this.son+"/"+this.mom;
}

}



public class labFractionAddition
{
public static void main(String[] args)
{
Fraction f1=new Fraction(),
f2=new Fraction();

f1.setFraction(1,2);
f2.setFraction(1,3);
System.out.println(f1.add (f2).toString());
}

}























沒有留言: