import java.util.Scanner;
public class PhanSo {
int tu;
int mau;
private Scanner nhap;
public PhanSo(int a, int b) {
}
public PhanSo() {
// TODO Auto-generated constructor stub
}
public int getTu() {
return tu;
}
public void setTu(int tu) {
this.tu = tu;
}
public int getMau() {
return mau;
}
public void setMau(int mau) {
this.mau = mau;
}
public void NhapPS() {
nhap = new Scanner(System.in);
System.out.print("Moi nhap tu so: ");
tu=nhap.nextInt();
System.out.print("Moi nhap mau so: ");
mau=nhap.nextInt();
do
{
if(mau==0)
System.out.print("Mau khong hop le! Moi nhap lai!");
}
while(mau==0);
}
public void XuatPS() {
if(tu == 0)
System.out.println(+0);
else if (mau == 1)
System.out.println("Phan so co dang: "+tu);
else
System.out.println("Phan so co dang: "+tu+"/"+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 PhanSo congPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.mau+this.mau*a.tu;
kq.mau = this.mau*a.mau;
PhanSo k=new PhanSo();
k.rutGon();
return kq;
}
public PhanSo truPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.mau-this.mau*a.tu;
kq.mau = this.mau*a.mau;
PhanSo k=new PhanSo();
k.rutGon();
return kq;
}
public PhanSo nhanPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.tu;
kq.mau = this.mau*a.mau;
PhanSo k=new PhanSo();
k.rutGon();
return kq;
}
public PhanSo chiaPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.mau;
kq.mau = this.mau*a.tu;
PhanSo k=new PhanSo();
k.rutGon();
return kq;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.