1. TRƯỜNG ĐẠI HỌC BÁCH KHOA
KHOA CÔNG NGHỆ THÔNG TIN
BÁO CÁO THỰC HÀNH
LẬP TRÌNH JAVA
GVHD: Mai Văn Hà
SVTH: Nguyễn Rim
MSSV:102110156
LỚP: 11T1
NHÓM: 12A
Đà Nẵng,12/2014
2. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Buổi 1:
Code bài 1 đến bài 5:
import java.io.*;
public class class_1{
public static void giaiptbn(int a,int b)
{
if (a==0)
{
if (b==0)
{
System.out.println("Phuong trinh vo so nghiem");
}
else
{
System.out.println("Phuong trinh vo nghiem");
}
}
else
{
float tmp;
tmp=-b/(float)a;
System.out.println("Phuong trinh co mot nghiem la :"+tmp);
3. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
}
}
public static void giaibac2(int a,int b,int c)
{
if (a==0)
{
if (b==0)
{
if (c==0) System.out.println("phuong trinh vo so nghiem");
else System.out.println("Phuong trinh vo nghiem");
}
else System.out.println("Phuong trinh co 1 nghiem: "+-1.0*c/b);
}
else
{
double delta,x1,x2;
delta=b*b-4*a*c;
if (delta<0) System.out.println("Phuong trinh vo nghiem");
else
if (delta==0) System.out.println("Phuong trinh co nghiem
kep: "+-1.0*b/(2*a));
else
4. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
{
x1=(-b+Math.sqrt(delta))/(2*a);
x2=(-b-Math.sqrt(delta))/(2*a);
System.out.println("Phuong trinh co 2 nghiem.
X1="+x1+", X2="+x2);
}
}
}
//Bai3
public static void trunggian(float a,float b, float c)
{
float max, min;
max=((a>=b)?((a>=c)?a:c):((b>=c)?b:c));
min=((a<=b)?((a<=c)?a:c):((b<=c)?b:c));
if ((a>=min) && (a<=max)) System.out.println("So trung gian la" + a);
else if ((b>=min) && (b<=max)) System.out.println("So trung gian la" +
b);
else System.out.println("So trung gian la" + c);
}
//Bai 4
public static void karaoke(int a,int b)
{
int money;
5. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
if (b<=18) money=(b-a)*45000;
else if((a<=18)&&(b>=18)) money = ((b-18)*60000+(18-a)*45000);
else money=(b-a)*60000;
System.out.println("So tien Karaoke phai tra la " + money);
}
//Bai5
public static int ngaythang(int m,int y)
{
int []DayOfMonth={31,28,31,30,31,30,31,31,30,31,30,31};
if (m!=2) return DayOfMonth[m-1];
else
{
int a,b,c;
a=y%400;
b=y%4;
c=y%100;
if ((a==0)||((b==0)&&(c!=0))) return 29;
else return 28;
}
}
public static void main(String [] args)
{
class_1 A=new class();
6. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
A.giaiptbn(5,7);
A.giaibac2(2,1,-5);
A.trunggian(1,2,-5);
A.karaoke(19,22);
System.out.println("So ngay trong thang 5 nam 2015 la " +
A.ngaythang(5,2015));
}
}
Demo:
7. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Code bài 6 đến bài 11:
package thuchanh;
public class class_2 {
//Bài 6
public static void bai6(int n)
{
if (n<1) System.out.println("Nhap khong dung");
else
{
float s=0;
int i=1;
while (i<=n)
{
s+=(float)1/i;
i++;
}
System.out.println("Bai 6. Ket qua la: "+s);
}
}
//Bài 7
public static void bai7(int n)
{
//S=15-1+1/2-1/3!+....+(-1)n 1/n!;
float S=15;
int i=1,tmp=1;
while (i<=n)
{
tmp=tmp*i*(-1);
S+=1.0/tmp;
i++;
}
System.out.println("Bai 7. Ket qua la: "+S);
}
//Bài 8
public static void bai8(int n)
{
//S=1+1/3!+1/5!+…..+1/(2n-1)!
float S=1;
int i=2,tmp=1;
while (i<=n)
{
tmp=tmp*(i*2-1)*(i*2-2);
S+=1.0/tmp;
i++;
}
8. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
System.out.println("Bai 8. Ket qua la: "+S);
}
//Bài 9
public static void bai9(int n)
{
//= 1*3*5*…..*n(n le)
//= 2*4*6*….*n(n chan)
int i,T=1;
if (n%2>0) // so le
{
i=1;
}
else i=2;
while (i<=n)
{
T*=i;
i+=2;
}
System.out.println("Bai 9. Ket qua: "+T);
}
//Bài 10
public static void bai10(int n)
{
//(VD : m=234=> S=2+3+4=9, P=2*3*4=24)
int M=0,P=1,i=0,j;
int []array = new int[20];
while (n!=0)
{
array[i]=n%10;
n=n/10;
i++;
}
for (j=0;j<i;j++)
{
M+=array[j];
P*=array[j];
}
System.out.println("Bai 10. Ket qua Tong = "+M+" , Tich = "+P);
}
//Bài 11
public static void bai11(int n)
{
int tmp,i,check=0;
tmp=n/2;
for (i=2;i<tmp;i++)
9. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
if (n%i==0)
{
check = 1;
break;
}
if (check==0)
System.out.println("Bai11. " + n +" la so nguyen to");
else
System.out.println("Bai11. " + n +" khong phai la so
nguyen to");
}
//Bài 12
public static void main(String []arg)
{
class_2 A = new class_2();
A.bai6(5);
A.bai7(12);
A.bai8(10);
A.bai9(4);
A.bai10(7);
A.bai11(13);
}
}
Demo:
10. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Code bài 12 đến 18:
package thuchanh;
public class class_3 {
//Bài 12
void bai12(int n)
{
int tmp;
tmp=(int)Math.sqrt(n);
tmp*=tmp;
System.out.print("Bai12. ");
if (tmp==n)
System.out.println(n+" la so chinh phuong");
else
System.out.println(n+" khong phai la so chinh phuong");
}
//Bài 13
void bai13(int n)
{ System.out.print("Bai13. ");
System.out.printf(n+"");
if (n<10)
{
System.out.printf(" khong phai la so doi xung");
}
else
{
int []array = new int[20];
int i=0;
while (n!=0)
{
array[i]=n%10;
n=n/10;
i++;
}
int tmp1=0,tmp2=0;
if (i%2>0) //co so chu so le
{
for (int j=i/2;j>=0;j--)
{
tmp1=tmp1*10+array[j];
}
for (int j=i/2;j<i;j++)
{
tmp2=tmp2*10+array[j];
}
if (tmp1==tmp2) System.out.printf(" la so doi xung");
11. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
else System.out.printf(" khong phai la so doi xung");
}
else
{
for (int j=i/2-1;j>=0;j--)
{
tmp1=tmp1*10+array[j];
}
for (int j=i/2;j<i;j++)
{
tmp2=tmp2*10+array[j];
}
if (tmp1==tmp2) System.out.printf(" la so doi xung");
else System.out.printf(" khong phai la so doi xung");
}
}
}
//Bài 14
void bai14(int n)
{ System.out.print("Bai14. ");
if (n>10000) System.out.printf(n+" qua lon");
else
{
int []array = new int[10000];
int i,j;
for (i=1;i<=n;i++) array[i]=0;
i=2;
while (i<=n)
{
if (array[i]==0)
for (j=2;j<=n/i;j++) array[i*j]=1;
i++;
}
System.out.println("n Cac so nguyen to nho hon "+n+"
la:");
for (i=2;i<=n;i++)
if (array[i]==0) System.out.printf(i+" ");
System.out.println("");
}
}
//Bài 15
void bai15()
{
//cac so hona hao nho hon 1000
System.out.print("Bai15. ");
System.out.printf("Cac so hoan hao nho hon 1000:n");
int tmp,n=2;
while (n<=1000)
{
tmp=1;
12. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
for (int i=n/2;i>=2;i--)
if (n%i==0) tmp+=i;
if (tmp==n) System.out.printf(n+" ");
n++;
}
System.out.println("");
}
//Bài 16
void bai16(int n)
{
//in ra n chu so fibonaci dau tien
System.out.print("Bai16. ");
System.out.printf(n+" chu so Fibonaci dau tien la: 1 2 ");
int i=3,tmp1=1,tmp2=2,tmp;
while (i<=n)
{
tmp=tmp1+tmp2;
tmp1=tmp2;
tmp2=tmp;
System.out.printf(tmp+" ");
i++;
}
System.out.println("");
}
//Bài 17
void bai17(int K)
{
//kiem tra K co thuoc day fibonaci ko?
System.out.print("Bai17. ");
int tmp1=1,tmp2=2,tmp;
while (tmp2<K)
{
tmp=tmp1+tmp2;
tmp1=tmp2;
tmp2=tmp;
}
if (tmp2==K)
System.out.printf(K+" thuoc day fibonaci");
else
System.out.printf(K+" khong thuoc day fibonaci");
System.out.println("");
}
//Bài 18
void bai18(int x,int y)
{
//tim uoc chung lon nhat va boi chung nho nhat
System.out.print("Bai18. ");
int ucln,bcnn,tmp;
bcnn=x*y;
13. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
tmp= y%x;
while (tmp!=0)
{
tmp=x%y;
x=y;
y=tmp;
}
ucln=x;
System.out.println("Uoc chung lon nhat va boi chung nho nhat cua
"+x+" va "+y+" la :");
System.out.printf("UCLN = "+ucln+" ,BCNN = "+ bcnn/ucln);
System.out.println("");
}
public static void main(String []arg)
{
class_3 A = new class_3();
A.bai12(5);
A.bai13(12321);
A.bai14(100);
A.bai15();
A.bai16(100);
A.bai17(125);
A.bai18(12,56);
}
}
Demo:
14. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Buổi 2:
Bài 1:
Code:
package thuchanh;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class giaodien1 extends JFrame implements ActionListener {
private JButton tinh,reset,exit;
private JTextField gta,gtb,gtx;
private double x,a,b;
private Container cont;
private JPanelpanel1,panel2,panel3;
private JLabel tieude,num1,num2,kq;
public giaodien1 (String s)
{
super (s);
cont = this.getContentPane();
tieude = new JLabel("giai phuong trinh bac nhat");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(tieude);
num1= new JLabel("nhap a:");
15. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
num2=new JLabel("nhap b:");
kq=new JLabel("ket qua:");
gta=new JTextField();
gtb=new JTextField();
gtx=new JTextField();
gtx.setEditable(false);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(3,2));
panel2.add(num1);
panel2.add(gta);
panel2.add(num2);
panel2.add(gtb);
panel2.add(kq);
panel2.add(gtx);
tinh =new JButton("tinh");
exit =new JButton("exit");
reset =new JButton("reset");
panel3 = new JPanel();
panel3.add(tinh);
panel3.add(reset);
panel3.add(exit);
cont.add(panel1,"North");
cont.add(panel2);
cont.add(panel3,"South");
tinh.addActionListener(this);
reset.addActionListener(this);
16. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
exit.addActionListener(this);
this.pack();
this.setVisible(true);
}
public void tinh()
{
a = Double.parseDouble(gta.getText());
b = Double.parseDouble(gtb.getText());
if (a==0) gtx.setText("gia tri a khong hop le");
else {
x = -b/a;
gtx.setText(String.valueOf(x));
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()=="tinh")
{
tinh();
}
if (e.getActionCommand()=="reset")
{
a=0;
b=0;
gtb.setText(" ");
17. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
gta.setText(" ");
gtx.setText(" ");
}
if (e.getActionCommand()=="exit")
{
System.exit(1);
}
}
public static void main(String args[])
{
giaodien1 operator = new giaodien1 ("giao dien ");
}
}
Demo:
18. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 2:
Code:
package thuchanh;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
@SuppressWarnings("serial")
public class giaodien2 extends Frame implements ActionListener{
Label lb;
Label lb1;
Label lb2;
Label lb3;
static TextField txta;
static TextField txtb;
static TextField txtkq;
Button cong, tru, nhan, chia, exit, reset;
19. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Panelpn, pn1,pn2,pn3,pn4;
public void GUI()
{
lb = new Label("minh hoa cac phep toan");
lb1 = new Label("Nhap a");
lb2 = new Label("Nhap b");
lb3 = new Label("Ket qua");
txta = new TextField();
txtb = new TextField();
txtkq = new TextField();
cong = new Button("Cong");
tru = new Button("Tru");
nhan = new Button("Nhan");
chia = new Button("Chia");
exit = new Button("Exit");
reset = new Button("Reset");
cong.addActionListener(this);
tru.addActionListener(this);
nhan.addActionListener(this);
chia.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);
pn = new Panel(new GridLayout(4,1));
pn1 = new Panel(new GridLayout());
pn2 = new Panel(new GridLayout(3,2));
pn3 = new Panel(new FlowLayout());
20. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn4 = new Panel(new FlowLayout());
pn1.add(lb);
pn2.add(lb1);
pn2.add(txta);
pn2.add(lb2);
pn2.add(txtb);
pn2.add(lb3);
pn2.add(txtkq);
pn3.add(cong);
pn3.add(tru);
pn3.add(nhan);
pn3.add(chia);
pn4.add(exit);
pn4.add(reset);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
this.add(pn);
setSize(400,300);
this.setVisible(true);
}
public static void cong()
{
float a = Float.parseFloat(txta.getText());
float b = Float.parseFloat(txtb.getText());
21. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
txtkq.setText(Float.toString(a + b));
}
public static void tru()
{
float a = Float.parseFloat(txta.getText());
float b = Float.parseFloat(txtb.getText());
txtkq.setText(Float.toString(a - b));
}
public static void nhan()
{
float a = Float.parseFloat(txta.getText());
float b = Float.parseFloat(txtb.getText());
txtkq.setText(Float.toString(a * b));
}
public static void chia()
{
float a = Float.parseFloat(txta.getText());
float b = Float.parseFloat(txtb.getText());
txtkq.setText(Float.toString((float)a/b));
}
22. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == cong)
{
cong();
}
else if(e.getSource() == tru)
{
tru();
}
else if(e.getSource() == nhan)
{
nhan();
}
else if(e.getSource() == chia)
{
chia();
}
else if(e.getSource() == reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
else
if(e.getSource() == exit)
23. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
{
System.exit(0);
}
}
public giaodien2(String s)
{
super(s);
GUI();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String []agrs)
{
new giaodien2("Minh hoa cac phep toan");
}
}
Demo:
25. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 3:
Code:
package thuchanh;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class giaodien3 extends Frame implements ActionListener{
Label lb,lb1,lb2;
static TextField nhap;
static int n;
static TextField kq;
// int n;
Button tim, reset,exit;
Panelpn, pn1, pn2,pn3,pn4;
26. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
public void GUI()
{
lb = new Label("Cac so nguyen to <= n");
lb1 = new Label("Nhap n");
lb2 = new Label(" KQ ");
nhap = new TextField(5);
kq = new TextField(5);
tim = new Button("Tim");
reset = new Button("Reset");
exit = new Button("Exit");
nhap.addActionListener(this);
kq.addActionListener(this);
tim.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
pn = new Panel(new GridLayout(4,1));
pn1 = new Panel(new GridLayout());
pn2 = new Panel(new FlowLayout());
pn3 = new Panel(new FlowLayout());
pn4 = new Panel(new FlowLayout());
pn1.add(lb);
pn2.add(lb1);
pn2.add(nhap);
pn3.add(lb2);
pn3.add(kq);
pn4.add(tim);
27. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn4.add(reset);
pn4.add(exit);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
this.add(pn);
setSize(400,350);
this.setVisible(true);
}
public static int ktra(int n)
{
if(n<=1) return 0;
if(n==2) return 1;
for(int i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
public static void nguyen_to()
{
28. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
n = Integer.parseInt(nhap.getText());
int i;
String k = "";
for(i=1;i<=n;i++)
{
if(ktra(i) == 1)
k += i + " ";
}
kq.setText(k);
}
public giaodien3(String s)
{
super(s);
GUI();
}
public static void main(String []agrs)
{
new giaodien3("Tim so nguyen to");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == tim)
{
29. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
nguyen_to();
}
else if(e.getSource() == reset)
{
nhap.setText("");
kq.setText("");
}
else if(e.getSource() == exit)
{
System.exit(0);
}
}
}
Demo:
30. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 4:
Code:
package thuchanh;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
31. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
import java.util.Scanner;
@SuppressWarnings({ "unused", "serial" })
public class giaodien4 extends Frame implements ActionListener{
Label lb;
Label lb1;
Label lb2;
TextField nhap;
TextField kq;
Button ok,reset,exit;
Panelpn,pn1,pn2,pn3,pn4;
static int n;
public void GUI()
{
lb = new Label("Kiem tra a co thuoc day fibonaci");
lb1 = new Label("Nhap a");
lb2 = new Label(" KQ ");
nhap = new TextField(20);
kq = new TextField(20);
ok = new Button("OK");
reset = new Button("Reset");
exit = new Button("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
32. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
exit.addActionListener(this);
pn = new Panel(new GridLayout(4,1));
pn1 = new Panel(new FlowLayout());
pn2 = new Panel(new FlowLayout());
pn3 = new Panel(new FlowLayout());
pn4 = new Panel(new FlowLayout());
pn1.add(lb);
pn2.add(lb1);
pn2.add(nhap);
pn3.add(lb2);
pn3.add(kq);
pn4.add(ok);
pn4.add(reset);
pn4.add(exit);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
this.add(pn);
setSize(400,350);
setVisible(true);
}
public void Ktra_Fibo()
{
n = Integer.parseInt(nhap.getText());
int fi1=0;
33. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
int fi2=1;
int fi;
while(true)
{
fi = fi1 + fi2;
if(fi == n)
{
kq.setText("thuoc day fibonaci"); break;
}
if(n < fi)
{
kq.setText("khong thuoc day fibonaci"); break;
}
fi1 = fi2;
fi2 = fi;
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == ok)
{
Ktra_Fibo();
}
else if(e.getSource() == reset)
34. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
{
nhap.setText("");
kq.setText("");
}
else if(e.getSource() == exit)
{
System.exit(0);
}
}
public giaodien4(String s)
{
super(s);
GUI();
}
public static void main(String []agrs)
{
new giaodien4("Kiem tra so thuoc day fibonaci");
}
}
Demo:
35. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 5:
Code:
package thuchanh;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class giaodien5 extends Frame implements ActionListener {
36. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Button green, blue,red,exit;
Panelpn;
public void GUI()
{
green = new Button("Green");
blue = new Button("Blue");
red = new Button("Red");
exit = new Button("Exit");
green.addActionListener(this);
blue.addActionListener(this);
red.addActionListener(this);
exit.addActionListener(this);
pn = new Panel(new FlowLayout());
pn.add(green);
pn.add(blue);
pn.add(red);
pn.add(exit);
this.add(pn);
pn.setBackground(Color.green);
setSize(350,400);
setVisible(true);
}
public giaodien5(String s)
{
37. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
super(s);
GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == green)
{
pn.setBackground(Color.green);
}
if(e.getSource() == blue)
{
pn.setBackground(Color.blue);
}
if(e.getSource() == red)
{
pn.setBackground(Color.red);
}
if(e.getSource() == exit)
{
System.exit(0);
}
}
public static void main(String s[])
38. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
{
new giaodien5("Doi nen");
}
}
Demo:
40. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
public class giaodien6 extends Frame implements ItemListener{
Checkbox male,female;
List list;
Choice ch;
public static Label lb;
Button bt;
Panelpn,pn1,pn2,pn3,pn4;
public void GUI()
{
CheckboxGroup cb = new CheckboxGroup();
MyActionListener listener = new MyActionListener();
list = new List(3, false);
list.add("MS DOS");
list.add("WINDOWS");
list.add("LINUX");
list.add("UNIX");
list.addItemListener(this);
ch = new Choice();
ch.addItem("Tiger");
ch.addItem("Cat");
41. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
ch.addItem("Dog");
ch.addItemListener(this);
lb = new Label("The Item is display here");
bt = new Button("Exit");
bt.addActionListener(listener);
pn = new Panel(new GridLayout(4,1));
pn1 = new Panel(new FlowLayout());
Checkbox male = new Checkbox("Male", cb, true);
Checkbox female = new Checkbox("Female", cb, true);
male.addItemListener(this);
female.addItemListener(this);
pn1.add(male);
pn1.add(female);
pn2 = new Panel(new FlowLayout());
pn2.add(list);
42. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn3 = new Panel(new FlowLayout());
pn3.add(ch);
pn4 = new Panel(new FlowLayout());
pn4.add(lb);
pn4.add(bt);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
this.add(pn);
setSize(400,400);
setVisible(true);
}
public giaodien6(String s)
{
super(s);
GUI();
}
public void windowClosing(WindowEvent e )
{
System.exit(0);
}
public static void main(String []agrs)
{
43. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
new giaodien6("Item Event");
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getStateChange() == ItemEvent.SELECTED)
{
Object temp = e.getItem();
String s = (String) temp;
lb.setText("You choice "+s);
}
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
Demo:
44. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 7:
Code:
package thuchanh;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class maytinh extends JFrame implements ActionListener{
JMenuBar jmb;
JMenu edit,view,help;
JTextArea txt;
JButton butback, butce, butc,butcongtru, butmc, butmr, butms,butsqrt,butphantram,but1x,butkq,
butcham, butmcong;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,cong,tru,nhan,chia,bang;
45. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
JPanelpn1,pn2,pn3;
public static double num1= 0, num2=0,result=0, bachai = 0;
private Container cont;
int kt = 0;
int add = 0;
int sub = 0;
int muti = 0;
int div = 0;
public maytinh (String s)
{ super (s);
butback= new JButton("Backspace");
butce=new JButton("CE");
butc=new JButton("C");
butcongtru=new JButton("+/-");
butmc=new JButton("MC");
butmr=new JButton("MR");
butms=new JButton("MS");
butsqrt=new JButton("SQRT");
butphantram=new JButton("%");
but1x=new JButton("1/x");
butkq=new JButton("");
butcham=new JButton(".");
butmcong=new JButton("M+");
b0=new JButton("0");
b1=new JButton("1");
46. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
cong=new JButton("+");
tru=new JButton("-");
nhan=new JButton("*");
chia=new JButton("/");
bang=new JButton("=");
cont=this.getContentPane();
jmb=new JMenuBar();
edit= new JMenu("Edit");
view= new JMenu("View");
help=new JMenu("Help");
jmb.add(edit);
jmb.add(view);
jmb.add(help);
setJMenuBar(jmb);
txt=new JTextArea();
pn1=new JPanel();
pn1.setLayout(new GridLayout(1,1));
pn1.add(txt);
47. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn2=new JPanel();
pn2.setLayout(new FlowLayout());
pn2.add(butkq);
pn2.add(butback);
pn2.add(butce);
pn2.add(butc);
pn3=new JPanel();
pn3.setLayout(new GridLayout(4,6));
pn3.add(butmc);
pn3.add(b7);
pn3.add(b8);
pn3.add(b9);
pn3.add(chia);
pn3.add(butsqrt);
pn3.add(butmc);
pn3.add(b4);
pn3.add(b5);
pn3.add(b6);
pn3.add(nhan);
pn3.add(butphantram);
pn3.add(butms);
pn3.add(b1);
pn3.add(b2);
pn3.add(b3);
pn3.add(tru);
pn3.add(but1x);
48. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn3.add(butmcong);
pn3.add(b0);
pn3.add(butcongtru);
pn3.add(butcham);
pn3.add(cong);
pn3.add(bang);
cont.add(pn1,"North");
cont.add(pn2);
cont.add(pn3,"South");
bang.addActionListener(this);
butback.addActionListener(this);
cong.addActionListener(this);
tru.addActionListener(this);
nhan.addActionListener(this);
chia.addActionListener(this);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
butcongtru.addActionListener(this);
49. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
butcham.addActionListener(this);
but1x.addActionListener(this);
butphantram.addActionListener(this);
butsqrt.addActionListener(this);
butc.addActionListener(this);
this.pack();
this.setVisible(true);
}
public double read()
{
double x;
x= Double.valueOf(txt.getText());
return x;
}
public double canx()
{
double x;
x=Double.valueOf(txt.getText());
return (double)Math.sqrt(x);
}
public double phantram()
{
double phantram;
phantram = Double.valueOf(txt.getText());
return (double) phantram/100;
}
50. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
public double chiax()
{
double chiax;
chiax = Double.valueOf(txt.getText());
return (double)1/chiax;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
try{
if(source==b1)
txt.append("1");
if(source==b2)
txt.append("2");
if(source==b3)
txt.append("3");
if(source==b4)
txt.append("4");
if(source==b5)
txt.append("5");
if(source==b6)
txt.append("6");
if(source==b7)
txt.append("7");
if(source==b8)
txt.append("8");
51. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
if(source==b9)
txt.append("9");
if(source==b0)
txt.append("0");
if(source==butc)
txt.setText("");
if(source == butsqrt)
{
bachai = canx();
txt.setText(Double.toString(bachai));
}
if(source == but1x)
{
txt.setText(Double.toString(chiax()));
}
if(source == butphantram)
{
txt.setText(Double.toString(phantram()));
}
if(source==cong)
{
num1 = read();
txt.setText("");
add = 1;
52. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
sub = 0;
div = 0;
muti = 0;
}
if(source == tru)
{
num1 = read();
txt.setText("");
add = 0;
sub = 1;
div = 0;
muti = 0;
}
if(source == nhan)
{
num1 = read();
txt.setText("");
add = 0;
sub = 0;
div = 0;
muti = 1;
}
if(source == chia)
{
num1 = read();
txt.setText("");
53. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
add = 0;
sub = 0;
div = 1;
muti = 0;
}
if(source == butback)
{
setTexttxt(getStringOnDisplay().substring(0, getStringOnDisplay().length()-1));
}
if(source == butcongtru)
{
changeSign();
}
if(source == butce)
{
txt.setText("0.0");
}
if(source == bang)
{
num2 = Double.valueOf(txt.getText());
{
if(add == 1)
{
result = num1 + num2;
txt.setText(Double.toString(result));
54. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
}
if(sub == 1)
{
result = num1 - num2;
txt.setText(Double.toString(result));
}
if(div == 1)
{
result = (double)num1/num2;
txt.setText(Double.toString(result));
}
if(muti ==1)
{
result = (double)num1*num2;
txt.setText(Double.toString(result));
}
}
}
}catch(Exception e1){};
}
private void setTexttxt(String s) {
// TODO Auto-generated method stub
55. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
txt.setText(s);
}
private String getStringOnDisplay() {
// TODO Auto-generated method stub
return txt.getText();
}
public void changeSign()
{
if(Double.parseDouble(txt.getText()) < 0)
{
setTexttxt(getStringOnDisplay().substring(1));
}
if(Double.parseDouble(txt.getText()) > 0)
{
setTexttxt(Double.toString(getNumberInDisplay() * -1));
}
}
public double getNumberInDisplay()
{
String stuff = txt.getText();
return Double.parseDouble(stuff);
}
public static void main(String args[])
56. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
{
maytinh operator = new maytinh ("May tinh bo tui ");
}
}
Demo:
58. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
bg.add(dc);
bg.add(gt);
pn1=new JPanel();
pn2=new JPanel();
pn3=new JPanel();
pn3.setLayout(new FlowLayout());
pn1.setLayout(new GridLayout(1,5));
pn2.setLayout(new FlowLayout());
pn1.add(nhap);
pn1.add(input);
pn1.add(timkiem);
pn1.add(reset);
pn1.add(exit);
pn2.add(tim);
pn2.add(ms);
pn2.add(ht);
pn2.add(ns);
pn2.add(dc);
pn2.add(gt);
timkiem.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
cont=this.getContentPane();
output(ip1,ip2);
/*Tạo bảng để chứa thông tin truy vấn từ cơ sở dữ
liệu*/
tableResult = new JScrollPane(new
JTable(vData,vTitle));
/*Tạo cửa sổ chứa bảng dữ liệu*/
pn3.add(tableResult);
cont.add(pn1,"North");
cont.add(pn2);
cont.add(pn3,"South");
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source==exit) System.exit(1);
if (source==reset) {
click=0;
ip1="ID";
59. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
ip2="0";
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (source==timkiem)
{ click=1;
if (ms.isSelected())
{
ip2=input.getText();
ip1="ID";
try {
output(ip1,ip2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (ht.isSelected())
{
ip1="Ho Ten";
ip2="'%"+input.getText()+"%'";
try {
output(ip1,ip2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (click==1)
{
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (ns.isSelected())
{
ip1="Ngay sinh";
60. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
ip2="'"+input.getText()+"'";
try {
output(ip1,ip2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (dc.isSelected())
{
ip1="Dia chi";
ip2=input.getText();
try {
output(ip1,ip2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (gt.isSelected())
{
ip1="Gioi tinh";
ip2=input.getText();
try {
output(ip1,ip2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
vera();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
61. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
}
public void output(String s1,String s2) throws SQLException
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Ket noi co so du lieu va taoý doi tuong Statement
String url="jdbc:odbc:milo";
Connection conn=DriverManager.getConnection(url);
Statement sm=conn.createStatement();
//Chèn thêm thông tin vào bảng
ResultSet rs=sm.executeQuery("Select * from Table1 where
"+s1+"="+s2);
ResultSetMetaData rsm=rs.getMetaData();
int col_num =rsm.getColumnCount();
//Chuẩn bị dữ liệu để tạo bảng (JTable) hiển thị thông tin vTitle
chứa đựng thông tin tên cột của bảng
vTitle =new Vector(col_num);
for (int i = 1; i <= col_num; i++) {
vTitle.add(rsm.getColumnLabel(i));
}
//vData chứa nội dung của bảng
//mi dung vecto cai nay met lam,dung tabel cho nhanh, dùng
sao , sửa trực tiêp bài này đi//sua lau lam ta dung cai hom truoc viet cho mi
nhe
//mi tao 1 cai table trong my sql di bang khach hang
do//cai my sql ta chua lam bao gio
vData=new Vector(10,10);
while (rs.next()) {
Vector row =new Vector(col_num);
for (int i = 1; i <= col_num; i++)
row.add(rs.getString(i));
vData.add(row);
}
rs.close();
sm.close();
conn.close();
} catch (ClassNotFoundException e) {
62. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
e.printStackTrace();
}
}
}
//class Main
package Ketnoicsdl_1;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) throws SQLException{
giao_dien gd = new giao_dien("demo");
}
}
Demo:
63. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Bài 2:
import java.awt.Checkbox;
public class truyvan extends JFrame implements
ActionListener,ItemListener{
JButton bt1, bt2, bt3,bt4,bt5,bt6,bt7,bt8;
JTextField tf1,tf2;
Panel pn, p1,p2,p3 ;
Checkbox cb1,cb2,cb3,cb4,cb5;
CheckboxGroup cg;
JLabel l1,l2;
64. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
Connection conn;
Statement sm ;
JTable tb1;
JScrollPane tbR ;
Vector vData = null;
Vector vTitle = null;
DefaultTableModel model;
ResultSet rs;
String truyvan;
String sql= "Select * from ThongTin";
public truyvan(){
setTitle("Tim Kiem");
pn = new Panel(new GridLayout(3,1));
p1 = new Panel(new FlowLayout());
p2 = new Panel(new FlowLayout());
p3 = new Panel(new GridLayout(1,1));
// P1
l1 = new JLabel("Nhap noi dung");
tf1= new JTextField(30);
bt1 = new JButton("Tim Kiem");
bt2 = new JButton("Reset");
bt3 = new JButton("Exit");
p1.add(l1);
p1.add(tf1);
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
65. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
pn.add(p1);
//P2
cg= new CheckboxGroup();
cb1 = new Checkbox("MaSo",cg,false);
cb1.addItemListener(this);
cb2 = new Checkbox("HoTen",cg,false);
cb2.addItemListener(this);
cb3 = new Checkbox("NgaySinh",cg,false);
cb3.addItemListener(this);
cb4 = new Checkbox("DiaChi",cg,false);
cb4.addItemListener(this);
cb5 = new Checkbox("GioiTinh",cg,false);
cb5.addItemListener(this);
l2 = new JLabel("Tim Kiem Theo");
p2.add(l2);
p2.add(cb1);
p2.add(cb2);
p2.add(cb3);
p2.add(cb4);
p2.add(cb5);
pn.add(p2);
tf2 = new JTextField("");
//-------------------
try{
load(sql);
model = new DefaultTableModel(vData,vTitle);
tb1 = new JTable(model);
tbR = new JScrollPane(tb1);
p3.add(tbR);
pn.add(p3);
66. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
}catch(Exception e){
System.out.println(e);
}
add(pn);
setSize(700,300);
setResizable(false);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
setVisible(false);
System.exit(0);
}
});
}
public void load(String sql){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:ThongTin";
conn=DriverManager.getConnection(url,"Login","Password");
sm = conn.createStatement();
rs=sm.executeQuery(sql);
ResultSetMetaData rsm=rs.getMetaData();
int socot = rsm.getColumnCount();
vTitle = new Vector(socot);
for(int i=1;i<=socot;i++){
vTitle.add(rsm.getColumnLabel(i));
}
vData = new Vector(10,10);
67. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
while(rs.next())
{
Vector row = new Vector(socot);
for(int i =1 ; i<=socot;i++){
row.add(rs.getString(i));
}
vData.add(row);
}
rs.close();
sm.close();
conn.close();
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) throws
ClassNotFoundException,SQLException{
new truyvan();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt1){
if(truyvan == "MaSo"){
truyvan ="select * from ThongTin where
"+truyvan+"="+tf1.getText();
}
else{
truyvan ="select * from ThongTin where
"+truyvan+"='"+tf1.getText()+"'";
}
load(truyvan);
68. Thực hành lập trình java
SVTH: Nguyễn Rim 11T1
tb1.setModel(new DefaultTableModel(vData,vTitle));
}
else if(e.getSource()==bt2){
cg.setSelectedCheckbox(null);
tf1.setText("");
load(sql);
tb1.setModel(new DefaultTableModel(vData,vTitle));
}
else System.exit(0);
}
//--------------------------------------------------------------------
-----
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==cb1)truyvan= "MaSo";
else if(e.getSource()==cb2)truyvan= "HoTen";
else if(e.getSource()==cb3)truyvan= "NgaySinh";
else if(e.getSource()==cb4)truyvan= "DiaChi";
else truyvan= "GioiTinh";
}
}
Demo: