ĐẠ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: [email protected]
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
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
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
6 tf6 = new TextField(40);
tf7 = new TextField(40);
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("");
Giao diện Swing if (e.getSource()==tinh)
{
int a1,b1,c1,a2,b2,c2;
}
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 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);
{
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
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 ");
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(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){
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();
Giao diện Swing
}
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);
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());
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;
public static void main(String[] args){
hpt3an_swing b = new hpt3an_swing("Ghptb2");
}
18
@Override
public void actionPerformed(ActionEvent arg0) {
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;
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
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 : ");
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) {
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
{
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.*;
pn2 = new JPanel(new FlowLayout());
pn2.add(lb2);
24
pn2.add(tf1);
pn2.add(lb3);
pn2.add(tf2);
pn2.add(lb4);
}
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
{
x1=(-b+delta1)/(2*a);
x2=(-b-delta1)/(2*a);
tf4.setText("Hai nghiem : "
+Float.toString(x1) +" " +Float.toString(x2));
}
}
}
}
}