This web page requires JavaScript to be enabled.

JavaScript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.

How to enable JavaScript?

Bài thực hành 3 – Phân số [oop]

10cth1-2 March 14, 2012 0

Môn java trong lớp ta có nhiều nhân tài mà giấu nghề quá, chả ai lên chia sẻ bài viết cho anh em gì cả, bài này mình làm theo hướng dẫn của tài liệu, các bạn nào chưa biết thì xem và ai biết thì cùng thảo luận tối ưu hơn hay cách khác he [emot]sleepy[/emot].

Cách này là khai báo tử và mẫu là friendly, pốp lịch thì cũng gần giống

[codes=java]import java.util.;
import java.io.
;

class phanso{
  int tu,ms;
  
  public phanso()
  {
    tu=0; ms=1;
  }
  
  public phanso(int t, int m)
  {
    tu=t;ms=m;
  }
  public phanso(phanso p)
  {
    this.tu=p.tu;
    this.ms=p.ms;
  }
  
  public void nhap()
  {
    Scanner nhap=new Scanner(System.in);
    System.out.print("NHap tu: ");
      tu=nhap.nextInt();
    System.out.print("Nhap mau: ");
      ms=nhap.nextInt();
    if(ms==0)
    {
      do
      {
        System.out.print("Nhap lai mau <>0:");
        ms=nhap.nextInt();
      }while(ms==0);
    }
  }
  
  public void xuat()
  {
    System.out.print(tu+" / "+ms);
  }
  
  public int ucln(int a, int b)
  {
    int mx=1;
    for(int i=1; i<=ms; i++)
      if(ms%i==0 && tu%i==0)
        mx=i;
    return mx;
  }
  
  public void rutgon()
  {
    int temp=ucln(tu,ms);
    tu=tu/temp;
    ms=ms/temp;
  }
  
  public void cong(phanso p)
  {
    phanso kq= new phanso();
    kq.ms=this.msp.ms;
    kq.tu=this.tu
p.ms+p.tuthis.ms;
    kq.rutgon();
    System.out.println("\n\nKet qua Cong: " + kq.tu + "/" + kq.ms);
  }
  
  public void tru(phanso p)
  {
    phanso kq= new phanso();
    kq.ms=this.ms
p.ms;
    kq.tu=this.tup.ms-p.tuthis.ms;
    kq.rutgon();
    System.out.println("Ket qua tru: " + kq.tu + "/" + kq.ms);
  }
  
  public void nhan(phanso p)
  {
    phanso kq= new phanso();
    kq.ms=this.msp.ms;
    kq.tu=this.tu
p.tu;
    kq.rutgon();
    System.out.println("Ket qua nhan: " + kq.tu + "/" + kq.ms);
  }
  
  public void chia(phanso p)
  {
    phanso kq= new phanso();
    kq.ms=this.msp.tu;
    kq.tu=this.tu
p.ms;
    kq.rutgon();
    System.out.println("Ket qua chia: " + kq.tu + "/" + kq.ms);
  }
}

class Demo
{
  public static void main (String[] args)
  {
    phanso p=new phanso();
    phanso p2=new phanso();
    
    System.out.print("\nNhap phan so thu 1:\n");
      p.nhap();
    System.out.print("\nPhan so 1 vua nhap la: ");
      p.xuat();
    System.out.print("\nPhan so 1 sau khi rut gon: ");
      p.rutgon();
      p.xuat();
    
    System.out.print("\n\nNhap phan so thu 2:\n");
      p2.nhap();
    System.out.print("\nPhan so 2 vua nhap la: ");
      p2.xuat();
    System.out.print("\nPhan so 2 sau khi rut gon: ");
      p2.rutgon();
      p2.xuat();
    
    p.cong(p2);
    p.tru(p2);
    p.nhan(p2);
    p.chia(p2);
  }
}[/codes]


Last modified on March 31st, 2021 at 12:56 am

Nam Le
lequocnam



0 responds

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.