Skip to content

Latest commit

 

History

History
72 lines (70 loc) · 2.24 KB

Q16.md

File metadata and controls

72 lines (70 loc) · 2.24 KB
public class PhanSo {
    private int tuSo;
    private int mauSo;

    public PhanSo() {
    }

    public PhanSo(int tuSo, int mauSo) {
        this.tuSo = tuSo;
        this.mauSo = mauSo;
    }
    public void inPut(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap tu so: ");
        tuSo = sc.nextInt();
        System.out.println("Nhap mau so: ");
        mauSo = sc.nextInt();
        while (mauSo==0){
            System.out.println("Nhap sai. Nhap lai di: ");
            mauSo = sc.nextInt();
        }
        System.out.println("-----------------------------");
    }
    public void outPut(){
        System.out.println("Phan So: "+tuSo+"/"+mauSo);
    }
    public void tongHaiPhanSo(PhanSo p1,PhanSo p2){
        PhanSo p = new PhanSo();
        p.tuSo = p1.tuSo * p2.mauSo + p2.tuSo * p1.mauSo;
        p.mauSo = p1.mauSo * p2.mauSo;
        System.out.println("Tong 2 phan so "+p1.tuSo+"/"+p1.mauSo+" + "+p2.tuSo+"/"+p2.mauSo+" = "+p.tuSo+"/"+p.mauSo);
    }
    public float max (float x, float y){
        if (x>y)
            return x;
        return y;
    }
    public void max2PhanSo(PhanSo p1,PhanSo p2){
        float temp1 = (float) p1.tuSo/p1.mauSo;
        float temp2 = (float) p2.tuSo/p2.mauSo;
        if (temp1>temp2){
            System.out.println("Phan so lon nhat trong 2 phan so: "+p1.tuSo+"/"+p1.mauSo);
        }else if(temp2>temp1) {
            System.out.println("Phan so lon nhat trong 2 phan so: "+p2.tuSo+"/"+p2.mauSo);
        }else {
            System.out.println("2 Phân số bằng nhau");
        }
    }
    public void max3PhanSo(PhanSo p1,PhanSo p2,PhanSo p3){
        float temp1 = (float) p1.tuSo/p1.mauSo;
        float temp2 = (float) p2.tuSo/p2.mauSo;
        float temp3 = (float) p3.tuSo/p3.mauSo;
        float max = max(max(temp1,temp2),temp3);
        if(temp1==temp2 && temp2 == temp3){
            System.out.println("3 Phan So bang nhau");
        }
        else {
            System.out.println("Phan so lon nhat trong 3 phan so la: ");
            if (temp1 == max){
                p1.outPut();
            }
            else if(temp2==max){
                p2.outPut();
            }
            else if(temp3==max){
                p3.outPut();
            }
        }
    }
}