2012 
TamGa 
www.tamga85.multiply.com 
LẬP TRÌNH JAVA CĂN BẢN 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 1 
Mục Lục 
*** Bài 1 : Hình tam giác. Tính : chu vi và diện tích ......................................................................... 4 
*** Bài 2 : Hình chữ nhật. Tính : chu vi và diện tích ........................................................................ 5 
*** Bài 3: Mảng. Tính : .................................................................................................................... 6 
- Hãy nhập vào số phần tử có trong Mãng ...................................................................................... 6 
- Liệt kê các phần tử đó .................................................................................................................. 6 
*** Bài 4: Phân Số. Tính : ............................................................................................................... 7 
- Tổng, hiệu, tích, thương của hai phân số...................................................................................... 7 
- Ước số chung lớn nhất của phân số đó ........................................................................................ 7 
*** Bài 5. Số Phức. Tính : ............................................................................................................... 9 
- Tổng, hiệu, tích, thương của số phức ........................................................................................... 9 
*** Bài 6 : Giải Phương Trình Bậc 1 và Bậc 2 ............................................................................... 11 
*** Bài 8: Điểm hai chiều trong không gian. Tính : ........................................................................ 13 
- In Tọa Độ .................................................................................................................................... 13 
- Tính Khoảng Cách ...................................................................................................................... 13 
- Tính Tổng Điểm AB , ABC .......................................................................................................... 13 
- Tìm vecto AB=(xB-xA,yB-yA) ...................................................................................................... 13 
- Xác định 2 vecto có vuông góc hay không (a1a2 + b1b2 = 0 ) .................................................... 13 
- Ba điểm có tạo thành tam giác hay không ? ............................................................................... 13 
*** Bài 9: Thiết kế Giao Diện Đồ Họa. Với yêu cầu sau : .............................................................. 17 
- Nhấn nút Chào , sẽ hiện lên khung textbox " xin chào các bạn " ................................................. 17 
- Nhấn nút Reb hay Blue hay Green , thì khung textbox đổi màu tương ứng ................................ 17 
- Nhấn nút Xóa thì trở lại vị trí ban đầu ......................................................................................... 17 
- Nhấn nút Thoát thì thoát ra khỏi giao diện đồ họa ....................................................................... 17 
*** Bài 10: Hãy thiết kế giao diện đồ họa Giải Phương Trình Bậc 1 .............................................. 20 
*** Bài 11: Hãy thiết kế giao diện đồ họa Giải Phương Trình Bậc 2 .............................................. 23 
*** Bài 12: Hãy thiết kế giao diện đồ họa ...................................................................................... 26 
Các Phép Tính cộng, trừ , nhân, chia ............................................................................................ 26 
*** Bài 13: Hãy thiết kế giao diện đồ họa ...................................................................................... 30 
Tính chu vi và diện tích Hình Chữ Nhật ........................................................................................ 30 
*** Bài 14: Hãy thiết kế giao diện đồ họa ...................................................................................... 34 
Tính max và min của 4 số bất kỳ a,b,c,d ....................................................................................... 34 
*** Bài 15: Hãy thiết kế giao diện đồ họa Thay Đổi Màu Nền Background .................................... 37 
Gồm có các nút thuộc dạng Radio Button ..................................................................................... 37 
*** Bài 16: Hãy thiết kế giao diện đồ họa Nhập Xuất Mảng 1 Chiều ............................................. 40 
Với các yêu cầu sau : ................................................................................................................... 40 
- Nhập một mảng bất kỳ ................................................................................................................ 40 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 2 
- Tính tổng các phần tử trong mảng .............................................................................................. 40 
- Sắp xếp lại mảng tăng dần ......................................................................................................... 40 
*** Bài 17: Hãy thiết kế giao diện đồ họa ...................................................................................... 44 
Với các yêu cầu sau : ................................................................................................................... 44 
- Dùng Radio Button ..................................................................................................................... 44 
- Tính các phép tính cơ bản như : cộng , trừ , nhân , chia ............................................................. 44 
*** Bài 18: Hãy thiết kế giao diện đồ họa ...................................................................................... 48 
Với các yêu cầu sau : ................................................................................................................... 48 
- Dùng Radio Button ..................................................................................................................... 48 
- Tính các phép tính cơ bản như : cộng , trừ , nhân , chia ............................................................. 48 
- Khi click thực hiện và chọn yêu cầu từ thì mới thực hiện ............................................................ 48 
*** Bài 19: Hãy thiết kế giao diện đồ họa ...................................................................................... 53 
Với các yêu cầu sau : ................................................................................................................... 53 
- Dùng Radio Button ..................................................................................................................... 53 
- Giải các phương trình bậc 1 và bậc 2 ......................................................................................... 53 
- Khi click thực hiện và chọn yêu cầu từ thì mới thực hiện ............................................................ 53 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 3 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 4 
*** Bài 1 : Hình tam giác. Tính : chu vi và diện tích 
============================ Đáp án xuất file 
Nhap canh a: 2 Nhap canh b: 4 Nhap canh c: 3 
Chu vi: 9.0 Dien tich: 2.9047375096555625 
============================ 
Và đây là code trong class : 
import java.io.*; 
public class tamga 
{ 
 static double a,b,c,chuvi,dientich; 
 public static void nhap() throws IOException 
 { 
 BufferedReader d=new BufferedReader (new InputStreamReader(System.in)); 
 System.out.print("Nhap canh a: "); 
 String s=d.readLine(); 
 a=Double.parseDouble(s); 
 System.out.print("Nhap canh b: "); 
 s=d.readLine(); 
 b=Double.parseDouble(s); 
 System.out.print("Nhap canh c: "); 
 s=d.readLine(); 
 c=Double.parseDouble(s); 
 } 
 public static double chuvi() 
 { return a+b+c ; } 
 public static double dientich() 
 { 
 double P,S ; 
 P=chuvi()/2; 
 S=Math.sqrt(P*(P-a)*(P-b)*(P-c)); 
 return S; 
 } 
} 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class HinhTamGiac 
{ 
 public static void main(String[] args) throws IOException 
 { 
 tamga tg=new tamga(); 
 tg.nhap(); 
 System.out.println("Chu vi: " + tg.chuvi()); 
 System.out.println("Dien tich: " + tg.dientich()); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 5 
*** Bài 2 : Hình chữ nhật. Tính : chu vi và diện tích 
============================ Đáp án mẫu xuất file 
Nhap a: 4 
Nhap b: 2 
Chu vi: 12.0 
Dien tich: 8.0 
============================ 
Và đây là code trong class : 
import java.io.*; 
public class tamgaalbum 
{ 
 static double a,b,chuvi,dientich; 
 public static void nhap() throws IOException 
 { 
 BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); 
 System.out.print("Nhap a: "); 
 String s=d.readLine(); 
 a=Double.parseDouble(s); 
 System.out.print("Nhap b: "); 
 s=d.readLine(); 
 b=Double.parseDouble(s); 
 } 
 public static double chuvi() 
 { return (a+b)*2; } 
 public static double dientich() 
 { 
 double S; 
 S=a*b; 
 return S; 
 } 
} 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class HinhChuNhat 
{ 
 public static void main(String[] args) throws IOException 
 { 
 tamgaalbum hcn=new tamgaalbum(); 
 hcn.nhap(); 
 System.out.println("Chu vi: " + hcn.chuvi()); 
 System.out.println("Dien tich: " + hcn.dientich()); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 6 
*** Bài 3: Mảng. Tính : 
- Hãy nhập vào số phần tử có trong Mãng 
- Liệt kê các phần tử đó 
============================ Đáp án mẫu xuất file 
Nhap so phan tu Mang: 5 Mang vua nhap la: 5 
a[0]: 2 a[1]: 5 a[2]: 4 a[3]: 2 a[4]: 1 
So phan tu mang: 2 5 4 2 1 
============================ 
Và đây là code trong class : 
// TamGa www.tamga.tk www.c10mt.tk www.c10maytinh.tk 
import java.io.*; 
public class tamgaalbum 
{ 
 static int n; 
 public static void nhap(int a[]) throws IOException 
 { 
 BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); 
 System.out.print("Nhap so phan tu Mang: "); 
 String s=d.readLine(); 
 n=Integer.parseInt(s); 
 System.out.println("Mang vua nhap la: "+n); 
 for (int i=0;i<n;i++) 
 { 
 System.out.print("a["+i+"]: "); 
 s=d.readLine(); 
 a[i]=Integer.parseInt(s); 
 } 
 } 
 public static void xuat(int a[]) 
 { 
 System.out.print("So phan tu mang: "); 
 for(int i=0;i<n;i++) 
 System.out.print(" " +a[i]); 
 } 
} 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class Mang 
{ 
 public static void main(String[] args) throws IOException 
 { 
 int[] a=new int[20]; 
 tamgaalbum b=new tamgaalbum(); 
 b.nhap(a); 
 b.xuat(a); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 7 
*** Bài 4: Phân Số. Tính : 
- Tổng, hiệu, tích, thương của hai phân số 
- Ước số chung lớn nhất của phân số đó 
============================ Đáp án mẫu xuất file 
Nhap p1: Nhap tu: 4 Nhap mau: 2 
Nhap p2: Nhap tu: 2 Nhap mau: 4 
Phan tu thu 1: 4/2 Phan tu thu 2: 2/4 
Tich la: 1/1 Tong la: 5/2. Hieu la: 3/2 Thuong la: 4/1 Uoc Chung Lon Nhat: 4 
============================ 
Và đây là code trong class : 
import java.io.*; 
public class tamgaalbum 
{ 
 double a,b; 
 static double tu,mau; 
 tamgaalbum() { }; 
 tamgaalbum(double x, double y) 
 { a=x; b=y; } 
 public static void tich (tamgaalbum c,tamgaalbum d) 
 { tu=c.a * d.a; 
 mau=c.b * d.b; 
 } 
 public static void tong (tamgaalbum c,tamgaalbum d) 
 { tu=(c.a*d.b) +(d.a*c.b); 
 mau=d.a * d.b; 
 } 
 public static void hieu (tamgaalbum c,tamgaalbum d) 
 { tu=(c.a*d.b) -(d.a*c.b); 
 mau=d.a * d.b; 
 } 
 public static void thuong (tamgaalbum c,tamgaalbum d) 
 { tu=(c.a*d.b); 
 mau=c.b * d.a; 
 } 
 public static double usclonnhat (double c,double d) 
 { while (c!=d) 
 if (c>d) 
 c = c - d; 
 else 
 d = d - c; 
 return c; 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 8 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class PhanSo { 
 public static void main(String[] args) throws IOException 
 { 
 BufferedReader d=new BufferedReader(new InputStreamReader(System.in)) ; 
 String s; double x,y; 
 tamgaalbum p1,p2; 
 System.out.print("Nhap p1: "); 
 System.out.print("\n Nhap tu: "); 
 s=d.readLine(); x=Double.parseDouble(s); 
 System.out.print("Nhap mau: "); 
 s=d.readLine(); y=Double.parseDouble(s); 
 p1=new tamgaalbum(x,y); 
 System.out.print("\n"); 
 System.out.print("Nhap p2: "); 
 System.out.print("\n Nhap tu: "); 
 s=d.readLine(); x=Double.parseDouble(s); 
 System.out.print("Nhap mau: "); 
 s=d.readLine(); y=Double.parseDouble(s); 
 p2=new tamgaalbum(x,y); 
 System.out.print("\n Phan tu thu 1: " +p1.a + "/"+ p1.b); 
 System.out.print("\n Phan tu thu 2: " +p2.a + "/"+ p2.b); 
 System.out.print("\n"); 
 double usc ; 
 tamgaalbum.tich(p1,p2); 
 usc=tamgaalbum.usclonnhat(tamgaalbum.tu,tamgaalbum.mau); 
 System.out.print("\n Tich la: " +tamgaalbum.tu/usc + "/"+ tamgaalbum.mau/usc); 
 tamgaalbum.tong(p1,p2); 
 usc=tamgaalbum.usclonnhat(tamgaalbum.tu,tamgaalbum.mau); 
 System.out.print("\n Tong la: " +tamgaalbum.tu/usc + "/"+ tamgaalbum.mau/usc); 
 tamgaalbum.hieu(p1,p2); 
 usc=tamgaalbum.usclonnhat(tamgaalbum.tu,tamgaalbum.mau); 
 System.out.print("\n Hieu la: " +tamgaalbum.tu/usc + "/"+ tamgaalbum.mau/usc); 
 tamgaalbum.thuong(p1,p2); 
 usc=tamgaalbum.usclonnhat(tamgaalbum.tu,tamgaalbum.mau); 
 System.out.print("\n Thuong la: " +tamgaalbum.tu/usc + "/"+ tamgaalbum.mau/usc); 
System.out.print("\n Uoc Chung Lon Nhat: " + tamgaalbum.usclonnhat(tamgaalbum.tu,tamgaalbum.mau)); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 9 
*** Bài 5. Số Phức. Tính : 
- Tổng, hiệu, tích, thương của số phức 
============================ Đáp án mẫu xuất file 
Nhap so thuc a: 8 Nhap so ao b: 4 So Phuc P1 la : 8 4 
Nhap so thuc a: 4 Nhap so ao b: 2 So Phuc P2 la : 4 2 
Tong la: 12 6 Hieu la: 4 2 Tich la: 32 8 Thuong la: 2 2 
============================ 
Và đây là code trong class : 
import java.io.*; 
public class tamgaalbum 
{ 
 int a,b; 
 static int thuc,ao; 
 tamgaalbum () {}; 
 tamgaalbum(int x , int y) 
 { 
 a=x; 
 b=y; 
 } 
public static void tong(tamgaalbum c, tamgaalbum d) 
 { 
 thuc=c.a+ c.b; 
 ao= d.a+d.b; 
 } 
public static void hieu(tamgaalbum c, tamgaalbum d) 
 { 
 thuc=c.a - c.b; 
 ao= d.a - d.b; 
 } 
public static void tich(tamgaalbum c, tamgaalbum d) 
 { 
 thuc=c.a * c.b; 
 ao= d.a * d.b; 
 } 
public static void thuong(tamgaalbum c, tamgaalbum d) 
 { 
 thuc=c.a / c.b; 
 ao= d.a / d.b; 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 10 
============================ 
Và đây là code trong void main : 
/** 
 TamGa 
 www.tamga.tk www.c10mt.tk www.c10maytinh.tk 
 Phone: 01283.98.69.98 Email : 
[email protected] 
 Bai tap : Tinh tong,hieu,tich,thuong cua So Phuc 
 */ 
import java.io.*; 
public class SoPhuc 
{ 
 public static void main(String[] args) throws IOException 
 { 
 BufferedReader d=new BufferedReader(new InputStreamReader(System.in)) ; 
 String s; 
 int x,y; 
 tamgaalbum p1,p2; 
 System.out.print("\nNhap so thuc a: "); 
 s=d.readLine(); x=Integer.parseInt(s); 
 System.out.print("Nhap so ao b: "); 
 s=d.readLine(); y=Integer.parseInt(s); 
 p1=new tamgaalbum(x,y); 
 System.out.print("So Phuc P1 la : " +p1.a +" " + p1.b); 
 System.out.print("\n"); 
 System.out.print("\nNhap so thuc a: "); 
 s=d.readLine(); x=Integer.parseInt(s); 
 System.out.print("Nhap so ao b: "); 
 s=d.readLine(); y=Integer.parseInt(s); 
 p2=new tamgaalbum(x,y); 
 System.out.print("So Phuc P2 la : " +p2.a +" " + p2.b); 
 System.out.print("\n"); 
 tamgaalbum.tong(p1,p2); 
 System.out.print("\n Tong la: " +tamgaalbum.thuc + " "+ tamgaalbum.ao); 
 tamgaalbum.hieu(p1,p2); 
 System.out.print("\n Hieu la: " +tamgaalbum.thuc + " "+ tamgaalbum.ao); 
 tamgaalbum.tich(p1,p2); 
 System.out.print("\n Tich la: " +tamgaalbum.thuc + " "+ tamgaalbum.ao); 
 tamgaalbum.thuong(p1,p2); 
 System.out.print("\n Thuong la: " +tamgaalbum.thuc + " "+ tamgaalbum.ao); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 11 
*** Bài 6 : Giải Phương Trình Bậc 1 và Bậc 2 
============================Đáp án mẫu xuất file 
======= Giai Phuong Trinh Bac 1 www.tamga.tk 
Nhap a: 2 Nhap b: 4 
Ket qua PT bac1: Phuong trinh co nghiem: -2.0 
======= Giai Phuong Trinh Bac 2 www.tamga.tk 
Nhap a: 2 Nhap b: 1 Nhap c: -3 
Ket qua PT bac2: Phuong trinh co 2 nghiem: x1= 1.0 va x2= -1.5 
============================ 
Và đây là code trong class : 
// TamGa - Class Giai Phuong Trinh Bac 1 va 2 
import java.io.*; 
class tamgaalbum 
{ 
 static double a,b,c ; 
 //=============== Giai phuong trinh bac 1 =============== 
 public static void nhapptbac1() throws IOException 
 { 
 BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); 
 System.out.println("======= Giai Phuong Trinh Bac 1 www.tamga.tk"); 
 System.out.print("Nhap a: "); 
 String s=d.readLine(); 
 a=Double.parseDouble(s); 
 System.out.print("Nhap b: "); 
 s=d.readLine(); 
 b=Double.parseDouble(s); 
 } 
 public static void giaiptbac1() 
 { 
 System.out.print("Ket qua PT bac1: "); 
 if(a==0) 
 System.out.println("Phuong trinh vo nghiem"); 
 else 
 { 
 if(b==0) 
 System.out.println("Phuong trinh vo so nghiem"); 
 else 
 System.out.println("Phuong trinh co nghiem: " +(-b/a)); 
 } 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 12 
 //=============== Giai phuong trinh bac 2 =============== 
 public static void nhapptbac2() throws IOException 
 { BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); 
 System.out.println("======= Giai Phuong Trinh Bac 2 www.tamga.tk"); 
 System.out.print("Nhap a: "); 
 String s=d.readLine(); 
 a=Double.parseDouble(s); 
 System.out.print("Nhap b: "); 
 s=d.readLine(); 
 b=Double.parseDouble(s); 
 System.out.print("Nhap c: "); 
 s=d.readLine(); 
 c=Double.parseDouble(s); 
 } 
 public static void giaiptbac2() 
 { double delta,x1,x2; 
 delta=(b*b)-(4*a*c); 
 System.out.print("Ket qua PT bac2: "); 
 if (delta<0) 
 System.out.println("Phuong trinh vo nghiem"); 
 else 
 { 
 if (delta==0) 
 System.out.println("Phuong trinh co 1 nghiem: " +(-b)/(2*a)); 
 else 
 { x1=(-b+Math.sqrt(delta))/(2*a); 
 x2=(-b-Math.sqrt(delta))/(2*a); 
 System.out.println("Phuong trinh co 2 nghiem: "); 
 System.out.println("x1= "+x1+" va "+"x2= "+x2); 
 } 
 } 
 } 
} 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class GiaiPhuongTrinhBac1va2 
{ 
 public static void main(String[] args) throws IOException 
 { 
 tamgaalbum gpt=new tamgaalbum(); 
 gpt.nhapptbac1(); 
 gpt.giaiptbac1(); 
 gpt.nhapptbac2(); 
 gpt.giaiptbac2(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 13 
*** Bài 7: update load sau 
*** Bài 8: Điểm hai chiều trong không gian. Tính : 
- In Tọa Độ 
- Tính Khoảng Cách 
- Tính Tổng Điểm AB , ABC 
- Tìm vecto AB=(xB-xA,yB-yA) 
- Xác định 2 vecto có vuông góc hay không (a1a2 + b1b2 = 0 ) 
- Ba điểm có tạo thành tam giác hay không ? 
*** Đáp án mẫu xuất file 
========= Nhap toa do diem A 
Toa do x1: 1 Toa do y1: 2 Toa do z1: 3 
Toa do diem A(1,2,3) 
========= Nhap toa do diem B 
Toa do x2: 4 Toa do y2: 5 Toa do z2: 6 
Toa do diem B(4,5,6) 
========= Nhap toa do diem C 
Toa do x3: 7 Toa do y3: 8 Toa do z3: 9 
Toa do diem C(7,8,9) 
========= Toa do cua 3 diem A,B,C nhu sau: 
A(1,2,3) B(4,5,6) C(7,8,9) 
========= Khoang cach cua 2 diem trong toa do: 
AB: 5.196152422706632 
AC: 10.392304845413264 
BC: 5.196152422706632 
========= Tong cua 2 diem A,B trong toa do: AB(5,7,9) 
========= Tong cua 3 diem A,B,C trong toa do: ABC(12,15,18) 
========= Toa do vecto AB la: AB(3,3,3) 
========= Kiem tra vecto co vuong goc khong : Vecto khong vuong goc 
========= Xet dieu kien Tam Giac : 3 Diem tao nen Tam Giac 
============================ 
Và đây là code trong class : 
/* 
TamGa 
www.tamga.tk 
Phone: 01283.98.69.98 
Bai Tap : Diem Hai Chieu Trong Khong Gian 
*/ 
import java.io.*; 
public class Ham 
{ 
 int a,b,c ; 
 static int tg1,tg2,tg3,vt1,vt2,vt3 ; 
 Ham(){} ; 
 Ham(int x,int y,int z) 
 { 
 a=x; 
 b=y; 
 c=z; 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 14 
 public static double khoangcach(Ham g,Ham h) 
 { 
 double D; 
 D=Math.sqrt( ((h.a-g.a)*(h.a-g.a)) + ((h.b-g.b)*(h.b-g.b)) + ((h.c-g.c)*(h.c-g.c)) ); 
 return D; 
 } 
 public static void tongdiem(Ham g,Ham h) 
 { tg1=g.a+h.a; 
 tg2=g.b+h.b; 
 tg3=g.c+h.c; 
 } 
 public static void tong3diem(Ham g,Ham h,Ham k) 
 { 
 tg1=g.a+h.a+k.a; 
 tg2=g.b+h.b+k.b; 
 tg3=g.c+h.c+k.c; 
 } 
 public static void vecto(Ham g,Ham h) 
 { // Ta co : OA=(x1,y1,z1) ; OB=(x2,y2,z2) => AB=(x2-x1,y2-y1,z2-z1) 
 vt1=h.a-g.a; 
 vt2=h.b-g.b; 
 vt3=h.c-g.c; 
 } 
 public static double vectovuonggoc(Ham g,Ham h,Ham k) 
 { double a1,a2,a3,b1,b2,b3,c1,c2,c3; 
 a1=g.a; a2=h.a; a3=k.a; 
 b1=g.a; b2=h.a; b3=k.a; 
 c1=g.a; c2=h.a; c3=k.a; 
 if( (a1*a2*a3)+(b1*b2*b3)+(c1*c2*c3)==0 ) 
 return 1; 
 return 0; 
 } 
 public static double xettamgiac(Ham g,Ham h,Ham k) 
 { double AB,AC,BC; 
 AB=khoangcach(g,h); 
 AC=khoangcach(g,k); 
 BC=khoangcach(h,k); 
 if(AB+AC>BC || AB+BC>AC || AC+BC>AB) 
 return 1; 
 return 0; 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 15 
============================ 
Và đây là code trong void main : 
import java.io.*; 
public class Diem2ChieuTrongKhongGian 
{ 
 public static void main(String[] args) throws IOException 
 { BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); 
 String s ; 
 Ham toado1,toado2,toado3 ; 
 int x,y,z ; 
 // www.tamga.tk =========================== 
 System.out.print("========= Nhap toa do diem A"); 
 System.out.print("\nToa do x1: "); 
 s=d.readLine(); x=Integer.parseInt(s); 
 System.out.print("Toa do y1: "); 
 s=d.readLine(); y=Integer.parseInt(s); 
 System.out.print("Toa do z1: "); 
 s=d.readLine(); z=Integer.parseInt(s); 
 toado1=new Ham(x,y,z); 
 System.out.print("Toa do diem A"+"("+toado1.a+","+toado1.b+","+toado1.c+")"); 
 System.out.print("\n========= Nhap toa do diem B"); 
 System.out.print("\nToa do x2: "); 
 s=d.readLine(); x=Integer.parseInt(s); 
 System.out.print("Toa do y2: "); 
 s=d.readLine(); y=Integer.parseInt(s); 
 System.out.print("Toa do z2: "); 
 s=d.readLine(); z=Integer.parseInt(s); 
 toado2=new Ham(x,y,z); 
 System.out.print("Toa do diem B"+"("+toado2.a+","+toado2.b+","+toado2.c+")"); 
 System.out.print("\n========= Nhap toa do diem C"); 
 System.out.print("\nToa do x3: "); 
 s=d.readLine(); x=Integer.parseInt(s); 
 System.out.print("Toa do y3: "); 
 s=d.readLine(); y=Integer.parseInt(s); 
 System.out.print("Toa do z3: "); 
 s=d.readLine(); z=Integer.parseInt(s); 
 toado3=new Ham(x,y,z); 
 System.out.print("Toa do diem C"+"("+toado3.a+","+toado3.b+","+toado3.c+")"); 
 // www.tamga.tk ================== Toa Do 
 System.out.print("\n========= Toa do cua 3 diem A,B,C nhu sau: "); 
 System.out.print("\n A"+"("+toado1.a+","+toado1.b+","+toado1.c+")"); 
 System.out.print("\n B"+"("+toado2.a+","+toado2.b+","+toado2.c+")"); 
 System.out.print("\n C"+"("+toado3.a+","+toado3.b+","+toado3.c+")"); 
 // www.tamga.tk =================== Khoang cach 
 System.out.print("\n========= Khoang cach cua 2 diem trong toa do: \n"); 
 System.out.print("\nAB: "); 
 System.out.print(Ham.khoangcach(toado1,toado2)); 
 System.out.print("\nAC: "); 
 System.out.print(Ham.khoangcach(toado1,toado3)); 
 System.out.print("\nBC: "); 
 System.out.print(Ham.khoangcach(toado2,toado3)); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 16 
 // www.tamga.tk =================== Tong Diem 
 Ham.tongdiem(toado1,toado2); 
 System.out.print("\n========= Tong cua 2 diem A,B trong toa do: \n"); 
 System.out.print("AB"+"("+Ham.tg1+","+Ham.tg2+","+Ham.tg3+")"); 
 Ham.tong3diem(toado1,toado2,toado3); 
 System.out.print("\n========= Tong cua 3 diem A,B,C trong toa do: \n"); 
 System.out.print("ABC"+"("+Ham.tg1+","+Ham.tg2+","+Ham.tg3+")"); 
 // www.tamga.tk =================== Tim vecto 
 Ham.vecto(toado1,toado2); 
 System.out.print("\n========= Toa do vecto AB la: \n"); 
 System.out.print("AB"+"("+Ham.vt1+","+Ham.vt2+","+Ham.vt3+")"); 
 // www.tamga.tk =================== Xac dinh 2vecto co vuong goc 
 System.out.print("\n========= Kiem tra vecto co vuong goc khong : \n"); 
 Ham.vectovuonggoc(toado1,toado2,toado3); 
 if(Ham.vectovuonggoc(toado1,toado2,toado3)==1) 
 System.out.print("Vecto vuong goc"); 
 else 
 System.out.print("Vecto khong vuong goc"); 
 // www.tamga.tk =================== Xac dinh 2vecto co vuong goc 
 System.out.print("\n========= Xet dieu kien Tam Giac : \n"); 
 // Cach 1: 
 if(Ham.xettamgiac(toado1,toado2,toado3)==1) 
 System.out.print("\n3 Diem tao nen Tam Giac"); 
 else 
 System.out.print("\n3 Diem khong tao nen Tam Giac"); 
 // Cach 2: 
 if(Ham.khoangcach(toado1,toado2)+Ham.khoangcach(toado1,toado3)>Ham.khoangcach(toado2,toado3)) 
 System.out.print("\n3 Diem tao nen Tam Giac"); 
 else 
 { 
 if(Ham.khoangcach(toado1,toado2)+Ham.khoangcach(toado2,toado3)>Ham.khoangcach(toado1,toado3)) 
 System.out.print("\n3 Diem tao nen Tam Giac"); 
 else 
 { 
 if(Ham.khoangcach(toado1,toado3)+Ham.khoangcach(toado2,toado3)>Ham.khoangcach(toado1,toado2)) 
 System.out.print("\n3 Diem tao nen Tam Giac"); 
 else 
 System.out.print("\n3 Diem khong tao nen Tam Giac"); 
 } 
 } 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 17 
*** Bài 9: Thiết kế Giao Diện Đồ Họa. Với yêu cầu sau : 
- Nhấn nút Chào , sẽ hiện lên khung textbox " xin chào các bạn " 
- Nhấn nút Reb hay Blue hay Green , thì khung textbox đổi màu tương ứng 
- Nhấn nút Xóa thì trở lại vị trí ban đầu 
- Nhấn nút Thoát thì thoát ra khỏi giao diện đồ họa 
============================ Đáp án mẫu xuất file 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; // khai bao thu vien cho setBounds 
public class DoHoa1 extends JFrame implements ActionListener 
{ 
 JLabel tieude=new JLabel(); 
 JTextField khung=new JTextField(); 
 JButton btchao=new JButton(); 
 JButton btxoa=new JButton(); 
 JButton btthoat=new JButton(); 
 JButton btBlue=new JButton(); 
 JButton btRed=new JButton(); 
 JButton btGreen=new JButton(); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 18 
 public DoHoa1() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); // cap may cho khung 
 tieude.setOpaque(true); //cho phep hien thi mau 
 tieude.setBounds(new Rectangle(200,60,100,30)); // Ke khung : (cot,dong,dai,rong) 
 tieude.setText(" Lop C10MT"); 
 this.getContentPane().add(tieude); 
 khung.setOpaque(true); 
 khung.setBounds(new Rectangle(120,150,250,30)); 
 this.getContentPane().add(khung); 
 btchao.setBackground(Color.yellow); 
 btchao.setOpaque(true); 
 btchao.setBounds(new Rectangle(100,200,80,30)); 
 btchao.setText("Chao"); 
 this.getContentPane().add(btchao); 
 btchao.addActionListener(this); 
 btxoa.setBackground(Color.yellow); 
 btxoa.setOpaque(true); 
 btxoa.setBounds(new Rectangle(200,200,80,30)); 
 btxoa.setText("Xoa"); 
 this.getContentPane().add(btxoa); 
 btxoa.addActionListener(this); 
 btthoat.setBackground(Color.yellow); 
 btthoat.setOpaque(true); 
 btthoat.setBounds(new Rectangle(300,200,80,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 btRed.setBackground(Color.red); 
 btRed.setOpaque(true); 
 btRed.setBounds(new Rectangle(100,250,80,30)); 
 btRed.setText("Red"); 
 this.getContentPane().add(btRed); 
 btRed.addActionListener(this); 
 btBlue.setBackground(Color.blue); 
 btBlue.setOpaque(true); 
 btBlue.setBounds(new Rectangle(200,250,80,30)); 
 btBlue.setText("Blue"); 
 this.getContentPane().add(btBlue); 
 btBlue.addActionListener(this); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 19 
 btGreen.setBackground(Color.green); 
 btGreen.setOpaque(true); 
 btGreen.setBounds(new Rectangle(300,250,80,30)); 
 btGreen.setText("Green"); 
 this.getContentPane().add(btGreen); 
 btGreen.addActionListener(this); 
 } 
 public void actionPerformed(ActionEvent e) 
 { 
 if(e.getSource()==btchao) 
 khung.setText("www.tamga.tk xin chao ban !"); 
 else if(e.getSource()==btRed) 
 { 
 khung.setBackground(Color.red); 
 khung.setOpaque(true); 
 } 
 else if(e.getSource()==btBlue) 
 { 
 khung.setBackground(Color.blue); 
 khung.setOpaque(true); 
 } 
 else if(e.getSource()==btGreen) 
 { 
 khung.setBackground(Color.green); 
 khung.setOpaque(true); 
 } 
 else if(e.getSource()==btxoa) 
 { 
 khung.setText(""); 
 khung.setBackground(Color.white); 
 khung.setOpaque(true); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoa1 f=new DoHoa1(); 
 f.setSize(500,400); 
 f.show(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 20 
*** Bài 10: Hãy thiết kế giao diện đồ họa Giải Phương Trình Bậc 1 
 Đáp án mẫu xuất file 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaGiaiPTBac1 extends JFrame implements ActionListener 
{ 
 JLabel tieude=new JLabel(); 
 JLabel hsa=new JLabel(); 
 JLabel hsb=new JLabel(); 
 JLabel ketqua=new JLabel(); 
 JTextField khunghsa=new JTextField(); 
 JTextField khunghsb=new JTextField(); 
 JTextField khungketqua=new JTextField(); 
 JButton btthuchien=new JButton(); 
 JButton btxoa=new JButton(); 
 JButton btthoat=new JButton(); 
 Double a,b,c; 
 public DoHoaGiaiPTBac1() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); // cap may cho khung 
 tieude.setOpaque(true); //cho phep hien thi mau 
 tieude.setBounds(new Rectangle(190,60,140,30)); // Ke khung : (cot,dong,dai,rong) 
 tieude.setText(" Giai Phuong Trinh Bac 1"); 
 this.getContentPane().add(tieude); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 21 
 //----- He so a ------- www.tamga.tk 
 hsa.setBounds(new Rectangle(90,110,140,30)); 
 hsa.setText("He so A :"); 
 this.getContentPane().add(hsa); 
 khunghsa.setBounds(new Rectangle(150,115,40,20)); 
 this.getContentPane().add(khunghsa); 
 //----- He so b ------- www.tamga.tk 
 hsb.setBounds(new Rectangle(90,140,140,30)); 
 hsb.setText("He so B :"); 
 this.getContentPane().add(hsb); 
 khunghsb.setBounds(new Rectangle(150,145,40,20)); 
 this.getContentPane().add(khunghsb); 
 //----- Ket qua ------- www.tamga.tk 
 ketqua.setBounds(new Rectangle(90,170,140,30)); 
 ketqua.setText("Ket qua :"); 
 this.getContentPane().add(ketqua); 
 khungketqua.setBounds(new Rectangle(150,175,160,20)); 
 this.getContentPane().add(khungketqua); 
 //----- Button Xoa , Thuc Hien , Thoat ------- www.tamga.tk 
 btthuchien.setBackground(Color.yellow); 
 btthuchien.setOpaque(true); 
 btthuchien.setBounds(new Rectangle(100,200,100,30)); 
 btthuchien.setText("Thuc hien"); 
 this.getContentPane().add(btthuchien); 
 btthuchien.addActionListener(this); 
 btxoa.setBackground(Color.yellow); 
 btxoa.setOpaque(true); 
 btxoa.setBounds(new Rectangle(220,200,80,30)); 
 btxoa.setText("Xoa"); 
 this.getContentPane().add(btxoa); 
 btxoa.addActionListener(this); 
 btthoat.setBackground(Color.yellow); 
 btthoat.setOpaque(true); 
 btthoat.setBounds(new Rectangle(320,200,80,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 22 
 public void actionPerformed(ActionEvent e) 
 { 
 if(e.getSource()==btthuchien) 
 { 
 String s1=khunghsa.getText(); 
 a=Double.valueOf(s1); 
 String s2=khunghsb.getText(); 
 b=Double.valueOf(s2); 
 if (a==0) 
 { 
 khungketqua.setText("Phuong trinh vo nghiem"); 
 } 
 else 
 { 
 c=(-b)/a; 
 String s3=String.valueOf(c); 
 khungketqua.setText(s3); 
 } 
 } 
 else if(e.getSource()==btxoa) 
 { 
 khungketqua.setText(""); 
 khunghsa.setText(""); 
 khunghsb.setText(""); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaGiaiPTBac1 f=new DoHoaGiaiPTBac1(); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 23 
*** Bài 11: Hãy thiết kế giao diện đồ họa Giải Phương Trình Bậc 2 
*** Đáp án mẫu xuất file 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaGiaiPTBac2 extends JFrame implements ActionListener 
{ 
 Double a,b,c,d,delta,x1,x2; 
 JLabel tieude=new JLabel(); 
 JLabel hsa=new JLabel(); 
 JLabel hsb=new JLabel(); 
 JLabel hsc=new JLabel(); 
 JLabel ketqua=new JLabel(); 
 JTextField khunghsa=new JTextField(); 
 JTextField khunghsb=new JTextField(); 
 JTextField khunghsc=new JTextField(); 
 JTextField khungketqua=new JTextField(); 
 JButton btthuchien=new JButton(); 
 JButton btxoa=new JButton(); 
 JButton btthoat=new JButton(); 
 public DoHoaGiaiPTBac2() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); // cap may cho khung 
 tieude.setOpaque(true); //cho phep hien thi mau 
 tieude.setBounds(new Rectangle(190,60,140,30)); 
 tieude.setText(" Giai Phuong Trinh Bac 2"); 
 this.getContentPane().add(tieude); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 24 
 //----- He so a ------- www.tamga.tk 
 hsa.setBounds(new Rectangle(90,110,140,30)); 
 hsa.setText("He so A :"); 
 this.getContentPane().add(hsa); 
 khunghsa.setBounds(new Rectangle(150,115,30,20)); 
 this.getContentPane().add(khunghsa); 
 //----- He so b ------- www.tamga.tk 
 hsb.setBounds(new Rectangle(180,110,140,30)); 
 hsb.setText("He so B :"); 
 this.getContentPane().add(hsb); 
 khunghsb.setBounds(new Rectangle(240,115,30,20)); 
 this.getContentPane().add(khunghsb); 
 //----- He so c ------- www.tamga.tk 
 hsc.setBounds(new Rectangle(270,110,140,30)); 
 hsc.setText("He so C :"); 
 this.getContentPane().add(hsc); 
 khunghsc.setBounds(new Rectangle(330,115,30,20)); 
 this.getContentPane().add(khunghsc); 
 //----- Ket qua ------- www.tamga.tk 
 ketqua.setBounds(new Rectangle(90,150,140,30)); 
 ketqua.setText("Ket qua :"); 
 this.getContentPane().add(ketqua); 
 khungketqua.setBounds(new Rectangle(150,155,200,20)); 
 this.getContentPane().add(khungketqua); 
 //----- Button Xoa , Thuc Hien , Thoat ------- www.tamga.tk 
 btthuchien.setBackground(Color.yellow); 
 btthuchien.setOpaque(true); 
 btthuchien.setBounds(new Rectangle(100,200,100,30)); 
 btthuchien.setText("Thuc hien"); 
 this.getContentPane().add(btthuchien); 
 btthuchien.addActionListener(this); 
 btxoa.setBackground(Color.yellow); 
 btxoa.setOpaque(true); 
 btxoa.setBounds(new Rectangle(220,200,80,30)); 
 btxoa.setText("Xoa"); 
 this.getContentPane().add(btxoa); 
 btxoa.addActionListener(this); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 25 
 btthoat.setBackground(Color.yellow); 
 btthoat.setOpaque(true); 
 btthoat.setBounds(new Rectangle(320,200,80,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
 public void actionPerformed(ActionEvent e) 
 { 
 if(e.getSource()==btthuchien) 
 { 
 String s1,s2,s3,s4; 
 s1=khunghsa.getText(); 
 a=Double.valueOf(s1); 
 s2=khunghsb.getText(); 
 b=Double.valueOf(s2); 
 s3=khunghsc.getText(); 
 c=Double.valueOf(s3); 
 if (a==0) 
 { 
 d=(-c)/b; 
 s4=String.valueOf(d); 
 khungketqua.setText(s4); 
 } 
 else 
 { 
 delta=(b*b)-(4*a*c); 
 if(delta<0) 
 khungketqua.setText("Phuong trinh vo nghiem"); 
 else 
 { 
 if(delta==0) 
 { 
 d=(-b)/(2*a); 
 s4=String.valueOf(d); 
 khungketqua.setText("PT co nghiem x = "+ s4); 
 } 
 else 
 { 
 x1=(-b+Math.sqrt(delta))/(2*a); 
 x2=(-b-Math.sqrt(delta))/(2*a); 
 khungketqua.setText("PT co nghiem x1= "+x1+" va x2="+x2); 
 } 
 } 
 } 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 26 
 else if(e.getSource()==btxoa) 
 { 
 khungketqua.setText(""); 
 khunghsa.setText(""); 
 khunghsb.setText(""); 
 khunghsc.setText(""); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaGiaiPTBac2 f=new DoHoaGiaiPTBac2(); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
*** Bài 12: Hãy thiết kế giao diện đồ họa 
Các Phép Tính cộng, trừ , nhân, chia 
*** Đáp án mẫu xuất file 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 27 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaPhepTinh extends JFrame implements ActionListener 
{ 
 Double a,b,c ; 
 JLabel tieude=new JLabel(); 
 JLabel so1=new JLabel(); 
 JLabel so2=new JLabel(); 
 JLabel ketqua=new JLabel(); 
 JTextField khungso1=new JTextField(); 
 JTextField khungso2=new JTextField(); 
 JTextField khungketqua=new JTextField(); 
 JButton btcong=new JButton(); 
 JButton bttru=new JButton(); 
 JButton btnhan=new JButton(); 
 JButton btchia=new JButton(); 
 JButton btxoa=new JButton(); 
 JButton btthoat=new JButton(); 
 public DoHoaPhepTinh() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); 
 tieude.setOpaque(true); 
 tieude.setBounds(new Rectangle(190,60,130,30)); 
 tieude.setText(" Cac Phep Tinh Co Ban"); 
 this.getContentPane().add(tieude); 
 //----- He so 1 ------- www.tamga.tk 
 so1.setBounds(new Rectangle(90,110,140,30)); 
 so1.setText("Nhap so thu 1 :"); 
 this.getContentPane().add(so1); 
 khungso1.setBounds(new Rectangle(180,115,40,20)); 
 this.getContentPane().add(khungso1); 
 //----- He so 2 ------- www.tamga.tk 
 so2.setBounds(new Rectangle(90,140,140,30)); 
 so2.setText("Nhap so thu 2 :"); 
 this.getContentPane().add(so2); 
 khungso2.setBounds(new Rectangle(180,145,40,20)); 
 this.getContentPane().add(khungso2); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 28 
 //----- Ket qua ------- www.tamga.tk 
 ketqua.setBounds(new Rectangle(90,170,140,30)); 
 ketqua.setText("Ket qua :"); 
 this.getContentPane().add(ketqua); 
 khungketqua.setBounds(new Rectangle(150,175,160,20)); 
 this.getContentPane().add(khungketqua); 
 //----- Button Xoa , Thuc Hien , Thoat ------- www.tamga.tk 
 btcong.setBackground(Color.yellow); 
 btcong.setOpaque(true); 
 btcong.setBounds(new Rectangle(100,210,70,30)); 
 btcong.setText("Cong"); 
 this.getContentPane().add(btcong); 
 btcong.addActionListener(this); 
 bttru.setBackground(Color.yellow); 
 bttru.setOpaque(true); 
 bttru.setBounds(new Rectangle(180,210,70,30)); 
 bttru.setText("Tru"); 
 this.getContentPane().add(bttru); 
 bttru.addActionListener(this); 
 btnhan.setBackground(Color.yellow); 
 btnhan.setOpaque(true); 
 btnhan.setBounds(new Rectangle(260,210,70,30)); 
 btnhan.setText("Nhan"); 
 this.getContentPane().add(btnhan); 
 btnhan.addActionListener(this); 
 btchia.setBackground(Color.yellow); 
 btchia.setOpaque(true); 
 btchia.setBounds(new Rectangle(340,210,70,30)); 
 btchia.setText("Chia"); 
 this.getContentPane().add(btchia); 
 btchia.addActionListener(this); 
 btxoa.setBackground(Color.yellow); 
 btxoa.setOpaque(true); 
 btxoa.setBounds(new Rectangle(150,250,80,30)); 
 btxoa.setText("Xoa"); 
 this.getContentPane().add(btxoa); 
 btxoa.addActionListener(this); 
 btthoat.setBackground(Color.yellow); 
 btthoat.setOpaque(true); 
 btthoat.setBounds(new Rectangle(250,250,80,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 29 
 public void actionPerformed(ActionEvent e) 
 { 
 if(e.getSource()==btcong) 
 { 
 String s1=khungso1.getText(); 
 a=Double.valueOf(s1); 
 String s2=khungso2.getText(); 
 b=Double.valueOf(s2); 
 c=a+b; 
 String s3=String.valueOf(c); 
 khungketqua.setText(s3); 
 } 
 else if(e.getSource()==bttru) 
 { 
 String s1=khungso1.getText(); 
 a=Double.valueOf(s1); 
 String s2=khungso2.getText(); 
 b=Double.valueOf(s2); 
 c=a-b; 
 String s3=String.valueOf(c); 
 khungketqua.setText(s3); 
 } 
 else if(e.getSource()==btnhan) 
 { 
 String s1=khungso1.getText(); 
 a=Double.valueOf(s1); 
 String s2=khungso2.getText(); 
 b=Double.valueOf(s2); 
 c=a*b; 
 String s3=String.valueOf(c); 
 khungketqua.setText(s3); 
 } 
 else if(e.getSource()==btchia) 
 { 
 String s1=khungso1.getText(); 
 a=Double.valueOf(s1); 
 String s2=khungso2.getText(); 
 b=Double.valueOf(s2); 
 c=a/b; 
 String s3=String.valueOf(c); 
 khungketqua.setText(s3); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 30 
 else if(e.getSource()==btxoa) 
 { 
 khungso1.setText(""); 
 khungso2.setText(""); 
 khungketqua.setText(""); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaPhepTinh f=new DoHoaPhepTinh(); 
 f.setSize(500,350); 
 f.show(); 
 } 
} 
*** Bài 13: Hãy thiết kế giao diện đồ họa 
Tính chu vi và diện tích Hình Chữ Nhật 
*** Đáp án mẫu xuất file 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 31 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaChuViDienTichHCN extends JFrame implements ActionListener 
{ 
 Integer a,b,cv,dt; 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel canha=new JLabel(); 
 JLabel canhb=new JLabel(); 
 JLabel chuvi=new JLabel(); 
 JLabel dientich=new JLabel(); 
 JTextField khunga=new JTextField(); 
 JTextField khungb=new JTextField(); 
 JTextField khungchuvi=new JTextField(); 
 JTextField khungdientich=new JTextField(); 
 JButton btthuchien=new JButton(); 
 JButton bttieptuc=new JButton(); 
 JButton btthoat=new JButton(); 
 public DoHoaChuViDienTichHCN() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); 
 tieude.setOpaque(true); 
 tieude.setBounds(new Rectangle (140,30,220,30)); 
 tieude.setText("Tinh Chu Vi va Dien Tich Hinh Chu Nhat"); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle (210,60,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 // www.tamga.tk ------- canh A 
 canha.setBounds(new Rectangle(100,90,140,30)); 
 canha.setText("Canh A: "); 
 this.getContentPane().add(canha); 
 khunga.setBounds(new Rectangle(150,90,40,25)); 
 this.getContentPane().add(khunga); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 32 
 // www.tamga.tk ------- canh B 
 canhb.setBounds(new Rectangle(100,120,140,30)); 
 canhb.setText("Canh B: "); 
 this.getContentPane().add(canhb); 
 khungb.setBounds(new Rectangle(150,120,40,25)); 
 this.getContentPane().add(khungb); 
 // www.tamga.tk ------- Chu Vi 
 chuvi.setBounds(new Rectangle(160,150,140,30)); 
 chuvi.setText("Chu vi: "); 
 this.getContentPane().add(chuvi); 
 khungchuvi.setBounds(new Rectangle(200,150,40,25)); 
 this.getContentPane().add(khungchuvi); 
 // www.tamga.tk ------- Dien Tich 
 dientich.setBounds(new Rectangle(244,150,140,30)); 
 dientich.setText("Dien tich: "); 
 this.getContentPane().add(dientich); 
 khungdientich.setBounds(new Rectangle(300,150,40,25)); 
 this.getContentPane().add(khungdientich); 
 // www.tamga.tk ------- Button 
 btthuchien.setBounds(new Rectangle(100,180,90,30)); 
 btthuchien.setText("Thuc hien"); 
 this.getContentPane().add(btthuchien); 
 btthuchien.addActionListener(this); 
 bttieptuc.setBounds(new Rectangle(200,180,90,30)); 
 bttieptuc.setText("Tiep Tuc"); 
 this.getContentPane().add(bttieptuc); 
 bttieptuc.addActionListener(this); 
 btthoat.setBounds(new Rectangle(300,180,90,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 33 
 public void actionPerformed(ActionEvent nut) 
 { 
 if (nut.getSource()==btthuchien) 
 { 
 String s1,s2,s3,s4; 
 s1=khunga.getText(); a=Integer.parseInt(s1); 
 s2=khungb.getText(); b=Integer.parseInt(s2); 
 cv=(a+b)*2; 
 s4=String.valueOf(cv); 
 khungchuvi.setText(s4); 
 dt=a*b; 
 s4=String.valueOf(dt); 
 khungdientich.setText(s4); 
 } 
 else if(nut.getSource()==bttieptuc) 
 { 
 khunga.setText(""); 
 khungb.setText(""); 
 khungchuvi.setText(""); 
 khungdientich.setText(""); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaChuViDienTichHCN f=new DoHoaChuViDienTichHCN(); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 34 
*** Bài 14: Hãy thiết kế giao diện đồ họa 
Tính max và min của 4 số bất kỳ a,b,c,d 
*** Đáp án mẫu xuất file 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaMaxMin extends JFrame implements ActionListener 
{ 
 Integer a,b,c,d,max,min; 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel nhapa=new JLabel(); 
 JLabel nhapb=new JLabel(); 
 JLabel nhapc=new JLabel(); 
 JLabel nhapd=new JLabel(); 
 JLabel tenmax=new JLabel(); 
 JLabel tenmin=new JLabel(); 
 JTextField khunga=new JTextField(); 
 JTextField khungb=new JTextField(); 
 JTextField khungc=new JTextField(); 
 JTextField khungd=new JTextField(); 
 JTextField khungmax=new JTextField(); 
 JTextField khungmin=new JTextField(); 
 JButton btthuchien=new JButton(); 
 JButton bttieptuc=new JButton(); 
 JButton btthoat=new JButton(); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 35 
 public DoHoaMaxMin() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); 
 tieude.setOpaque(true); 
 tieude.setBounds(new Rectangle (210,30,95,30)); 
 tieude.setText("Tinh Max va Min"); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle (210,60,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 // www.tamga.tk ------- nhap A 
 nhapa.setBounds(new Rectangle(100,90,140,30)); 
 nhapa.setText("Nhap A "); 
 this.getContentPane().add(nhapa); 
 khunga.setBounds(new Rectangle(150,90,40,25)); 
 this.getContentPane().add(khunga); 
 // www.tamga.tk ------- nhap B 
 nhapb.setBounds(new Rectangle(100,120,140,30)); 
 nhapb.setText("Nhap B "); 
 this.getContentPane().add(nhapb); 
 khungb.setBounds(new Rectangle(150,120,40,25)); 
 this.getContentPane().add(khungb); 
 // www.tamga.tk ------- nhap C 
 nhapc.setBounds(new Rectangle(100,150,140,30)); 
 nhapc.setText("Nhap C "); 
 this.getContentPane().add(nhapc); 
 khungc.setBounds(new Rectangle(150,150,40,25)); 
 this.getContentPane().add(khungc); 
 // www.tamga.tk ------- nhap D 
 nhapd.setBounds(new Rectangle(100,180,140,30)); 
 nhapd.setText("Nhap D "); 
 this.getContentPane().add(nhapd); 
 khungd.setBounds(new Rectangle(150,180,40,25)); 
 this.getContentPane().add(khungd); 
 // www.tamga.tk ------- Max 
 tenmax.setBounds(new Rectangle(224,120,140,30)); 
 tenmax.setText("Max "); 
 this.getContentPane().add(tenmax); 
 khungmax.setBounds(new Rectangle(260,120,40,25)); 
 this.getContentPane().add(khungmax); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 36 
 // www.tamga.tk ------- Min 
 tenmin.setBounds(new Rectangle(224,150,140,30)); 
 tenmin.setText("Min "); 
 this.getContentPane().add(tenmin); 
 khungmin.setBounds(new Rectangle(260,150,40,25)); 
 this.getContentPane().add(khungmin); 
 // www.tamga.tk ------- Button 
 btthuchien.setBounds(new Rectangle(350,90,90,30)); 
 btthuchien.setText("Thuc hien"); 
 this.getContentPane().add(btthuchien); 
 btthuchien.addActionListener(this); 
 bttieptuc.setBounds(new Rectangle(350,130,90,30)); 
 bttieptuc.setText("Tiep Tuc"); 
 this.getContentPane().add(bttieptuc); 
 bttieptuc.addActionListener(this); 
 btthoat.setBounds(new Rectangle(350,170,90,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
 public void actionPerformed(ActionEvent nut) 
 { 
 if (nut.getSource()==btthuchien) 
 { 
 String s1,s2,s3,s4,s5; 
 s1=khunga.getText(); a=Integer.parseInt(s1); 
 s2=khungb.getText(); b=Integer.parseInt(s2); 
 s3=khungc.getText(); c=Integer.parseInt(s3); 
 s4=khungd.getText(); d=Integer.parseInt(s4); 
 max=a; 
 if(max<b) max=b; 
 if(max<c) max=c; 
 if(max<d) max=d; 
 s5=String.valueOf(max); 
 khungmax.setText(s5); 
 min=a; 
 if(min>b) min=b; 
 if(min>c) min=c; 
 if(min>d) min=d; 
 s5=String.valueOf(min); 
 khungmin.setText(s5); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 37 
 else if(nut.getSource()==bttieptuc) 
 { 
 khunga.setText(""); 
 khungb.setText(""); 
 khungc.setText(""); 
 khungd.setText(""); 
 khungmax.setText(""); 
 khungmin.setText(""); 
 } 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaMaxMin f=new DoHoaMaxMin(); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
*** Bài 15: Hãy thiết kế giao diện đồ họa Thay Đổi Màu Nền Background 
Gồm có các nút thuộc dạng Radio Button 
*** Đáp án mẫu xuất file 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 38 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class DoHoaRadioButton extends JFrame implements ItemListener 
{ 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JRadioButton jRadioButton1 =new JRadioButton("Yellow"); 
 JRadioButton jRadioButton2 =new JRadioButton("Cyan"); 
 JRadioButton jRadioButton3 =new JRadioButton("Green"); 
 JRadioButton jRadioButton4 =new JRadioButton("Thoat"); 
 ButtonGroup buttonGroup1=new ButtonGroup(); 
 public DoHoaRadioButton() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); 
 tieude.setOpaque(true); 
 tieude.setBounds(new Rectangle (160,30,180,30)); 
 tieude.setText(" Radio Button Mau Background"); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle (210,60,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 // www.tamga.tk ------- Mau Yellow 
 jRadioButton1.setOpaque(false); 
 jRadioButton1.setBounds(new Rectangle(100,90,140,30)); 
 jRadioButton1.addItemListener(this); 
 this.getContentPane().add(jRadioButton1); 
 // www.tamga.tk ------- Mau Cyan 
 jRadioButton2.setOpaque(false); 
 jRadioButton2.setBounds(new Rectangle(100,120,140,30)); 
 jRadioButton2.addItemListener(this); 
 this.getContentPane().add(jRadioButton2); 
 // www.tamga.tk ------- Mau Green 
 jRadioButton3.setOpaque(false); 
 jRadioButton3.setBounds(new Rectangle(100,150,140,30)); 
 jRadioButton3.addItemListener(this); 
 this.getContentPane().add(jRadioButton3); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 39 
 // www.tamga.tk ------- Thoat 
 jRadioButton4.setOpaque(false); 
 jRadioButton4.setBounds(new Rectangle(100,180,140,30)); 
 jRadioButton4.addItemListener(this); 
 this.getContentPane().add(jRadioButton4); 
 // www.tamga.tk ------- Cau hinh mac dinh nut chon 1 trong 3 
 buttonGroup1.add(jRadioButton1); 
 buttonGroup1.add(jRadioButton2); 
 buttonGroup1.add(jRadioButton3); 
 buttonGroup1.add(jRadioButton4); 
 } 
 public void itemStateChanged(ItemEvent nut) 
 { 
 if(jRadioButton1.isSelected()) 
 this.getContentPane().setBackground(Color.yellow); 
 if(jRadioButton2.isSelected()) 
 this.getContentPane().setBackground(Color.cyan); 
 if(jRadioButton3.isSelected()) 
 this.getContentPane().setBackground(Color.green); 
 if(jRadioButton4.isSelected()) 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaRadioButton f=new DoHoaRadioButton(); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 40 
*** Bài 16: Hãy thiết kế giao diện đồ họa Nhập Xuất Mảng 1 Chiều 
Với các yêu cầu sau : 
- Nhập một mảng bất kỳ 
- Tính tổng các phần tử trong mảng 
- Sắp xếp lại mảng tăng dần 
*** Đáp án xuất file 
============================ 
Và đây là code trong void main : 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JOptionPane; 
public class DoHoaMang extends JFrame implements ActionListener 
{ 
 String s2,s3,s4,s=" "; int i=0,n; 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel nhapsomang=new JLabel(); 
 JLabel nhapptmang=new JLabel(); 
 JLabel xuatmang=new JLabel(); 
 JLabel tinhtong=new JLabel(); 
 JLabel sapxep=new JLabel(); 
 JTextField khungsomang=new JTextField(); 
 JTextField khungptmang=new JTextField(); 
 JTextField khungxuatmang=new JTextField(); 
 JTextField khungtong=new JTextField(); 
 JTextField khungsapxep=new JTextField(); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 41 
 JButton btnhap=new JButton(); 
 JButton btthuchien=new JButton(); 
 JButton btxoa=new JButton(); 
 JButton btthoat=new JButton(); 
 public DoHoaMang(String title) 
 { super(title); 
 this.getContentPane().setLayout(null); 
 tieude.setBackground(Color.cyan); 
 tieude.setOpaque(true); 
 tieude.setBounds(new Rectangle(180,20,140,30)); 
 tieude.setText(" Chuong Trinh Tinh Mang"); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle(210,50,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 //----- nhap so phan tu ------- www.tamga.tk 
 nhapsomang.setBounds(new Rectangle(50,80,140,30)); 
 nhapsomang.setText("Nhap so pt mang:"); 
 this.getContentPane().add(nhapsomang); 
 khungsomang.setBounds(new Rectangle(150,80,40,25)); 
 this.getContentPane().add(khungsomang); 
 //----- Nhap phan tu ------- www.tamga.tk 
 nhapptmang.setBounds(new Rectangle(210,80,140,30)); 
 nhapptmang.setText("Nhap phan tu :"); 
 this.getContentPane().add(nhapptmang); 
 khungptmang.setBounds(new Rectangle(300,80,40,25)); 
 this.getContentPane().add(khungptmang); 
 //----- Mang da nhap ------- www.tamga.tk 
 xuatmang.setBounds(new Rectangle(50,120,140,30)); 
 xuatmang.setText("Mang da nhap :"); 
 this.getContentPane().add(xuatmang); 
 khungxuatmang.setBounds(new Rectangle(150,120,190,25)); 
 this.getContentPane().add(khungxuatmang); 
 //----- Tinh tong ------- www.tamga.tk 
 tinhtong.setBounds(new Rectangle(50,160,140,30)); 
 tinhtong.setText("Tinh tong :"); 
 this.getContentPane().add(tinhtong); 
 khungtong.setBounds(new Rectangle(150,160,40,25)); 
 this.getContentPane().add(khungtong); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 42 
 // -------Sap xep ------- www.tamga.tk 
 sapxep.setBounds(new Rectangle(50,200,140,30)); 
 sapxep.setText("Sap Xep"); 
 this.getContentPane().add(sapxep); 
 khungsapxep.setBounds(new Rectangle(150,200,190,25)); 
 this.getContentPane().add(khungsapxep); 
 // ------- Button------- www.tamga.tk 
 btnhap.setBounds(new Rectangle(350,80,90,30)); 
 btnhap.setText("Nhap"); 
 this.getContentPane().add(btnhap); 
 btnhap.addActionListener(this); 
 btthuchien.setBounds(new Rectangle(350,120,90,30)); 
 btthuchien.setText("Thuc hien"); 
 this.getContentPane().add(btthuchien); 
 btthuchien.addActionListener(this); 
 btxoa.setBounds(new Rectangle(350,160,90,30)); 
 btxoa.setText("Xoa"); 
 this.getContentPane().add(btxoa); 
 btxoa.addActionListener(this); 
 btthoat.setBounds(new Rectangle(350,200,90,30)); 
 btthoat.setText("Thoat"); 
 this.getContentPane().add(btthoat); 
 btthoat.addActionListener(this); 
 } 
 int b[]=new int[10]; 
 public void actionPerformed(ActionEvent nut) 
 { 
 this.n=Integer.parseInt(khungsomang.getText()); 
 int a[] = new int[n]; 
 // ------ Nhap Mang --- www.tamga85.multiply.com 
 if(nut.getSource()==btnhap) 
 { 
 int s2= Integer.parseInt(khungptmang.getText()); // string -> int 
 if(i < n) 
 { 
 /* 
 a[i++] = s2; 
 s=this.khungxuatmang.getText() + " " + s2; 
 this.khungxuatmang.setText(s); 
 */ 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 43 
 s=khungptmang.getText(); 
 b[i]=Integer.valueOf(s); 
 s3=this.khungxuatmang.getText() + " " + s2; 
 this.khungxuatmang.setText(s3); 
 i++; 
 } 
 } 
 // ------- Tong & Sap Xep --- www.tamga85.multiply.com 
 else if(nut.getSource()==btthuchien) 
 { 
 // --- Tong 
 int sum=0; 
 for(i=0;i<n;i++) sum=sum+b[i]; 
 khungtong.setText(String.valueOf(sum)); 
 // --- Sap Xep 
 int temp; 
 String sapxep = ""; 
 for(i=0;i<n;i++) 
 { khungsapxep.setText(khungsapxep.getText()+" "+String.valueOf(b[i])); } 
 for(int i = 0; i < n-1; i++) 
 { for(int j = i+1; j < n; j++) 
 { 
 if(b[i] > b[j]) 
 { temp = b[i]; 
 b[i] = b[j]; 
 b[j] = temp; 
 } 
 } 
 } 
 for(int i=0 ; i< n ; i++) 
 { sapxep += " "+b[i]; } 
 this.khungsapxep.setText(sapxep); 
 } 
 // ------- Xoa --- www.tamga85.multiply.com 
 else if (nut.getSource()== btxoa) 
 { 
 khungsomang.setText(""); 
 khungptmang.setText(""); 
 khungxuatmang.setText(""); 
 khungtong.setText(""); 
 khungsapxep.setText(""); 
 i=0;n=0; 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 44 
 else 
 { 
 System.exit(0); 
 this.dispose(); 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaMang f=new DoHoaMang("TamGa - www.tamga85.multiply.com"); 
 f.setSize(500,300); 
 f.show(); 
 } 
} 
*** Bài 17: Hãy thiết kế giao diện đồ họa 
Với các yêu cầu sau : 
- Dùng Radio Button 
- Tính các phép tính cơ bản như : cộng , trừ , nhân , chia 
*** Đáp án xuất file 
============================ 
Và đây là code trong void main : 
/* 
 TamGa 
 www.tamga.tk www.c10mt.tk www.c10maytinh.tk 
 www.tamga85.multiply.com 
 Phone: 01283.98.69.98 Email : 
[email protected] 
 Bai tap : Thiet Ke Giao Dien Do Hoa dung Radio Button 
 Nhap & tinh cac phep toan co ban 
 */ 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 45 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.Font.*; 
public class DoHoaRadioPhepTinhCoBan extends JFrame 
{ 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel lso1=new JLabel(); 
 JLabel lso2=new JLabel(); 
 JLabel lketqua=new JLabel(); 
 JTextField so1=new JTextField(); 
 JTextField so2=new JTextField(); 
 JTextField ketqua=new JTextField(); 
 JRadioButton cong=new JRadioButton("Cong"); 
 JRadioButton tru=new JRadioButton("Tru"); 
 JRadioButton nhan=new JRadioButton("Nhan"); 
 JRadioButton chia=new JRadioButton("Chia"); 
 JButton thoat=new JButton(); 
 JButton tieptuc=new JButton(); 
 ButtonGroup group1=new ButtonGroup(); 
 Font font = new Font("Tahoma", Font.BOLD, 24); 
 public DoHoaRadioPhepTinhCoBan() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBounds(new Rectangle(50,20,350,30)); 
 tieude.setText("Radio Phep Tinh Co Ban"); 
 tieude.setFont(font); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle(150,50,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 //---- Nhap so thu nhat ----- www.tamga.tk 
 lso1.setBounds(new Rectangle(60,80,100,30)); 
 lso1.setText("Nhap so 1 :"); 
 this.getContentPane().add(lso1); 
 so1.setBounds(new Rectangle(140,80,100,25)); 
 this.getContentPane().add(so1); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 46 
 //---- Nhap so thu hai ----- www.tamga.tk 
 lso2.setBounds(new Rectangle(60,120,100,30)); 
 lso2.setText("Nhap so 2 :"); 
 this.getContentPane().add(lso2); 
 so2.setBounds(new Rectangle(140,120,100,25)); 
 this.getContentPane().add(so2); 
 //---- Ket qua ----- www.tamga.tk 
 lketqua.setBounds(new Rectangle(60,160,100,30)); 
 lketqua.setText("Ket qua :"); 
 this.getContentPane().add(lketqua); 
 ketqua.setBounds(new Rectangle(140,160,100,25)); 
 this.getContentPane().add(ketqua); 
 //---- Button ----- www.tamga.tk 
 thoat.setBounds(new Rectangle(60,200,100,30)); 
 thoat.setText("Thoat"); 
 this.getContentPane().add(thoat); 
 tieptuc.setBounds(new Rectangle(200,200,100,30)); 
 tieptuc.setText("Tiep tuc"); 
 this.getContentPane().add(tieptuc); 
 //---- Phep toan ----- www.tamga.tk 
 cong.setBounds(new Rectangle(270,70,100,30)); 
 this.getContentPane().add(cong); 
 tru.setBounds(new Rectangle(270,100,100,30)); 
 this.getContentPane().add(tru); 
 nhan.setBounds(new Rectangle(270,130,100,30)); 
 this.getContentPane().add(nhan); 
 chia.setBounds(new Rectangle(270,160,100,30)); 
 this.getContentPane().add(chia); 
 //---- Set mac dinh chon 1 trong 4 ----- 
 group1.add(cong); 
 group1.add(tru); 
 group1.add(nhan); 
 group1.add(chia); 
 // ---- Phan mac dinh Radio 
 phanradio k =new phanradio(); 
 cong.addItemListener(k); 
 tru.addItemListener(k); 
 nhan.addItemListener(k); 
 chia.addItemListener(k); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 47 
 // ---- Phan mac dinh Button 
 phanbutton j=new phanbutton(); 
 thoat.addActionListener(j); 
 tieptuc.addActionListener(j); 
 } 
 public class phanradio implements ItemListener 
 { 
 public void itemStateChanged(ItemEvent e) 
 { 
 double a,b; 
 a=Double.parseDouble(so1.getText()); 
 b=Double.parseDouble(so2.getText()); 
 String s; 
 if(e.getItem()==cong) 
 { 
 double x1; 
 x1=a+b; 
 s=String.valueOf(x1); 
 ketqua.setText(s); 
 } 
 else 
 { 
 if(e.getItem()==tru) 
 { 
 double x2; 
 x2=a-b; 
 s=String.valueOf(x2); 
 ketqua.setText(s); 
 } 
 else 
 { 
 if(e.getItem()==nhan) 
 { double x3; 
 x3=a*b; 
 s=String.valueOf(x3); 
 ketqua.setText(s); 
 } 
 else 
 { double x4; 
 x4=a/b; 
 s=String.valueOf(x4); 
 ketqua.setText(s); 
 } 
 } 
 } 
 } 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 48 
 public class phanbutton implements ActionListener 
 { 
 public void actionPerformed(ActionEvent h) 
 { 
 DoHoaRadioPhepTinhCoBan xx=new DoHoaRadioPhepTinhCoBan(); 
 if(h.getSource()==thoat) 
 { 
 System.exit(0); 
 xx.dispose(); 
 } 
 else 
 { 
 so1.setText(""); 
 so2.setText(""); 
 ketqua.setText(""); 
 } 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaRadioPhepTinhCoBan f=new DoHoaRadioPhepTinhCoBan(); 
 f.setSize(400,300); 
 f.show(); 
 } 
} 
*** Bài 18: Hãy thiết kế giao diện đồ họa 
Với các yêu cầu sau : 
- Dùng Radio Button 
- Tính các phép tính cơ bản như : cộng , trừ , nhân , chia 
- Khi click thực hiện và chọn yêu cầu từ thì mới thực hiện 
*** Đáp án xuất file 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 49 
============================ 
Và đây là code trong void main : 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.Font.*; 
public class DoHoaRadioPhepTinhCoBan2 extends JFrame 
{ 
 int tam; 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel lso1=new JLabel(); 
 JLabel lso2=new JLabel(); 
 JLabel lketqua=new JLabel(); 
 JTextField so1=new JTextField(); 
 JTextField so2=new JTextField(); 
 JTextField ketqua=new JTextField(); 
 JRadioButton cong=new JRadioButton("Cong"); 
 JRadioButton tru=new JRadioButton("Tru"); 
 JRadioButton nhan=new JRadioButton("Nhan"); 
 JRadioButton chia=new JRadioButton("Chia"); 
 JButton thoat=new JButton(); 
 JButton thuchien=new JButton(); 
 JButton tieptuc=new JButton(); 
 ButtonGroup group1=new ButtonGroup(); 
 Font font = new Font("Tahoma", Font.BOLD, 24); 
 public DoHoaRadioPhepTinhCoBan2() 
 { this.getContentPane().setLayout(null); 
 tieude.setBounds(new Rectangle(50,20,350,30)); 
 tieude.setText("Radio Phep Tinh Co Ban"); 
 tieude.setFont(font); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle(150,50,140,30)); 
 gioithieu.setText("www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 //---- Nhap so thu nhat ----- www.tamga.tk 
 lso1.setBounds(new Rectangle(60,80,100,30)); 
 lso1.setText("Nhap so 1 :"); 
 this.getContentPane().add(lso1); 
 so1.setBounds(new Rectangle(140,80,100,25)); 
 this.getContentPane().add(so1); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 50 
 //---- Nhap so thu hai ----- www.tamga.tk 
 lso2.setBounds(new Rectangle(60,120,100,30)); 
 lso2.setText("Nhap so 2 :"); 
 this.getContentPane().add(lso2); 
 so2.setBounds(new Rectangle(140,120,100,25)); 
 this.getContentPane().add(so2); 
 //---- Ket qua ----- www.tamga.tk 
 lketqua.setBounds(new Rectangle(60,160,100,30)); 
 lketqua.setText("Ket qua :"); 
 this.getContentPane().add(lketqua); 
 ketqua.setBounds(new Rectangle(140,160,100,25)); 
 this.getContentPane().add(ketqua); 
 //---- Button ----- www.tamga.tk 
 thuchien.setBounds(new Rectangle(40,200,90,30)); 
 thuchien.setText("Thuc Hien"); 
 this.getContentPane().add(thuchien); 
 tieptuc.setBounds(new Rectangle(145,200,90,30)); 
 tieptuc.setText("Tiep Tuc"); 
 this.getContentPane().add(tieptuc); 
 thoat.setBounds(new Rectangle(250,200,80,30)); 
 thoat.setText("Thoat"); 
 this.getContentPane().add(thoat); 
 //---- Phep toan ----- www.tamga.tk 
 cong.setBounds(new Rectangle(270,70,100,30)); 
 this.getContentPane().add(cong); 
 tru.setBounds(new Rectangle(270,100,100,30)); 
 this.getContentPane().add(tru); 
 nhan.setBounds(new Rectangle(270,130,100,30)); 
 this.getContentPane().add(nhan); 
 chia.setBounds(new Rectangle(270,160,100,30)); 
 this.getContentPane().add(chia); 
 //---- Set mac dinh chon 1 trong 4 ----- 
 group1.add(cong); 
 group1.add(tru); 
 group1.add(nhan); 
 group1.add(chia); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 51 
 // ---- Phan mac dinh Radio 
 phanradio k =new phanradio(); 
 cong.addItemListener(k); 
 tru.addItemListener(k); 
 nhan.addItemListener(k); 
 chia.addItemListener(k); 
 // ---- Phan mac dinh Button 
 phanbutton j=new phanbutton(); 
 thoat.addActionListener(j); 
 thuchien.addActionListener(j); 
 tieptuc.addActionListener(j); 
 } 
 public class phanradio implements ItemListener 
 { 
 public void itemStateChanged(ItemEvent e) 
 { 
 double a,b; 
 a=Double.parseDouble(so1.getText()); 
 b=Double.parseDouble(so2.getText()); 
 String s; 
 if(e.getItem()==cong) 
 { 
 tam=1; 
 } 
 else 
 { 
 if(e.getItem()==tru) 
 { 
 tam=2; 
 } 
 else 
 { 
 if(e.getItem()==nhan) 
 { 
 tam=3; 
 } 
 else 
 { 
 tam=4; 
 } 
 } 
 } 
 } 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 52 
 public class phanbutton implements ActionListener 
 { public void actionPerformed(ActionEvent h) 
 { DoHoaRadioPhepTinhCoBan2 xx=new DoHoaRadioPhepTinhCoBan2(); 
 if(h.getSource()==thoat) 
 { System.exit(0); 
 xx.dispose(); 
 } 
 if(h.getSource()==tieptuc) 
 { so1.setText(""); 
 so2.setText(""); 
 ketqua.setText(""); 
 } 
 double a,b; 
 a=Double.parseDouble(so1.getText()); 
 b=Double.parseDouble(so2.getText()); 
 String s; 
 if(h.getSource()==thuchien && tam==1) 
 { double x1; 
 x1=a+b; 
 s=String.valueOf(x1); 
 ketqua.setText(s); 
 } 
 if(h.getSource()==thuchien && tam==2) 
 { double x2; 
 x2=a-b; 
 s=String.valueOf(x2); 
 ketqua.setText(s); 
 } 
 if(h.getSource()==thuchien && tam==3) 
 { double x3; 
 x3=a*b; 
 s=String.valueOf(x3); 
 ketqua.setText(s); 
 } 
 if(h.getSource()==thuchien && tam==4) 
 { double x4; 
 x4=a-b; 
 s=String.valueOf(x4); 
 ketqua.setText(s); 
 } 
 } 
 } 
 public static void main(String[] args) 
 { 
 DoHoaRadioPhepTinhCoBan2 f=new DoHoaRadioPhepTinhCoBan2(); 
 f.setSize(400,300); 
 f.show(); 
 } 
} 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 53 
*** Bài 19: Hãy thiết kế giao diện đồ họa 
Với các yêu cầu sau : 
- Dùng Radio Button 
- Giải các phương trình bậc 1 và bậc 2 
- Khi click thực hiện và chọn yêu cầu từ thì mới thực hiện 
*** Đáp án xuất file 
============================ 
Và đây là code trong void main : 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.Font.*; 
public class DoHoaRadioButtonGiaiPT extends JFrame 
{ 
 int tam; String s; 
 double a,b,c,xx1,xx2,delta,x1,x2; 
 JLabel tieude=new JLabel(); 
 JLabel gioithieu=new JLabel(); 
 JLabel hesoa=new JLabel(); 
 JLabel hesob=new JLabel(); 
 JLabel hesoc=new JLabel(); 
 JLabel ketqua=new JLabel(); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 54 
 JTextField khunghsa=new JTextField(); 
 JTextField khunghsb=new JTextField(); 
 JTextField khunghsc=new JTextField(); 
 JTextArea khungketqua=new JTextArea(); 
 JRadioButton giaiptbac1=new JRadioButton("Giai Phuong Trinh Bac 1"); 
 JRadioButton giaiptbac2=new JRadioButton("Giai Phuong Trinh Bac 2"); 
 JButton thoat=new JButton(); 
 JButton tieptuc=new JButton(); 
 JButton thuchien=new JButton(); 
 ButtonGroup group1=new ButtonGroup(); 
 Font font = new Font("Tahoma", Font.BOLD, 20); 
 public DoHoaRadioButtonGiaiPT() 
 { 
 this.getContentPane().setLayout(null); 
 tieude.setBounds(new Rectangle(30,20,350,30)); 
 tieude.setText("Giai Phuong Trinh Bac 1 va Bac 2"); 
 tieude.setFont(font); 
 this.getContentPane().add(tieude); 
 gioithieu.setBounds(new Rectangle(100,50,200,30)); 
 gioithieu.setText("Radio Button --- www.tamga.tk"); 
 this.getContentPane().add(gioithieu); 
 //---- Nhap he so a ----- www.tamga.tk 
 hesoa.setBounds(new Rectangle(30,100,100,30)); 
 hesoa.setText("Nhap he so a :"); 
 this.getContentPane().add(hesoa); 
 khunghsa.setBounds(new Rectangle(120,100,30,25)); 
 this.getContentPane().add(khunghsa); 
 //---- Nhap he so b ----- www.tamga.tk 
 hesob.setBounds(new Rectangle(30,140,100,30)); 
 hesob.setText("Nhap he so b :"); 
 this.getContentPane().add(hesob); 
 khunghsb.setBounds(new Rectangle(120,140,30,25)); 
 this.getContentPane().add(khunghsb); 
 //---- Nhap he so c ----- www.tamga.tk 
 hesoc.setBounds(new Rectangle(30,180,100,30)); 
 hesoc.setText("Nhap he so c :"); 
 this.getContentPane().add(hesoc); 
 khunghsc.setBounds(new Rectangle(120,180,30,25)); 
 this.getContentPane().add(khunghsc); 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 55 
 //---- Ket qua ----- www.tamga.tk 
 ketqua.setBounds(new Rectangle(260,100,100,30)); 
 ketqua.setText("Ket qua"); 
 this.getContentPane().add(ketqua); 
 khungketqua.setBounds(new Rectangle(190,140,180,65)); 
 this.getContentPane().add(khungketqua); 
 //---- Radio Giai Phuong Trinh ----- www.tamga.tk 
 giaiptbac1.setBounds(new Rectangle(30,220,160,30)); 
 this.getContentPane().add(giaiptbac1); 
 giaiptbac2.setBounds(new Rectangle(200,220,160,30)); 
 this.getContentPane().add(giaiptbac2); 
 //---- Button ----- www.tamga.tk 
 thuchien.setBounds(new Rectangle(30,260,100,30)); 
 thuchien.setText("Thuc hien"); 
 this.getContentPane().add(thuchien); 
 tieptuc.setBounds(new Rectangle(140,260,100,30)); 
 tieptuc.setText("Tiep tuc"); 
 this.getContentPane().add(tieptuc); 
 thoat.setBounds(new Rectangle(250,260,100,30)); 
 thoat.setText("Thoat"); 
 this.getContentPane().add(thoat); 
 //---- Set mac dinh chon 1 trong 4 ----- 
 group1.add(giaiptbac1); 
 group1.add(giaiptbac2); 
 // ---- Phan mac dinh Radio 
 phanradio k =new phanradio(); 
 giaiptbac1.addItemListener(k); 
 giaiptbac2.addItemListener(k); 
 // ---- Phan mac dinh Button 
 phanbutton j=new phanbutton(); 
 thuchien.addActionListener(j); 
 tieptuc.addActionListener(j); 
 thoat.addActionListener(j); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 56 
 public class phanradio implements ItemListener 
 { 
 public void itemStateChanged(ItemEvent e) 
 { 
 if(e.getItem()==giaiptbac1) 
 { 
 tam=1; 
 khunghsc.enable(false); 
 } 
 else //(e.getItem()==giaiptbac2) 
 { 
 tam=2; 
 khunghsc.enable(true); 
 } 
 } 
 } 
 public class phanbutton implements ActionListener 
 { 
 public void actionPerformed(ActionEvent tamga) 
 { 
 DoHoaRadioButtonGiaiPT nut=new DoHoaRadioButtonGiaiPT(); 
 if(tamga.getSource()==thoat) // ---- Phan Thoat 
 { 
 System.exit(0); 
 nut.dispose(); 
 } 
 if(tamga.getSource()==tieptuc) // ---- Phan Tiep Tuc --- www.tamga.tk 
 { 
 khunghsa.setText(""); 
 khunghsb.setText(""); 
 khunghsc.setText(""); 
 khungketqua.setText(""); 
 } 
 // ---- Phan Thuc Hien Giai Phuong Trinh Bac 1 
 if(tamga.getSource()==thuchien && tam==1) 
 { 
 a=Double.parseDouble(khunghsa.getText()); 
 b=Double.parseDouble(khunghsb.getText()); 
 if (a==0) 
 { 
 khungketqua.setText("Phuong trinh vo nghiem"); 
 } 
Tâm Gà – Phone: 01283.98.69.98 Email: 
[email protected] Site : www.tamga.tk 57 
 else 
 { xx1=(-b)/a; 
 s=String.valueOf(xx1); 
 khungketqua.setText("PT co nghiem x = "+ s); 
 } 
 } 
 // ---- Phan Thuc Hien Giai Phuong Trinh Bac 2 
 if(tamga.getSource()==thuchien && tam==2) 
 { a=Double.parseDouble(khunghsa.getText()); 
 b=Double.parseDouble(khunghsb.getText()); 
 c=Double.parseDouble(khunghsc.getText()); 
 if (a==0) 
 { 
 xx2=(-c)/b; 
 s=String.valueOf(xx2); 
 khungketqua.setText(s); 
 } 
 else 
 { 
 delta=(b*b)-(4*a*c); 
 if(delta<0) 
 khungketqua.setText("Phuong trinh vo nghiem"); 
 else 
 { 
 if(delta==0) 
 { 
 xx2=(-b)/(2*a); 
 s=String.valueOf(xx2); 
 khungketqua.setText("PT co nghiem x = "+ s); 
 } 
 else 
 { 
 x1=(-b+Math.sqrt(delta))/(2*a); 
 x2=(-b-Math.sqrt(delta))/(2*a); 
 khungketqua.setText("PT co nghiem x1= "+x1+" va x2="+x2); 
 } 
 } 
 } 
 } 
 } 
 } 
 public static void main(String[] args) 
 { DoHoaRadioButtonGiaiPT f=new DoHoaRadioButtonGiaiPT(); 
 f.setSize(400,360); 
 f.show(); 
 } 
}