Tải bản đầy đủ (.docx) (7 trang)

Bài tập JaVa: Cài đặt ps1 gồm có:

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (68.09 KB, 7 trang )

BÀI TẬP 4:
Đề bài:
Cài đặt ps1 gồm có:





Dữ liệu tử số, mẫu số.
Phương thức: nhập ps(mẫu khác 0),in ps,tối giản,công 2 ps
Phương thức:get, set,phép +,-,*,/, phép so sánh: ==,!=,>,>=,<,<=,++,-Chương trình chính:nhập 2 ps,thông báo các kết quả của phép toán +,-,*,/

Chương trình code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ngoan;

/**
*
* @author Ngoan
*/
import java.util.Scanner;
public class classphanso
{
int ts,ms;
Scanner input=new Scanner(System.in);
public classphanso()



{
}
public void InputData()
{
System.out.print("Nhap tu so: ");
this.ts=input.nextInt();
do
{
System.out.print("Nhap mau so: ");
this.ms=input.nextInt();
}
while (this.ms==0);
}
public void OutputData(String y)
{
System.out.println(y+" " +this.ts+"/"+this.ms);
}
public int UCLN(int a,int b)
{
if ((a==0) || (b==0))
return a+b;
while(a!=b)
{


if(a>b)
a=a-b;
else
b=b-a;
}

return a;
}
public void ToiGian()
{
int i=UCLN(ts,ms);
this.ts = this.ts/i;
this.ms = this.ms/i;
}
public void NghichDao()
{
int tg=this.ts;
this.ts = this.ms;
this.ms = tg;
}
public classphanso Tong(classphanso f)
{
classphanso ps=new classphanso();
ps.ts = ((this.ts*f.ms) + (this.ms*f.ts));


ps.ms = (this.ms*f.ms);
ps.ToiGian();
return ps;
}
public classphanso Hieu(classphanso f)
{
classphanso ps=new classphanso();
ps.ts = (this.ts*f.ms) - (this.ms*f.ts);
ps.ms = (this.ms*f.ms);
return ps;

}
public classphanso Tich(classphanso f)
{
classphanso ps=new classphanso();
ps.ts = this.ts*f.ts;
ps.ms = this.ms*f.ms;
ps.ToiGian();
return ps;
}
public classphanso Thuong(classphanso f)
{
classphanso ps=new classphanso();
ps.ts = this.ts*f.ms;


ps.ms = this.ms*f.ts;
ps.ToiGian();
return ps;
}
public classphanso quydong(classphanso f)
{
classphanso ps=new classphanso();
ps.ts=(this.ts*f.ms);
ps.ms=(this.ms*f.ms);
return ps;
}
public static void main(String[] argv)
{
classphanso ps1=new classphanso();
classphanso ps2=new classphanso();

ps1.InputData();
ps2.InputData();
ps1.OutputData("Phan so 1 la: ");
ps2.OutputData("Phan so 2 la: ");
ps1.ToiGian();
ps1.OutputData("Phan so 1 sau khi toi gian la: ");
ps2.ToiGian();
ps2.OutputData("Phan so 2 sau khi toi gian la: ");


ps1.Tong(ps2).OutputData("Tong hai phan so la: ");
ps1.Hieu(ps2).OutputData("Hieu hai phan so la: ");
ps1.Tich(ps2).OutputData("Tich hai phan so la: ");
ps1.Thuong(ps2).OutputData("Thuong hai phan so la:");
ps1.quydong(ps2).OutputData("phan so 1 đã quy đồng là: ");
ps2.quydong(ps1).OutputData("phan so 2 đã quy đồng là: ");
ps1.quydong(ps2);
ps2.quydong(ps1);
{
if (ps1.ts==ps2.ts)
System.out.print("hai phan so bang nhau ");
else
{
if(ps1.ts>ps2.ts)
System.out.print("phan so 1 lon hon phan so 2");
else
System.out.print("phan so 1 nho hơn phan so 2");
}
}
}

}


Chương trình chạy:



×