Bài toán: Xây dựng Lớp Phân Số. Tính toán Cộng, trừ, nhân, chia
Đây là bài cơ bản, nhưng chưa tối ưu, các bạn có thể tham gia đóng góp cho bài viết phong phú qua mail: admincnttvn@gmail.com
Đây là bài cơ bản, nhưng chưa tối ưu, các bạn có thể tham gia đóng góp cho bài viết phong phú qua mail: admincnttvn@gmail.com
class PhanSo
{
private int tu, mau;
public PhanSo(int tu, int mau)
{
this.tu=tu;
this.mau=mau;
}
public void setTu(int tu)
{
this.tu=tu;
}
public void setMau(int mau)
{
this.mau=mau;
}
public int getTu()
{
return this.tu;
}
public int getMau()
{
return this.mau;
}
public int USCLN(int a, int b)
{
while(a!=b)
{
if(a>b) a=a-b;
else b=b-a;
}
return a;
}
public void rutGon()
{
int i=USCLN(this.getTu(),this.getMau());
this.setTu(this.getTu()/i);
this.setMau(this.getMau()/i);
}
public void congPS(PhanSo ps)
{
int a=(this.getTu()*ps.getMau()) + (ps.getTu()*this.getMau());
int b=ps.getMau()*this.getMau();
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void truPS(PhanSo ps)
{
int a=(this.getTu()*ps.getMau()) - (ps.getTu()*this.getMau());
int b=ps.getMau()*this.getMau();
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void nhanPS(PhanSo ps)
{
int a=ps.tu*this.tu;
int b=ps.mau*this.mau;
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void chiaPS(PhanSo ps)
{
int a=this.tu*ps.mau;
int b=this.mau*ps.tu;
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public static void main(String[] args)
{
PhanSo ps=new PhanSo(16,4);
PhanSo ps2=new PhanSo(4,8);
System.out.println("Phan so: " + ps.tu + "/" + ps.mau);
System.out.println("USCLN: " + ps.USCLN(ps.tu,ps.mau));
//ps.rutGon();
//System.out.println("Phan so: " + ps.tu + "/" + ps.mau);
ps.congPS(ps2);
ps.truPS(ps2);
ps.nhanPS(ps2);
ps.chiaPS(ps2);
}
}
{
private int tu, mau;
public PhanSo(int tu, int mau)
{
this.tu=tu;
this.mau=mau;
}
public void setTu(int tu)
{
this.tu=tu;
}
public void setMau(int mau)
{
this.mau=mau;
}
public int getTu()
{
return this.tu;
}
public int getMau()
{
return this.mau;
}
public int USCLN(int a, int b)
{
while(a!=b)
{
if(a>b) a=a-b;
else b=b-a;
}
return a;
}
public void rutGon()
{
int i=USCLN(this.getTu(),this.getMau());
this.setTu(this.getTu()/i);
this.setMau(this.getMau()/i);
}
public void congPS(PhanSo ps)
{
int a=(this.getTu()*ps.getMau()) + (ps.getTu()*this.getMau());
int b=ps.getMau()*this.getMau();
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void truPS(PhanSo ps)
{
int a=(this.getTu()*ps.getMau()) - (ps.getTu()*this.getMau());
int b=ps.getMau()*this.getMau();
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void nhanPS(PhanSo ps)
{
int a=ps.tu*this.tu;
int b=ps.mau*this.mau;
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public void chiaPS(PhanSo ps)
{
int a=this.tu*ps.mau;
int b=this.mau*ps.tu;
PhanSo k=new PhanSo(a,b);
k.rutGon();
System.out.println("Ket qua Cong: " + k.tu + "/" + k.mau);
}
public static void main(String[] args)
{
PhanSo ps=new PhanSo(16,4);
PhanSo ps2=new PhanSo(4,8);
System.out.println("Phan so: " + ps.tu + "/" + ps.mau);
System.out.println("USCLN: " + ps.USCLN(ps.tu,ps.mau));
//ps.rutGon();
//System.out.println("Phan so: " + ps.tu + "/" + ps.mau);
ps.congPS(ps2);
ps.truPS(ps2);
ps.nhanPS(ps2);
ps.chiaPS(ps2);
}
}
0 comments:
Post a Comment