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 void nghichDao() {
int temp = this.tu;
this.tu = this.mau;
this.mau = temp;
}
public void giaTriNghichDao() {
System.out.println(+this.mau+"/"+this.tu);
}
public double tinhGiaTriThuc() {
double kq;
kq=(float)this.tu/this.mau;
return kq;
}
public boolean soSanhLonHon(PhanSo a) {
boolean kq;
if(this.tu*a.mau == this.mau*a.tu)
kq = true;
else
kq = false;
return kq;
}
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;
}
public PhanSo toiGianPhanSo() {
PhanSo kq = new PhanSo();
int ucln=1;
while (this.tu != this.mau) {
if (this.tu > this.mau)
ucln = this.tu - this.mau;
else
ucln = this.mau - this.tu;
}
if (this.tu==this.mau)
ucln=this.tu;
kq.tu=this.tu/ucln;
kq.mau=this.mau/ucln;
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.