Hệ phương trình hai ẩn .... tf6 = new TextField40; tf7 = new TextField40; tf7.setEditablefalse; //Khoi tao Button tinh = new Button"Tinh"; tinh.addActionListenerthis; reset = new Button"
Trang 1ĐẠ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: cntt@edu.ud.vn
ĐÀ NẴNG, 12/2013
Trang 2Báo cáo thực hành Java
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
Trang 39.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
Trang 4Báo cáo thực hành Java
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
Trang 5Ngược lại hệ phương trình vô định
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
Trang 6tf6 = 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());
//Khoi tao ham main
public static void main(String[] args) {
hpt2an_awt nt = new hpt2an_awt();
System.exit(0);
}
if (e.getSource()==reset) {
Trang 7Giao diện Swing
if (e.getSource()==tinh) {
if ((d2==0) && (d1==d2)) {
tf7.setText("He phuong trinh vo nghiem");
if (d==0) tf7.setText("Phuong trinh vo nghiem!!!");
} }
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 : ");
Trang 8//Khoi tao TextField
//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());
//Khoi tao ham main
public static void main(String[] args) {
hpt2an_swing nt = new hpt2an_swing();
System.exit(0);
}
Trang 9if (e.getSource()==reset) {
if ((d2==0) && (d1==d2)) {
tf7.setText("He phuong trinh vo nghiem");
if (d==0) tf7.setText("Phuong trinh vo nghiem!!!");
} }
}
Trang 101.3 Demo
Trang 112 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 serialVersionUID = 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 ");
Trang 12ta2 = 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);
Trang 13p.add(pnhap, BorderLayout.WEST);
p.add(ptit, BorderLayout.NORTH);
p.add(pbut, BorderLayout.SOUTH);
//Dong window khi bam nut x
protected void processWindowEvent(WindowEvent e){
if (e.getID()== WindowEvent.WINDOW_CLOSING){
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();
}
Trang 14public void resetNghiem(){
lx1.setText("x1= ");
lx2.setText("x2= ");
lx3.setText("x3= ");
}
public void giai(){
hpt3an_awt.this.invalidate();
}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( [i][i]==0){
for(k=2; k<=n;k++){
if( [k][i]!=0) break; }
Trang 15Giao 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( [i][i]!=0) x[i]=s/a[i][i];
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class hpt3an_swing extends JFrame implements ActionListener{
private static final long serialVersionUID = 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 ");
Trang 16kq = new JLabel("So nghiem: ");
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);
Trang 17p.add(pnhap, BorderLayout.WEST);
p.add(ptit, BorderLayout.NORTH);
p.add(pbut, BorderLayout.SOUTH);
//Dong window khi bam nut x
protected void processWindowEvent(WindowEvent e){
if (e.getID()== WindowEvent.WINDOW_CLOSING){
public static void main(String[] args){
hpt3an_swing b = new hpt3an_swing("Ghptb2");
}
Trang 18public void actionPerformed(ActionEvent arg0) {
JButton t = (JButton) arg0.getSource();
public void resetNghiem(){
lx1.setText("x1= ");
lx2.setText("x2= ");
lx3.setText("x3= ");
}
public void giai(){
hpt3an_swing.this.invalidate();
}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( [i][i]==0){
for(k=2; k<=n;k++){
if( [k][i]!=0) break; }
Trang 19for(k=i+1;k<=n;k++) s-=a[i][k]*x[k];
if( [i][i]!=0) x[i]=s/a[i][i];
} }
Trang 202.3 Demo
Trang 21B4: 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
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 : ");
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());
Trang 22public static void main(String []args) {
ptbachai_awt dt = new ptbachai_awt();
System.exit(0);
}
if (e.getSource()==tinh) {
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"); }
{
delta=(b*b)-(4*a*c);
delta1=(float)Math.sqrt(delta);
if (delta<0)
Trang 23Giao diện Swing
x=-b/(2*a);
tf4.setText("Mot nghiem : "+x); }
else {
} }
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 : ");
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);
Trang 24public static void main(String []args) {
ptbachai_swing dt = new ptbachai_swing();
System.exit(0);
}
if (e.getSource()==tinh) {
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
{
Trang 25tf4.setText("Phuong trinh vo nghiem");
}
else
if (delta==0) {
x=-b/(2*a);
tf4.setText("Mot nghiem : "+x);
}
else {
} }
}
Trang 263.3 Demo
Trang 27Nếu delta = 0
Gán: t =-b / 2*a + Nếu ( t > 0 ) phương trình có 2 nghiệm + Nếu t < 0 thì phương trình vô nghiệm Nếu delta < 0 thì phương trình vô nghiệm
public pttrungphuong_awt() {
lb1 = new Label("Chuong trinh giai phuong trinh trung phuong!!!");
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 : ");
tinh = new Button("Tinh");
tinh.addActionListener(this);
reset = new Button("Reset");
Trang 28reset.addActionListener(this);
exit = new Button("Exit");
exit.addActionListener(this);
pn1 = new Panel(new FlowLayout());
public static void main(String []args) {
ptbachai_awt dt = new ptbachai_awt();
System.exit(0);
}
if (e.getSource()==tinh) {
tf4.setText("Phuong trinh vo nghiem");
}
if(del==0) {
float t = (float)-b/2*a;
Trang 29if(t>0) {
tf4.setText("Phuong trinh co nghiem duy nhat : x = 0");
}
if(t<0) {
tf4.setText("Phuong trinh vo nghiem"); }
}
if(del>0) {
float t1 = (float)(-b+Math.sqrt(del))/(2*a);
float t2 = (float)(-b-Math.sqrt(del))/(2*a);
tf4.setText("4 nghiem : " +Float.toString(x1) +" "
+Float.toString(x2) + " "+ Float.toString(x3) +" " +Float.toString(x4));
}
if(t1>=0 && t2<0) {
tf4.setText("Phuong trinh vo nghiem");
} }
} }
}
Trang 30Giao diện Swing
public pttrungphuong_swing() {
lb1 = new JLabel("Chuong trinh giai phuong trinh trung phuong!!!");
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 : ");
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());
public static void main(String []args) {
pttrungphuong_swing dt = new pttrungphuong_swing();
dt.setResizable(false);
dt.setSize(500,400);
Trang 31System.exit(0);
}
if (e.getSource()==tinh) {
tf4.setText("Phuong trinh vo nghiem");
}
if(del==0) {
float t = (float)-b/2*a;
if(t>0) {
tf4.setText("Phuong trinh co nghiem duy nhat : x = 0");
}
if(t<0) {
tf4.setText("Phuong trinh vo nghiem"); }
}
if(del>0)
{
float t1 = (float)(-b+Math.sqrt(del))/(2*a);
float t2 = (float)(-b-Math.sqrt(del))/(2*a);
if(t1>=0 && t2>=0){
x1 = (float) Math.sqrt(t1);
x2 = - (float) Math.sqrt(t1);
Trang 32x3 = (float) Math.sqrt(t2);
x4 = - (float) Math.sqrt(t2);
tf4.setText("4 nghiem : " +Float.toString(x1) +" "
+Float.toString(x2) + " "+ Float.toString(x3) +" " +Float.toString(x4));
}
if(t1>=0 && t2<0) {
tf4.setText("Phuong trinh vo nghiem");
} }
} }
}
Trang 334.4 Demo
Trang 34BÀI THỰC HÀNH BUỔI 2
way that each click on the button increases the number displayed in the text field by one The button should have an image above You can choose any image you want
private ImageIcon icon=null;
private Image image=null;
image = Toolkit.getDefaultToolkit().getImage("smile.png");
icon=new ImageIcon(image);
tf = new JTextField(4); this.add(tf);
bt=new JButton("Image With Button",icon);
bt.addActionListener(this);
this.add(bt);
} public static void main(String arg[]) {
bai1 ex=new bai1();
JFrame frame=new JFrame("Example 1");
frame.setSize(200,150);
frame.getContentPane().add(ex,"Center");
frame.setVisible(true);
} }
Trang 35Nguyễn Thanh Vũ – 10T2 – Nhóm 12A 35
1.2 Demo
The calculator should have 3 simple buttons that allow changing its
appearance look & feel
public static void main(String[] args) {
frame=new JFrame();
frame.setTitle("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel mainPanel=new JPanel(new GridBagLayout());
GridBagConstraints ct=new GridBagConstraints();
ct.fill=GridBagConstraints.BOTH;
ct.weightx=1.0;
ct.weighty=1.0;
ct.insets=new Insets(10, 10, 0, 10);
ct.gridx=0;ct.gridy=0;ct.gridwidth=1;ct.gridheight=1;
JPanel buttonPanel=new JPanel(new GridLayout(1, 3));
mainPanel.add(buttonPanel,ct); buttonPanel.add(createButton("Window",
laf1 )); buttonPanel.add(createButton("Metal", laf2));
buttonPanel.add(createButton("Window classic", laf3));
ct.gridx=0;ct.gridy=1;ct.gridwidth=1;ct.gridheight=4;ct.weighty=10.0;
ct.insets=new Insets(10, 10, 10, 10);
CalculatorPanel calculatorPanel=new CalculatorPanel();
mainPanel.add(calculatorPanel,ct); frame.add(mainPanel,BorderLayout.CENTER);
frame.setSize(400, 600);
Trang 36private static JButton createButton(String label,final String laf){ JButton bt=new JButton(label); bt.addActionListener(new
} } }); return bt;
}
private static JFrame frame; private static String
laf1="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
private static String laf2="javax.swing.plaf.metal.MetalLookAndFeel";
private static String
laf3="com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"; }
class CalculatorPanel extends JPanel{ public CalculatorPanel(){
setLayout(new GridBagLayout());
screenResult=new JTextField("0"); screenResult.setFont(new
Font("Tahoma", Font.BOLD, 40));
screenResult.setHorizontalAlignment(JTextField.RIGHT); gt=new
GridBagConstraints(); gt.fill=GridBagConstraints.BOTH;
gt.weightx=1.0;
gt.weighty=2.0;
gt.insets=new Insets(5, 0, 0, 0);
gt.gridx=0;gt.gridy=0;gt.gridwidth=4;gt.gridheight=2;
Trang 37ActionListener btAction=new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
isResulted=false;
hasDot=false; }else{
else{
screenResult.setText(label);
} }else{
screenResult.setText(str+label);
}
Trang 38} } } };
addButton("<-", 0, 2, 1, 1, new ActionListener() { public void
else if(isOperator(String.valueOf(c))) operatorPosition=0;
String s=str.substring(0, str.length()-1); if(s.length()==0){
s="0";
isResulted=true; }
screenResult.setText(s);
} } } });
addButton("CE", 1, 2, 1, 1, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
} } });
addButton("C", 2, 2, 1, 1, new ActionListener() {
public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub
hasDot=false;
isResulted=true;
screenResult.setText("0");
} });
addButton("/", 3, 2, 1, 1, btAction); addButton("7", 0, 3, 1, 1, btAction); addButton("8", 1, 3, 1, 1, btAction); addButton("9", 2, 3,
1, 1, btAction); addButton("*", 3, 3, 1, 1, btAction); addButton("4",
0, 4, 1, 1, btAction); addButton("5", 1, 4, 1, 1, btAction);
addButton("6", 2, 4, 1, 1, btAction); addButton("-", 3, 4, 1, 1, btAction); addButton("1", 0, 5, 1, 1, btAction); addButton("2", 1, 5,
1, 1, btAction); addButton("3", 2, 5, 1, 1, btAction); addButton("+",
3, 5, 1, 1, btAction); addButton("0", 0, 6, 2, 1, btAction);
addButton(".", 2, 6, 1, 1, btAction); addButton("=", 3, 6, 1, 1, new
Trang 39JButton button=new JButton(label); button.setFont(new Font("Tahoma", Font.PLAIN, 20)); button.addActionListener(action);
gt.gridx=gx;gt.gridy=gy;gt.gridwidth=gw;gt.gridheight=gh;
double p1=Double.parseDouble(str.substring(0, operatorPosition));
double p2=Double.parseDouble(str.substring(operatorPosition+1));
char c=str.charAt(operatorPosition); double result;
switch(c){
case '+':result=p1+p2;break; case '-':result=p1-p2;break; case
'/':result=p1/p2;break; case '*':result=p1*p2;break; default:return; }
}
}
}
private boolean isOperator(String str){
return str.equals("+")||str.equals("")||str.equals("*")||str.equals("/"); }
private JTextField screenResult;
private GridBagConstraints gt;
private int operatorPosition;
private boolean isResulted;
private boolean hasDot;
private Font font; }
Trang 402.2 Demo