
BÀI T P TH C HÀNH NGÔN NG L P TRÌNH JAVAẬ Ự Ữ Ậ
Vi t ch ng trình cho m i yêu c u sau đây:ế ươ ỗ ầ
1. In h và tên c a chính mình ra màn hình.okọ ủ
2. Tính di n tích hình thang có đáy l n b ng 7, đáy nh b ng 5, ệ ớ ằ ỏ ằ
chi u cao b ng 6.ề ằ
S=(dl+dn)/2*dc.
Public class tdtht
{
public static void main(String[] args)
{
int dn=5;
int dl=7;
int dc=6;
float s=(dn+dl)/2*dc;
System.out.println(“dien tich hinh thang la: “ +s);
}
}
3. Tính di n tích c a hình tròn bán kính b ng 3ệ ủ ằ
public class tdthtron
{
public static void main(String[] args)
{
float pi=3.1416;
int r=3;
float s=pi*r*r;
System.out.println(“dien tich hinh tron la:” +s);
}
}
4. Tính di n tích c a tam giác có ba c nh l n l t b ng 5,6,7.ệ ủ ạ ầ ượ ằ
5. Tính giá tr c a ị ủ
yx
yx
v i x=1234, y=4321.ớ
public class tinhfx
{
public static void main(String[] args)

{
int x=1234;
int y=4321;
float fx=(x+y)/(x-y);
System.out.println(“gia tri cua ham so la:” +fx);
}
}
6. Tính giá tr c a ị ủ
xxx
v i x=3ớ
Public class tinhham
{
public static void main(String[] args)
{
int x=3;
float y=Math.sqrt(x+Math.sqrt(x+Math.sqrt(x)));
System.out.println(“ket qua cua ham so la:” +y);
}
}
7. Tính giá tr c a ị ủ
3
4
2
34)2(log
xxx
v i x=6ớ
//Co so e = 2,71828183
Public class tinhham2
{
public static void main(String[] args)
{
int x=6;
//log 2(x^4+2)=log e(x^4+2)/log e(2)
double z=Math.log(x*x*x*x+2)/Math.log(2)
double y=x*x*x+Math.log(x*x*x*x+2)-Math.cbrt(x-4);
8. 543200 giây là bao nhiêu gi , phút, giây? Ví d 3662 giây là 1gi ờ ụ ờ
1 phút 2 giây.
Public class doi_gio
{
public static void main(String[] args) throws Exception
{
int x=543200;
//doi sang gio

int gio=x/3600;
Math.floor(gio);
Int phut=(x-gio*3600)/60;
Math.floor(phut);
Int giay=x-gio*3600-phut*60;
System.out.println(“ket qua la: “
+gio+”gio”+phut+”phut”+giay+”giay”);
}
}
BÀI T P TH C HÀNH S 2Ậ Ự Ố
Vi t ch ng trình cho m i yêu c u sau đây:ế ươ ỗ ầ
1. Tính di n tích hình thang có đáy l n, đáy nh , chi u cao nh p t ệ ớ ỏ ề ậ ừ
bàn phím.
Import java.io.*;
public class tinhdtht
{
public static void main(String[] args) throws Exception
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
System.out.print(“nhap day lon cua hinh thang: “);
int dl=Integer.parseInt(in.readLine());
System.out.print(“nhap day nho cua hinh thang: “);
int dn= Integer.parseInt(in.readLine());
System.out.print(“nhap chieu cao cua hinh thang: “);
int cc=Integer.parseInt(in.readLine());
double s=(dl+dn)*cc/2;
System.out.print(“Dien tich cua hinh thang la: “ +s);
}
}
2. Tính di n tích c a hình tròn bán kính nh p t bàn phímệ ủ ậ ừ
Import java.io.*;
Public class tinhdthtron
{
public static void main(String[] args) throws Exception
{

BufferedReader in=new BufferedReader(new
InputStreamReader(System.in);
System.out.print(“nhap ban kinh cua hinh tron R= ”);
Int r=Integer.parseInt(in.readLine());
Float s=Math.PI*R*R;
System.out.println(“dien tich hinh tron la:” +s);
}
}
3. Tính di n tích c a tam giác có đ dài ba c nh nh p t bàn phím.ệ ủ ộ ạ ậ ừ
Import java.io.*;
Public class tinhdthtamgiac
{
public static void main(String[] args)
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Nhap ba canh cua tam giac:”;
System.out.print(“a=”);
Int a=Integer.parseInt(in.readLine());
Sytem.out.print(“b=”);
Int b=Integer.parseInt(in.readLine());
System.out.print(“c=”);
Int c=Integer.parseInt(in.readLine());
Double p=(a+b+c)/2;
Double s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.print(“Dien tich hinh tam giac la: s=” +s);
}
}
4. Tính giá tr c a ị ủ
yx
yx
v i x, y nh p t bàn phím.ớ ậ ừ
Import java.io.*;
public class tinhfx
{
public static void main(String[] args) throws Exception
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Nhap cac bien cua ham so”);

System.out.print(“x=”);
Int x=Integer.parseInt(in.readLine());
System.out.print(“y=”);
Int y=Integer.parseInt(in.readLine());
If ((x-y)==0)
System.out.print(“ket qua bi loi, phep chia cho 0”);
Else
{
double fx=(x+y)/(x-y);
System.out.print(“Ket qua cua ham so la:” +fx);
}
}
}
int x=1234;
int y=4321;
float fx=(x+y)/(x-y);
System.out.println(“gia tri cua ham so la:” +fx);
}
}
5. Tính giá tr c a ị ủ
xxx
v i x nh p t bàn phímớ ậ ừ
Import java.io.*;
Public class tinhhamx
{
public static void main(String[] args) throws Exception
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
System.out.print(“Nhap x=” );
Int x=Integer.parseInt(in.readLine());
If (x<0)
System.out.println(“Phuong trinh vo nghia”);
esle
{float y=Math.sqrt(x+Math.sqrt(x+Math.sqrt(x)));
System.out.println(“ket qua cua ham so la:” +y);
}
}