0% found this document useful (0 votes)
2 views

java

The document is a Java program that defines a class for handling fractions, including methods for input, output, addition, subtraction, multiplication, and division of fractions. It also includes a method to simplify fractions using the greatest common divisor. The main method allows users to input two fractions and perform various arithmetic operations on them, displaying the results.

Uploaded by

longtygu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java

The document is a Java program that defines a class for handling fractions, including methods for input, output, addition, subtraction, multiplication, and division of fractions. It also includes a method to simplify fractions using the greatest common divisor. The main method allows users to input two fractions and perform various arithmetic operations on them, displaying the results.

Uploaded by

longtygu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package phanso1;

import java.util.Scanner;

/**
*
* @author Star Le
*/
public class PhanSo1 {
int tu; int mau;
PhanSo1(){ tu =0; mau = 1;}
PhanSo1(PhanSo1 p){
this.tu=p.tu;
this.mau=p.mau;
}
PhanSo1 Nhapgt(){
Scanner sc = new Scanner(System.in);
System.out.print(" nhap tu so = ");
tu = sc.nextInt();
System.out.print(" nhap mau so = ");
mau = sc.nextInt();
return this;
}
static void XuatPS(PhanSo1 ps){
if(ps.mau==1)
System.out.print(ps.tu);
else
System.out.println(+ps.tu+"/"+ps.mau);
}
static int USCLN(int a, int b){
if(a<0)
a=-a;
if(b<0)
b=-b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a+b;
}
public PhanSo1 Simplify(PhanSo1 ps){
int usc = USCLN(ps.tu,ps.mau);
ps.tu=ps.tu/usc; ps.mau=ps.mau/usc;
return ps;
}
public PhanSo1 Congps(PhanSo1 p1, PhanSo1 p2){
PhanSo1 p3 = new PhanSo1();
p3.tu=(p1.tu*p2.mau)+(p1.mau*p2.tu);
p3.mau=p1.mau*p2.mau;
p3=p3.Simplify(p3);
return p3;
}
public PhanSo1 Trups(PhanSo1 p1, PhanSo1 p2){
PhanSo1 p3 = new PhanSo1();
p3.tu=(p1.tu*p2.mau)-(p1.mau*p2.tu);
p3.mau=p1.mau*p2.mau;
p3=p3.Simplify(p3);
return p3;
}
public PhanSo1 Nhanps(PhanSo1 p1, PhanSo1 p2){
PhanSo1 p3 = new PhanSo1();
p3.tu=p1.tu*p2.tu;
p3.mau=p1.mau*p2.mau;
p3=p3.Simplify(p3);
return p3;
}
public PhanSo1 Chiaps(PhanSo1 p1, PhanSo1 p2){
PhanSo1 p3 = new PhanSo1();
p3.tu=p1.tu*p2.mau;
p3.mau=p1.mau*p2.tu;
p3=p3.Simplify(p3);
return p3;
}
public static void main(String[] args) {
PhanSo1 ps1= new PhanSo1();
PhanSo1 ps2= new PhanSo1();
PhanSo1 ps3= new PhanSo1();
System.out.println("Nhap phan so thu nhat");
ps1.Nhapgt();
System.out.print("Nhap phan so thu 2");
ps2.Nhapgt();
System.out.print(" Phan so thu 1 = ");
PhanSo1.XuatPS(ps1);
System.out.print(" Phan so thu 2 = ");
PhanSo1.XuatPS(ps2);
System.out.print(" Tong 2 phan so = ");
ps3= ps3.Congps(ps1, ps2);
ps3.XuatPS(ps3);
System.out.print(" Tru 2 phan so = ");
ps3= ps3.Trups(ps1, ps2);
ps3.XuatPS(ps3);
System.out.print(" Tich 2 phan so = ");
ps3= ps3.Nhanps(ps1, ps2);
ps3.XuatPS(ps3);
System.out.print(" Thuong 2 phan so = ");
ps3= ps3.Chiaps(ps1, ps2);
ps3.XuatPS(ps3);

}
Kết quả

You might also like