import java.util.List;
import java.util.Scanner;
public class PhanSo {
private int tu;
private int mau;
public PhanSo() {
this.tu = 0;
this.mau = 1;
}
public PhanSo(int tu, int mau) {
this.tu = tu;
this.mau = mau;
}
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 nhapPhanSo() {
Scanner input = new Scanner(System.in);
System.out.println("Nhap vao tu so: ");
this.tu = input.nextInt();
do {
System.out.println("Nhap vao mau so: ");
this.mau = input.nextInt();
if (this.mau == 0)
System.out.println("Mau cua ban khong hop le, mau phai khac 0");
} while (this.mau == 0);
}
public void xuatPhanSo() {
if(this.tu == 0)
System.out.println(+0);
else if (this.mau == 1)
System.out.println(+tu);
else
System.out.println(+this.tu+"/"+this.mau);
}
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;
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;
return kq;
}
public PhanSo nhanPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.tu;
kq.mau = this.mau*a.mau;
return kq;
}
public PhanSo chiaPhanSo(PhanSo a) {
PhanSo kq = new PhanSo();
kq.tu = this.tu*a.mau;
kq.mau = this.mau*a.tu;
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.