Tải bản đầy đủ (.pdf) (93 trang)

báo cáo thực hành lập trình java

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


ĐẠI HỌC ĐÀ NẴNG
TRƯỜNG ĐẠI HỌC BÁCH KHOA
KHOA CÔNG NGHỆ THÔNG TIN
Tel. (84-511) 736 949, Fax. (84-511) 842 771
Website: itf.ud.edu.vn, E-mail:



BÁO CÁO THỰC HÀNH
LẬP TRÌNH JAVA



GIÁO VIÊN: ThS.NGUYỄN VĂN NGUYÊN
SINH VIÊN : Nguyễn Thanh Vũ
LỚP : 10T2
NHÓM : 12A
MSV : 102152101168






ĐÀ NẴNG, 12/2013

Báo cáo thực hành Java

2


MỤC LỤC
BÀI THỰC HÀNH BUỔI 1 5
1. Hệ phương trình hai ẩn 5
1.1 Thuật toán 5
1.2 Chương trình 5
1.3 Demo 10
2. Hệ phương trình ba ẩn 11
2.1 Thuật toán 11
2.2 Chương trình 11
2.3 Demo 20
3. Phương trình bậc hai 21
3.1 Thuật toán 21
3.2 Chương trình 21
3.3 Demo 26
4. Phương trình trùng phương 27
4.1 Thuật toán 27
4.2 Chương trình 27
4.4 Demo 33
BÀI THỰC HÀNH BUỔI 2 34
1. 34
1.1 Chương trình 34
1.2 Demo 35
2. 35
2.1 Chương trình 35
2.2 Demo 40
3. 41
3.1 Chương trình 41
3.2 Demo 42
4. 42
4.1 Chương trình 42

4.2 Demo 44
5. . 44
5.1 Chương trình 44
5.2 Demo 45
6. . 46
6.1 Chương trình 46
6.2 Demo 47
7. 47
7.1 Chương trình 47
7.2 Demo 48
8. 48
8.1 Chương trình 48
8.2 Demo 49
9. 49
9.1 Chương trình 49
9.2 Demo 50
10. 51
10.1 Chương trình 51
10.2 Demo 52
11. 52
11.1 Chương trình 52
11.2 Demo 53
12. 53
12.1 Chương trình 53
12.1 Demo 54
13. 55
13.1 Chương trình 55
13.2 Demo 56
14. 56
14.1 Chương trình 56

14.2 Demo 58
15. 58
15.1 Chương trình 58
15.2 Demo 63
16. 63
16.1 Chương trình 63
16.2 Demo 65
17. 66
17.1 Chương trình 66
17.2 Demo 67
18. 67
18.1 Chương trình 67
18.2 Demo 70
19. 70
19.1 Chương trình 71
20. 79
20.1 Chương trình 79
20.2 Demo 81
21. 81
21.1 Chương trình 81
21.2 Demo 82
22. 83
22.1 Chương trình 83
22.2 Demo 84
23. 84
23.1 Chương trình 84
23.2 Demo 85
Báo cáo thực hành Java

4

24. 85
24.1 Chương trình 85
24.2 Demo 86
25. 86
25.1 Chương trình 87
25.2 Demo 89
BÀI THỰC HÀNH BUỔI 3 90
1. Nhập xuất hai ma trận và tính toán cộng, trừ, nhân, chia hai ma trận đã nhập. 90
1.1 Chương trình 90
1.2 Demo 93


Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 5

BÀI THỰC HÀNH BUỔI 1
1. Hệ phương trình hai ẩn
1.1 Thuật toán
B1: Khai báo giá trị các biến:a,b,c,d,e,f, dinhthuc,dx,dy:real
B2: Đọc và gán các giá trị từ bàn phím
B3: Gán : dinhthuc = a*e*d*b;
dx = c*e – f*b;
dy = a*f – d*c;
+ Nếu dinhthuc <> 0 thi hệ phương trình có nghiệm
Nghiệm x = dx/dinhthuc
Nghiệm y = dy/dinhthuc
+ Ngược lại Nếu dx <> 0 hoặc dy <> 0 thì hệ phương trình vô nghiệm
Ngược lại hệ phương trình vô định.
1.2 Chương trình
Giao diện AWT



import java.io.*;
import java.awt.*;
import java.awt.event.*;

class hpt2an_awt extends Frame implements ActionListener {

//Khai bao cac bien khoi tao giao dien AWT
Label lb1,lb2,lb3,lb4,lb5,lb6,lb7,lb8;
TextField tf1,tf2,tf3,tf4,tf5,tf6,tf7;
Button tinh,reset,exit;
Panel pn1,pn2,pn3,pn;

public hpt2an_awt() {
//Khoi tao Label
lb1 = new Label("Giai he phuong trinh 2 an");
lb2 = new Label("Nhap gia tri a1 : ");
lb3 = new Label("Nhap gia tri b1 : ");
lb4 = new Label("Nhap gia tri c1 : ");
lb5 = new Label("Nhap gia tri a2 : ");
lb6 = new Label("Nhap gia tri b2 : ");
lb7 = new Label("Nhap gia tri c2 : ");
lb8 = new Label("Ket qua nghiem : ");
//Khoi tao TextField
tf1 = new TextField(40);
tf2 = new TextField(40);
tf3 = new TextField(40);
tf4 = new TextField(40);
tf5 = new TextField(40);



6


























tf6 = new TextField(40);
tf7 = new TextField(40);

tf7.setEditable(false);

//Khoi tao Button
tinh = new Button("Tinh");
tinh.addActionListener(this);
reset = new Button("Reset");
reset.addActionListener(this);
exit = new Button("Exit");
exit.addActionListener(this);

//Khoi tao Panel
pn1 = new Panel(new FlowLayout());
pn1.add(lb1);
pn2 = new Panel(new FlowLayout());
pn2.add(lb2);
pn2.add(tf1);
pn2.add(lb3);
pn2.add(tf2);
pn2.add(lb4);
pn2.add(tf3);
pn2.add(lb5);
pn2.add(tf4);
pn2.add(lb6);
pn2.add(tf5);
pn2.add(lb7);
pn2.add(tf6);
pn2.add(lb8);
pn2.add(tf7);
pn3 = new Panel(new FlowLayout());
pn3.add(tinh);

pn3.add(reset);
pn3.add(exit);
pn = new Panel(new GridLayout(3,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
}
//Khoi tao ham main
public static void main(String[] args) {
hpt2an_awt nt = new hpt2an_awt();
nt.setSize(500,400);
nt.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==exit)
{
System.exit(0);
}
if (e.getSource()==reset)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");

}
Báo cáo Thực hành Java 7

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 7

































Giao diện Swing




















if (e.getSource()==tinh)
{
int a1,b1,c1,a2,b2,c2;

int d,d1,d2;
float x,y;
a1 = Integer.parseInt(tf1.getText());
b1 = Integer.parseInt(tf2.getText());
c1 = Integer.parseInt(tf3.getText());
a2 = Integer.parseInt(tf4.getText());
b2 = Integer.par seInt(tf5.getText());
c2 = Integer.parseInt(tf6.getText());
d = a1*b2 - a2*b1;
d1 = c1*b2 - c2*b1;
d2 = a2*c1 - c2*a1;

if (d!=0)
{
if ((d2==0) && (d1==d2))
{
tf7.setText("He phuong trinh vo nghiem");
}
else
{
x = (float) d1/d;
y = (float) d2/d;
tf7.setText("Co 2 nghiem phan biet : "
+Float.toString(x) +" " +Float.toString(y));
}
}

if (d==0) tf7.setText("Phuong trinh vo nghiem!!!");
}
}

}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class hpt2an_swing extends JFrame implements ActionListener {

//Khai bao cac bien khoi tao giao dien AWT
JLabel lb1,lb2,lb3,lb4,lb5,lb6,lb7,lb8;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7;
JButton tinh,reset,exit;
JPanel pn1,pn2,pn3,pn;

public hpt2an_swing() {
//Khoi tao Label
lb1 = new JLabel("Giai he phuong trinh 2 an");
lb2 = new JLabel("Nhap gia tri a1 : ");
lb3 = new JLabel("Nhap gia tri b1 : ");
lb4 = new JLabel("Nhap gia tri c1 : ");
lb5 = new JLabel("Nhap gia tri a2 : ");
lb6 = new JLabel("Nhap gia tri b2 : ");
lb7 = new JLabel("Nhap gia tri c2 : ");

8


























//Khoi tao TextField
tf1 = new JTextField(30);
tf2 = new JTextField(30);
tf3 = new JTextField(30);
tf4 = new JTextField(30);
tf5 = new JTextField(30);
tf6 = new JTextField(30);
tf7 = new JTextField(30);
tf7.setEditable(false);


//Khoi tao Button
tinh = new JButton("Tinh");
tinh.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
exit = new JButton("Exit");
exit.addActionListener(this);

//Khoi tao Panel
pn1 = new JPanel(new FlowLayout());
pn1.add(lb1);
pn2 = new JPanel(new FlowLayout());
pn2.add(lb2);
pn2.add(tf1);
pn2.add(lb3);
pn2.add(tf2);
pn2.add(lb4);
pn2.add(tf3);
pn2.add(lb5);
pn2.add(tf4);
pn2.add(lb6);
pn2.add(tf5);
pn2.add(lb7);
pn2.add(tf6);
pn2.add(lb8);
pn2.add(tf7);
pn3 = new JPanel(new FlowLayout());
pn3.add(tinh);
pn3.add(reset);
pn3.add(exit);

pn = new JPanel(new GridLayout(3,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
}
//Khoi tao ham main
public static void main(String[] args) {
hpt2an_swing nt = new hpt2an_swing();
nt.setResizable(true);
nt.setSize(500,500);
nt.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==exit)
{
System.exit(0);
}
Báo cáo Thực hành Java 9

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 9











































if (e.getSource()==reset)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
}
if (e.getSource()==tinh)
{
int a1,b1,c1,a2,b2,c2;
int d,d1,d2;
float x,y;
a1 = Integer.parseInt(tf1.getText());
b1 = Integer.parseInt(tf2.getText());
c1 = Integer.parseInt(tf3.getText());
a2 = Integer.parseInt(tf4.getText());
b2 = Integer.parseInt(tf5.getText());
c2 = Integer.parseInt(tf6.getText());
d = a1*b2 - a2*b1;
d1 = c1*b2 - c2*b1;
d2 = a2*c1 - c2*a1;

if (d!=0)

{
if ((d2==0) && (d1==d2))
{
tf7.setText("He phuong trinh vo nghiem");
}
else
{
x = (float) d1/d;
y = (float) d2/d;
tf7.setText("Co 2 nghiem phan biet : "
+Float.toString(x) +" " +Float.toString(y));
}
}

if (d==0) tf7.setText("Phuong trinh vo nghiem!!!");
}
}
}

10
1.3 Demo


Báo cáo Thực hành Java 11

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 11

2. Hệ phương trình ba ẩn
2.1 Thuật toán
B1: Tính định thức delta của ma trận hệ số A của hệ phương trình

B2: Nếu delta != 0 thì phương trình có nghiệm duy nhất. Nếu không chuyển sang B3.
B2.1: Lập các ma trận vuông mới X, Y, Z bằng từ ma trận hệ số A bằng cách thay cột hệ
số của biến x, y, z tương ứng bởi cột hệ số tự do.
B2.2: Tính định thức của delta_X, delta_Y, delta_Z tương ứng từ ma trận X, Y, Z.
B2.3: Tìm x, y, z bằng cách tìm kết quả của phép chia của định thức delta_X, delta_Y,
delta_Z tương ứng với delta.
B2.4: Kết thúc chương trình.
B3: Tìm các định thức delta_X, delta_Y, delta_Z.
B4: Nếu tất cả định thức delta_X, delta_Y, delta_Z == 0 thì hệ phương trình vô số nghiệm.
Nếu không, hệ phương trình vô nghiệm.
2.2 Chương trình
Giao diện AWT





























import java.awt.*;
import java.awt.event.*;

public class hpt3an_awt extends Frame implements ActionListener{

private static final long serialVersion UID = 1L;
Label title, la, lb, lc, ld, kq, lkq, lx1, lx2, lx3;
TextField ta1, tb1, tc1, td1, ta2, tb2, tc2, td2, ta3, tb3, tc3, td3;
Button ok, reset, exit;
Panel p,pnhap, ptit, pbut, pnghiem1;

float[][] a = new float[4][5];

public hpt3an_awt(String text){
super(text);

title = new Label("Giai he phuong trinh 3 an");
la = new Label("a ");
lb = new Label("b ");
lc = new Label("c ");

ld = new Label("d ");
kq = new Label("So nghiem: ");
lx1 = new Label("x1= ");
lx2 = new Label("x2= ");
lx3 = new Label("x3= ");
lkq = new Label("");

ta1 = new TextField(7);
tb1 = new TextField(7);
tc1 = new TextField(7);
td1 = new TextField(7);

12





















































ta2 = new TextField(7);
tb2 = new TextField(7);
tc2 = new TextField(7);
td2 = new TextField(7);
ta3 = new TextField(7);
tb3 = new TextField(7);
tc3 = new TextField(7);
td3 = new TextField(7);

ok = new Button("Tinh");
reset = new Button("Reset");
exit = new Button("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);

ptit = new Panel(new FlowLayout());
pbut = new Panel(new FlowLayout());
pnghiem1 = new Panel(new GridLayout(1,2));
p = new Panel(new BorderLayout());
pnhap=new Panel(new GridBagLayout());

pbut.add(ok); pbut.add(reset); pbut.add(exit);
ptit.add(title);

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(3, 3, 3, 3);

gbc.gridx=0;
gbc.gridy=1;
pnhap.add(new Label("Pt1: "), gbc);
gbc.gridy=2;
pnhap.add(new Label("Pt2: "), gbc);
gbc.gridy=3;
pnhap.add(new Label("Pt3: "), gbc);
gbc.gridy=4;
pnhap.add(kq, gbc);

gbc.gridx=1;
gbc.gridy=0;
pnhap.add(la,gbc);
gbc.gridy=1;
pnhap.add(ta1,gbc);
gbc.gridy=2;
pnhap.add(ta2,gbc);
gbc.gridy=3;
pnhap.add(ta3,gbc);
gbc.gridy=4;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
pnhap.add(lkq, gbc);

gbc.anchor = GridBagConstraints.BASELINE;
gbc.gridx=2;
gbc.gridy=0;
pnhap.add(lb,gbc);
gbc.gridy=1;

pnhap.add(tb1,gbc);
gbc.gridy=2;
pnhap.add(tb2,gbc);
gbc.gridy=3;
pnhap.add(tb3,gbc);
Báo cáo Thực hành Java 13

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 13




gbc.gridx=3;
gbc.gridy=0;
pnhap.add(lc,gbc);
gbc.gridy=1;
pnhap.add(tc1,gbc);
gbc.gridy=2;
pnhap.add(tc2,gbc);
gbc.gridy=3;
pnhap.add(tc3,gbc);

gbc.gridx=4;
gbc.gridy=0;
pnhap.add(ld,gbc);
gbc.gridy=1;
pnhap.add(td1,gbc);
gbc.gridy=2;
pnhap.add(td2,gbc);
gbc.gridy=3;

pnhap.add(td3,gbc);

gbc.gridx=1;
gbc.gridy=5;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
pnhap.add(lx1, gbc);
gbc.gridx=2;
pnhap.add(lx2, gbc);
gbc.gridx=3;
pnhap.add(lx3, gbc);

p.add(pnhap, BorderLayout.WEST);
p.add(ptit, BorderLayout.NORTH);
p.add(pbut, BorderLayout.SOUTH);
add(p);
setSize(440, 250);
setVisible(true);
setResizable(false);
enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);
}
//Dong window khi bam nut x
protected void processWindowEvent(WindowEvent e){
if (e.getID()== WindowEvent.WINDOW_CLOSING){
dispose();
System.exit(0);
}
super.processWindowEvent(e);
}

public static void main(String[] args){

hpt3an_awt b = new hpt3an_awt("Ghptb2");
}

@Override
public void actionPerformed(ActionEvent arg0) {
Button t = (Button) arg0.getSource();
if(t==ok){
giai();
}

14


if(t==reset){
ta1.setText("");
tb1.setText("");
tc1.setText("");
td1.setText("");
ta2.setText("");
tb2.setText("");
tc2.setText("");
td2.setText("");
ta3.setText("");
tb3.setText("");
tc3.setText("");
td3.setText("");
lkq.setText("");
resetNghiem();
}
if(t==exit){

System.exit(0);
}
}
public void resetNghiem(){
lx1.setText("x1= ");
lx2.setText("x2= ");
lx3.setText("x3= ");
}
public void giai(){
hpt3an_awt.this.invalidate();
float m,s;
float[] x = new float[4];
int i, j, k, n;
n=3;//Set cai nay cho khoe. Vo bac 3 chi can set lai
try{
a[1][1] = Float.pa rseFloat(ta1.getText());
a[1][2] = Float.pa rseFloat(tb1.getText());
a[1][3] = Float.pa rseFloat(tc1.getText());
a[1][4] = Float.pa rseFloat(td1.getText());
a[2][1] = Float.pa rseFloat(ta2.getText());
a[2][2] = Float.pa rseFloat(tb2.getText());
a[2][3] = Float.pa rseFloat(tc2.getText());
a[2][4] = Float.pa rseFloat(td2.getText());
a[3][1] = Float.pa rseFloat(ta3.getText());
a[3][2] = Float.pa rseFloat(tb3.getText());
a[3][3] = Float.pa rseFloat(tc3.getText());
a[3][4] = Float.pa rseFloat(td3.getText());
}catch(Exception e){
lkq.setText("Loi Format");
resetNghiem();

hpt3an_awt.this.validate();
return;
}

//Bien doi a ve ma tran tam giac tren
for(i=1; i<n; i++){
if(a[i][i]==0){
for(k=2; k<=n;k++){
if(a[k][i]!=0) break;
}
doidong(n, i, k);
if(k>n){
lkq.setText("Du lieu khong hop le");
resetNghiem();
hpt3an_awt.this.validate();
return;
Báo cáo Thực hành Java 15

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 15
































Giao diện Swing






















//Tim nghiem theo qua trinh nguoc
for(i=n;i>=1;i ){
s=a[i][n+1];
for(k=i+1;k<=n;k++) s-=a[i][k]*x[k];
if(a[i][i]!=0) x[i]=s/a[i][i];
else{
lkq.setText("Vo so nghiem");
resetNghiem();
hpt3an_awt.this.validate();
return;
}
}
//Ra khoi vong lap thi ta co nghiem
lkq.setText("3");
lx1.setText(""+x[1]);
lx2.setText(""+x[2]);
lx3.setText(""+x[3]);
hpt3an_awt.this.validate();

}

private void hoandoi(float a, float b){
float t;
t = a; a=b; b=t;
}

private void doidong(int n, int p, int q){
int k;
if((p<=n&&q<=n)&&(p!=q)){
for(k=1; k<=n+1;k++) hoandoi(a[p][k], a[q][k]);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class hpt3an_swing extends JFrame implements ActionListener{

private static final long serialVersion UID = 1L;
JLabel title, la, lb, lc, ld, kq, lkq, lx1, lx2, lx3;
JTextField ta1, tb1, tc1, td1, ta2, tb2, tc2, td2, ta3, tb3, tc3, td3;
JButton ok, reset, exit;
JPanel p,pnhap, ptit, pbut, pnghiem1;

float[][] a = new float[4][5];
public hpt3an_swing(String text){
super(text);


title = new JLabel("Giai he phuong trinh 3 an:");
la = new JLabel("a ");
lb = new JLabel("b ");
lc = new JLabel("c ");
ld = new JLabel("d ");


16



















kq = new JLabel("So nghiem: ");
lx1 = new JLabel("x1= ");

lx2 = new JLabel("x2= ");
lx3 = new JLabel("x3= ");
lkq = new JLabel("");

ta1 = new JTextField(7);
tb1 = new JTextField(7);
tc1 = new JTextField(7);
td1 = new JTextField(7);
ta2 = new JTextField(7);
tb2 = new JTextField(7);
tc2 = new JTextField(7);
td2 = new JTextField(7);
ta3 = new JTextField(7);
tb3 = new JTextField(7);
tc3 = new JTextField(7);
td3 = new JTextField(7);

ok = new JButton("Tinh");
reset = new JButton("Reset");
exit = new JButton("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);

ptit = new JPanel(new FlowLayout());
pbut = new JPanel(new FlowLayout());
pnghiem1 = new JPanel(new GridLayout(1,2));
p = new JPanel(new BorderLayout());
pnhap=new JPanel(new GridBagLayout());


pbut.add(ok); pbut.add(reset); pbut.add(exit);
ptit.add(title);

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(3, 3, 3, 3);

gbc.gridx=0;
gbc.gridy=1;
pnhap.add(new Label("Pt1: "), gbc);
gbc.gridy=2;
pnhap.add(new Label("Pt2: "), gbc);
gbc.gridy=3;
pnhap.add(new Label("Pt3: "), gbc);
gbc.gridy=4;
pnhap.add(kq, gbc);
gbc.gridx=1;
gbc.gridy=0;
pnhap.add(la,gbc);
gbc.gridy=1;
pnhap.add(ta1,gbc);
gbc.gridy=2;
pnhap.add(ta2,gbc);
gbc.gridy=3;
pnhap.add(ta3,gbc);
gbc.gridy=4;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
pnhap.add(lkq, gbc);
gbc.anchor = GridBagConstraints.BASELINE;
gbc.gridx=2;
gbc.gridy=0;

Báo cáo Thực hành Java 17

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 17



pnhap.add(lb,gbc);
gbc.gridy=1;
pnhap.add(tb1,gbc);
gbc.gridy=2;
pnhap.add(tb2,gbc);
gbc.gridy=3;
pnhap.add(tb3,gbc);

gbc.gridx=3;
gbc.gridy=0;
pnhap.add(lc,gbc);
gbc.gridy=1;
pnhap.add(tc1,gbc);
gbc.gridy=2;
pnhap.add(tc2,gbc);
gbc.gridy=3;
pnhap.add(tc3,gbc);

gbc.gridx=4;
gbc.gridy=0;
pnhap.add(ld,gbc);
gbc.gridy=1;
pnhap.add(td1,gbc);
gbc.gridy=2;

pnhap.add(td2,gbc);
gbc.gridy=3;
pnhap.add(td3,gbc);

gbc.gridx=1;
gbc.gridy=5;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
pnhap.add(lx1, gbc);
gbc.gridx=2;
pnhap.add(lx2, gbc);
gbc.gridx=3;
pnhap.add(lx3, gbc);

p.add(pnhap, BorderLayout.WEST);
p.add(ptit, BorderLayout.NORTH);
p.add(pbut, BorderLayout.SOUTH);
add(p);
setSize(440, 250);
setVisible(true);
setResizable(false);
enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK)
}
//Dong window khi bam nut x
protected void processWindowEvent(WindowEvent e){
if (e.getID()== WindowEvent.WINDOW_CLOSING){
dispose();
System.exit(0);
}
super.processWindowEvent(e);
}


public static void main(String[] args){
hpt3an_swing b = new hpt3an_swing("Ghptb2");
}

18






















@Override
public void actionPerformed(ActionEvent arg0) {

JButton t = (JButton) arg0.getSource();
if(t==ok){
giai();
}
if(t==reset){
ta1.setText("");
tb1.setText("");
tc1.setText("");
td1.setText("");
ta2.setText("");
tb2.setText("");
tc2.setText("");
td2.setText("");
ta3.setText("");
tb3.setText("");
tc3.setText("");
td3.setText("");
lkq.setText("");
resetNghiem();
}
if(t==exit){
System.exit(0);
}
}
public void resetNghiem(){
lx1.setText("x1= ");
lx2.setText("x2= ");
lx3.setText("x3= ");
}
public void giai(){

hpt3an_swing.this.invalidate();
float m,s;
float[] x = new float[4];
int i, j, k, n;
n=3;//Set cai nay cho khoe. Vo bac 3 chi can set lai
try{
a[1][1] = Float.pa rseFloat(ta1.getText());
a[1][2] = Float.pa rseFloat(tb1.getText());
a[1][3] = Float.pa rseFloat(tc1.getText());
a[1][4] = Float.pa rseFloat(td1.getText());
a[2][1] = Float.pa rseFloat(ta2.getText());
a[2][2] = Float.pa rseFloat(tb2.getText());
a[2][3] = Float.pa rseFloat(tc2.getText());
a[2][4] = Float.pa rseFloat(td2.getText());
a[3][1] = Float.pa rseFloat(ta3.getText());
a[3][2] = Float.pa rseFloat(tb3.getText());
a[3][3] = Float.pa rseFloat(tc3.getText());
a[3][4] = Float.pa rseFloat(td3.getText());
}catch(Exception e){
lkq.setText("Loi Format");
resetNghiem();
hpt3an_swing.this.validate();
return;
}

//Bien doi a ve ma tran tam giac tren
for(i=1; i<n; i++){
if(a[i][i]==0){
for(k=2; k<=n;k++){
if(a[k][i]!=0) break;

}
Báo cáo Thực hành Java 19

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 19






doidong(n, i, k);
if(k>n){
lkq.setText("Du lieu khong hop le");
resetNghiem();
hpt3an_swing.this.validate();
return;
}
}
for(j=i+1; j<=n; j++){
m = -a[j][i]/a[i][i];
for(k=i;k<=n+1;k++) a[j][k]+=a[i][k]*m;
}
}

//Tim nghiem theo qua trinh nguoc
for(i=n;i>=1;i ){
s=a[i][n+1];
for(k=i+1;k<=n;k++) s-=a[i][k]*x[k];
if(a[i][i]!=0) x[i]=s/a[i][i];
else{

lkq.setText("Vo so nghiem");
resetNghiem();
hpt3an_swing.this.validate();
return;
}
}
//Ra khoi vong lap thi ta co nghiem
lkq.setText("3");
lx1.setText(""+x[1]);
lx2.setText(""+x[2]);
lx3.setText(""+x[3]);
hpt3an_swing.this.validate();
}

private void hoandoi(float a, float b){
float t;
t = a; a=b; b=t;
}

private void doidong(int n, int p, int q){
int k;
if((p<=n&&q<=n)&&(p!=q)){
for(k=1; k<=n+1;k++) hoandoi(a[p][k], a[q][k]);
}
}
}

20
2.3 Demo





Báo cáo Thực hành Java 21

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 21

3. Phương trình bậc hai
3.1 Thuật toán
B1: Khai báo giá trị các biến: a,b,c,delta,x1,x2
B2: Đọc giá trị các biến từ bàn phím
B3: Lập delta = b*b – 4*a*c
B4: Nếu delta = 0 thì phương trình có nghiệm kép
Ngược lại nếu delta > 0 phương trình có 2 nghiệm thực
Ngược lại phương trình vô nghiệm
B5: Viết kết quả ra màn hình
3.2 Chương trình
Giao diện Awt


























import java.awt.*;
import java.awt.event.*;

class ptbachai_awt extends Frame implements ActionListener {

Label lb1,lb2,lb3,lb4,lb5;
TextField tf1,tf2,tf3,tf4;
Button tinh,reset,exit;
Panel pn,pn1,pn2,pn3;

public ptbachai_awt() {
lb1 = new Label("Chuong trinh giai phuong trinh bac hai!!!");
lb2 = new Label("Nhap he so a : ");
lb3 = new Label("Nhap he so b : ");
lb4 = new Label("Nhap he so c : ");
lb5 = new Label("Ket qua nghiem : ");


tf1 = new TextField(40);
tf2 = new TextField(40);
tf3 = new TextField(40);
tf4 = new TextField(42);
tf4.setEditable(false);

tinh = new Button("Tinh");
tinh.addActionListener(this);
reset = new Button("Reset");
reset.addActionListener(this);
exit = new Button("Exit");
exit.addActionListener(this);

pn1 = new Panel(new FlowLayout());
pn1.add(lb1);
pn2 = new Panel(new FlowLayout());
pn2.add(lb2);
pn2.add(tf1);
pn2.add(lb3);
pn2.add(tf2);
pn2.add(lb4);
pn2.add(tf3);
pn2.add(lb5);

22























pn2.add(tf4);
pn3 = new Panel(new FlowLayout());
pn3.add(tinh);
pn3.add(reset);
pn3.add(exit);
pn = new Panel(new GridLayout(3,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
}

public static void main(String []args) {

ptbachai_awt dt = new ptbachai_awt();
dt.setSize(500,400);
dt.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource()==reset)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}

if (e.getSource()==exit)
{
System.exit(0);
}
if (e.getSource()==tinh)
{
float a,b,c,x;
float delta,x1,x2,delta1;
a = Integer.parseInt(tf1.getText());
b = Integer.parseInt(tf2.getText());
c = Integer.parseInt(tf3.getText());

if (a==0)
{

if (b==0 && c==0)
{
tf4.setText("Phuong trinh co vo so nghiem");
}
else
if (b==0 && c!=0)
{
tf4.setText("Phuong trinh vo nghiem");
}
else
{
x=-c/b;
tf4.setText("Mot nghiem : "+x);
}
}else
{
delta=(b*b)-(4*a*c);
delta1=(float)Math.sqrt(delta);
if (delta<0)
Báo cáo Thực hành Java 23

Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 23























Giao diện Swing























{
tf4.setText("Phuong trinh vo nghiem");
}
else
if (delta==0)
{
x=-b/(2*a);
tf4.setText("Mot nghiem : "+x);
}
else
{
x1=(-b+delta1)/(2*a);
x2=(-b-delta1)/(2*a);
tf4.setText("Hai nghiem : "
+Float.toString(x1) +" " +Float.toString(x2));
}
}
}
}

}
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class ptbachai_swing extends JFrame implements ActionListener {

JLabel lb1,lb2,lb3,lb4,lb5;
JTextField tf1,tf2,tf3,tf4;
JButton tinh,reset,exit;
JPanel pn,pn1,pn2,pn3;

public ptbachai_swing() {
lb1 = new JLabel("Chuong trinh giai phuong trinh bac hai!!!");
lb2 = new JLabel("Nhap he so a : ");
lb3 = new JLabel("Nhap he so b : ");
lb4 = new JLabel("Nhap he so c : ");
lb5 = new JLabel("Ket qua nghiem : ");

tf1 = new JTextField(30);
tf2 = new JTextField(30);
tf3 = new JTextField(30);
tf4 = new JTextField(32);
tf4.setEditable(false);

tinh = new JButton("Tinh");
tinh.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
exit = new JButton("Exit");
exit.addActionListener(this);
pn1 = new JPanel(new FlowLayout());
pn1.add(lb1);

pn2 = new JPanel(new FlowLayout());
pn2.add(lb2);

24






















pn2.add(tf1);
pn2.add(lb3);
pn2.add(tf2);
pn2.add(lb4);

pn2.add(tf3);
pn2.add(lb5);
pn2.add(tf4);
pn3 = new JPanel(new FlowLayout());
pn3.add(tinh);
pn3.add(reset);
pn3.add(exit);
pn = new JPanel(new GridLayout(3,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
}

public static void main(String []args) {
ptbachai_swing dt = new ptbachai_swing();
dt.setSize(500,400);
dt.setResizable(false);
dt.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource()==reset)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");

}
if (e.getSource()==exit)
{
System.exit(0);
}

if (e.getSource()==tinh)
{
float a,b,c,x;
float delta,x1,x2,delta1;
a = Integer.parseInt(tf1.getText());
b = Integer.parseInt(tf2.getText());
c = Integer.parseInt(tf3.getText());

if (a==0)
{
if (b==0 && c==0)
{
tf4.setText("Phuong trinh co vo so nghiem");
}
else
if (b==0 && c!=0)
{
tf4.setText("Phuong trinh vo nghiem");
}
else
{

Báo cáo Thực hành Java 25


Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 25



if (b==0 && c!=0)
{
tf4.setText("Phuong trinh vo nghiem");
}
else
{
x=-c/b;
tf4.setText("Mot nghiem : "+x);
}
}

else
{
delta=(b*b)-(4*a*c);
delta1=(float)Math.sqrt(delta);
if (delta<0)
{
tf4.setText("Phuong trinh vo nghiem");
}
else
if (delta==0)
{
x=-b/(2*a);
tf4.setText("Mot nghiem : "+x);
}
else

{
x1=(-b+delta1)/(2*a);
x2=(-b-delta1)/(2*a);
tf4.setText("Hai nghiem : "
+Float.toString(x1) +" " +Float.toString(x2));
}
}
}
}

}

×