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

Bài thực hành ngôn ngữ 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.28 MB, 54 trang )

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

BÀI THỰC HÀNH NGÔN NGỮ LẬP TRÌNH JAVA
CHƯƠNG I:

CÁC KIẾN THỨC CƠ BẢN-CẤU TRÚC
CHƯƠNG TRÌNH JAVA

A. Cấu trúc lựa chọn:
1. Giải phương trình bậc nhất ax+b=0:
package baocao;
import java.util.Scanner;
public class Bai1 {
private float a;
private float b;
private Scanner input;
public void nhap() {
input = new Scanner(System.in);
System.out.println("Nhap cac he so:");
System.out.print("a = ");
a = input.nextFloat();
System.out.print("b = ");
b = input.nextFloat();
}
public void giai() {
if (a == 0) {
if (b == 0 )
System.out.println("PT co vo so nghiem");
else
System.out.println("PT vo nghiem");
}


else
System.out.println("PT co 1 nghiem: x = " + (-b/a));
}
public static void main(String arg[]) {
Bai1 dt = new Bai1();
dt.nhap();
dt.giai();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 1


THỰC HÀNH LẬP TRÌNH JAVA
2. Phương trình bậc 2 ax2+bx+c=0:
package baocao;
import java.util.Scanner;
public class Bai2 {
float a,b,c;
public void nhap() {
Scanner input = new Scanner(System.in);
System.out.println("Nhap cac he so");
System.out.print("a = ");
a = input.nextFloat();
System.out.print("b = ");
b = input.nextFloat();
System.out.print("c = ");
c = input.nextFloat();

}
public void giai() {
if (a == 0)
if (b == 0)
if (c == 0)
System.out.println("PT co vo so nghiem");
else
System.out.println("PT vo nghiem");
else {
if (b == 0) {
if (c == 0 )
System.out.println("PT co vo so nghiem");
else
System.out.println("PT vo nghiem");
}
else
System.out.println("PT co 1 nghiem: x = " + (-c/b));
}
else {
float delta = b*b - 4*a*c;
if (delta < 0)
System.out.println("PT vo nghiem");
else if (delta == 0)
System.out.println("PT co 1 nghiem kep: x = " + (-b/(2*a)));
else {
System.out.println("PT co 2 nghiem phan biet");
System.out.println("x1 = " + ((-b-Math.sqrt(delta))/(2*a)));
System.out.println("x2 = " + ((-b+Math.sqrt(delta))/(2*a)));
}
}

}
public static void main(String areg[]) {
Bai2 dt = new Bai2();
dt.nhap();
dt.giai();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 2


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

3. Tìm số trung gian của 3 số a,b,c:
package baocao;
import java.util.Scanner;
public class Bai3 {
float a,b,c;
public void nhap()
{
Scanner input=new Scanner(System.in);
System.out.println("Nhap a b c:");
System.out.print("a= ");
a=input.nextFloat();
System.out.print("b= ");
b=input.nextFloat();
System.out.print("c= ");
c=input.nextFloat();

}
public void tg(){
float tg;
if(((a<=b)&&(b<=c))||((c<=b)&&(b<=a))) tg=b;
else if(((b<=a)&&(a<=c))||((c<=a)&&(a<=b))) tg=a;
else tg=c;
System.out.println(" So trung gian la "+tg);
}
public static void main (String[] args) {
Bai3 stg=new Bai3();
stg.nhap();
stg.tg();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 3


THỰC HÀNH LẬP TRÌNH JAVA
4. Viết chương trình tính tiền cho bài toán karaoke:
package baocao;
import java.util.*;
public class Bai4
{
int a,b;
public Bai4()
{
a=0;b=0;

}
public void nhap()
{
Scanner input =new Scanner(System.in);
System.out.println("Nhap gio bat dau ");
a = input.nextInt();
System.out.println("Nhap gio ket thuc ");
b = input.nextInt();
}
public int tinhtien()
{
int sotien=0;
if(a> 0 && b<=18) sotien = (b-a)*45000;
if(a>0 && b>18) sotien = (18-a)*45000 + (b-18)*60000;
if(a>18) sotien = (b-a)*60000;
return sotien;
}
public static void main(String[] str)
{
Bai4 k =new Bai4();
k.nhap();
System.out.println("So tien can fai tra la : "+k.tinhtien());
}
}

5. Nhập vào tháng năm bất kì in ra số ngày ứng với tháng năm đó:
package baocao;
import java.util.Scanner;
public class Bai5 {
int nam,thang;

public Bai5() {
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 4


THỰC HÀNH LẬP TRÌNH JAVA
public boolean namnhuan(int nam)
{
if((nam%4==0 && nam%100!=0)||(nam%400==0)) return
else return false;
}
public void nhap()

true;

{
Scanner input= new Scanner (System.in);
System.out.println("Nhap nam thang ");
System.out.print("Nam ");
nam=input.nextInt();
System.out.print("Thang ");
thang=input.nextInt();
}
public void inra()
{
switch(thang)
{

case 4:
case 6:
case 9:
case 11: System.out.println("Thang "+thang+" nam "+nam+" co 30 ngay");break;
case 2 : if(namnhuan(nam))
{System.out.println("Thang "+thang+" nam"+nam+" co 29 ngay");
break; }
else {System.out.println("Thang "+thang+" nam "+nam+" co 28 ngay");
break;}
default: System.out.println("Thang "+thang+" nam "+nam+" co 31 ngay");break;
}
}
public static void main (String[] args) {
Bai5 temp= new Bai5();
temp.nhap();
temp.inra();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 5


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

B. Cấu trúc lặp
1. Viết chương trình tính: 1+1/2+1/3+...+1/n
package baocao;
import java.util.Scanner;

public class Bai6 {
int n;
public void nhapn()
{
Scanner input=new Scanner(System.in);
System.out.println("Nhap n");
n=input.nextInt();
}
public void tinh()
{
float s=0;
for(int i=1;i<=n;i++) s+=(float) 1/i;
System.out.println("S= "+s);
}
public static void main (String[] args) {
Bai6 b6=new Bai6();
b6.nhapn();
b6.tinh() ;
}
}

2. Viết chương trình tính : S=15-1+1/2-1/3!+....+(-1)n 1/n!
package baocao;
import java.util.Scanner;
public class Bai7
{
int n;
public void nhap()
{
Scanner input =new Scanner(System.in);

System.out.print("Nhap n = ");
n = input.nextInt();
}
public int giaithua(int m)
{
if(m==0) return 1;
else return m*giaithua(m-1);
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 6


THỰC HÀNH LẬP TRÌNH JAVA
public void tinh()
{
float s=15.0f;
for(int i=1;i<=n;i++)
{
float p = 1.0f/giaithua(i); p*=Math.pow(-1,i);
s+=p;
}
System.out.print("S = " + s);
}
public static void main(String[] str)
{
Bai7 b = new Bai7();
b.nhap();
b.tinh();

}
}

3. Viết chương trình tính : S=1+1/3!+1/5!+…..+1/(2n-1)!
package baocao;
import java.util.Scanner;
public class Bai8
{
int n;
public void nhap()
{
Scanner input =new Scanner(System.in);
System.out.println("Nhap n : ");
n = input.nextInt();
}
public int giaithua(int m)
{
if(m==0) return 1;
else return m*giaithua(m-1);
}
public void tinh()
{
float s=0.0f;
for(int i=1;i<2*n;i+=2) s+=1.0f/giaithua(i);
System.out.print("S = " + s);
}
public static void main(String[] str)
{
Bai8 b = new Bai8();
b.nhap();


TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 7


THỰC HÀNH LẬP TRÌNH JAVA
b.tinh();
}
}

4. Tính n!! = 1*3*5*…..*n(n lẽ)
= 2*4*6*….*n(n chẵn)
package baocao;
import java.util.Scanner;
public class Bai9 {
int n;
public void nhap()
{
Scanner input=new Scanner(System.in);
System.out.print("Nhap n= ");
n=input.nextInt();
}
public void tinh()
{
float gt=1;
//int i;
for(int i=n;i>=1;i-=2) gt*=i;
//for(i=((n%2)==0)?2:1;i<=n;i+=2) gt*=i;
System.out.println(n+"!!= "+gt);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai9 dt=new Bai9();
dt.nhap();
dt.tinh();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 8


THỰC HÀNH LẬP TRÌNH JAVA
5. Tính tổng và tích các chữ số của một số nguyên dương m cho trước:
package baocao;
import java.util.Scanner;
public class Bai10 {
int m;
public void nhap()
{
Scanner input=new Scanner(System.in);
System.out.print("Nhap so nguyen duong m= ");
m=input.nextInt();
}
public void tinhtong()
{
int s=0,p=1,tam=m;
while(tam!=0)

{
s+=tam%10;
p*=tam%10;
tam=tam/10;
}
System.out.println("Tong cac chu so cua "+m+" la "+s);
System.out.println("Tich cac chu so cua "+m+" la "+p);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai10 dt=new Bai10();
dt.nhap();
dt.tinhtong();
}
}

6. Nhập một số và kiểm tra có phải nguyên tố không?
package baocao;
import java.util.Scanner;
public class Bai11 {
static int n;
public int get() {
return n;
}
public void nhap() {
Scanner input = new Scanner(System.in);
System.out.print("Nhap so : ");
n = input.nextInt();

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A


Trang 9


THỰC HÀNH LẬP TRÌNH JAVA
}
public boolean check() {
int i=2;
if (n == 0 || n == 1) return false;
while (i <= n/2) {
if (n%i == 0) return false;
i ++;
}
return true;
}
public static void main(String args[]) {
Bai11 a = new Bai11();
a.nhap();
if (a.check()) System.out.println(n + " la so nguyen to");
else System.out.println(a.get() + " ko phai la so nguyen to");
}
}

7. Kiểm tra số P có phải là số chính phương không?
package baocao;
import java.util.Scanner;
public class Bai12 {
static int n;
private Scanner input;
void nhap(){

input=new Scanner(System.in);
System.out.printf("Nhap n= ");
n=input.nextInt();
}
public boolean chinhphuong(){
for(int i=0;i<=n;i++) if(i*i==n) return true;
return false;
}
public static void main(String args[])
{
Bai12 dt =new Bai12();
dt.nhap();
if(dt.chinhphuong()) System.out.println(n+ " la so chinh phuong");
else System.out.println(n+ " khong la so chinh phuong");
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 10


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

8. Kiểm tra số M có phải là số đối xứng không?
package baocao;
import java.util.Scanner;
public class Bai13 {
static int n;
private Scanner input;

public void nhap(){
input=new Scanner(System.in);
System.out.print("Nhap n=");
n=input.nextInt();
}
public void doixung(int n){
int dao=0,tam=n;
while(tam!=0)
{
dao*=10;
dao+=tam%10;
tam/=10;
}
if(dao==n) System.out.println(n+ " la so doi xung");
else System.out.println(n+" Khong phai la so doi xung");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai13 dt=new Bai13();
dt.nhap();
dt.doixung(n);
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 11


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


9. In ra các số nguyên tố nhỏ hơn hoặc bằng số nguyên dương n cho trước:
package baocao;
import java.util.Scanner;
public class Bai14 {
int n;
private Scanner input;
public void nhap(){
input=new Scanner(System.in);
System.out.print("Nhap n=");
n=input.nextInt();
}
public boolean nto(int n)
{
if(n==0||n==1) return false;
else for(int i=2;ireturn true;
}
public void in(){
System.out.println("Cac so nguyen to <= " +n+ " la : ");
for(int i=2;i<=n;i++) if(nto(i)) System.out.print(" "+i);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai14 dt=new Bai14();
dt.nhap();
dt.in();
}
}


10. In ra các số hoàn hảo nhỏ hơn 1000.
( Ví dụ : 6=1+2+3, 28=1+2+4+7+14)
package baocao;
public class Bai15 {
public boolean hh(int n)
{
int sum=0;
for (int i=1;iif (sum==n) return true;
return false;
}
public void inra()
{
System.out.println ("Cac so hoan hao <1000 la: ");

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 12


THỰC HÀNH LẬP TRÌNH JAVA
for(int i=1;i<1000;i++) if(hh(i)) System.out.println(i);
}
public static void main(String[] args) {
Bai15 dt=new Bai15();
dt.inra();
}
}

11. In ra n chữ số Fibonaci đầu tiên:

package baocao;
public class Bai16 {
int n;
public Bai16(int m)
{
n=m;
}
public int fibo(int i)
{
if(i==1||i==2) return 1;
else return (fibo(i-1)+fibo(i-2));
}
public void inra()
{
System.out.println(n+" Chu so Fibonaci dau tien la: ");
for(int i=1;i<=n;i++)
System.out.print(fibo(i)+" ");
}
public static void main (String[] args)
{
Bai16 dt=new Bai16(10);
dt.inra();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 13



THỰC HÀNH LẬP TRÌNH JAVA
12. Kiểm tra số K có thuộc dãy Fibonaci hay không?
package baocao;
import java.util.Scanner;
public class Bai17 {
int n;
private Scanner input;
public void nhap()
{
input=new Scanner(System.in);
System.out.print("Nhap n= ");
n=input.nextInt();
}
public void testnto()
{
int x=0,y=1,z=0;
while(z{
z=x+y;
x=y;
y=z;
}
if(z==n) System.out.println(n+ " la so FIBONACI");
else System.out.println(n+" Khong phai la so FBONACI");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai17 dt=new Bai17();
dt.nhap();
dt.testnto();

}
}

13. Tìm ước chung lớn nhất và bội chung nhỏ nhất của 2 số a và b:
package baocao;
import java.util.Scanner;
public class Bai18 {
int a,b;
private Scanner input;
public void nhap()
{
input=new Scanner(System.in);
System.out.println("Nhap 2 so: ");
System.out.print("a= ");
a=input.nextInt();
System.out.print("b= ");

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 14


THỰC HÀNH LẬP TRÌNH JAVA
b=input.nextInt();
}
public void ucbc()
{
int x=a,y=b,uc;
if(x==y) uc=x;
else{

while(x!=y)
{if(x>y) x=x-y;
else y=y-x;
}
uc=x;
}
System.out.println("UCLN cua "+a+" va "+b+" la: "+uc);
System.out.println("BCNN cua "+a+" va "+b+" la: "+(a*b)/uc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai18 dt=new Bai18();
dt.nhap();
dt.ucbc();
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 15


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

CHƯƠNG II:

LẬP TRÌNH AWT-SWING

1. Giải phương trình bậc nhất:
package baocao;

import java.awt.*;
import java.awt.event.*;
public class Bai19 extends Frame implements ActionListener
{
Label lb1,lb2,lb3,lb;
TextField txta,txtb,txtkq;
Button kq,reset,thoat;
Panel pn,pn1,pn2,pn3;
public void GUI() {
lb=new Label("Giai phuong trinh bac nhat");
lb1=new Label("Nhap a");
lb2=new Label("Nhap b");
lb3=new Label("Ket qua");
txta=new TextField();
txtb=new TextField();
txtkq=new TextField();
kq=new Button("Tinh");
reset=new Button("Reset");
thoat=new Button("Thoat");
kq.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(3,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(3,2));
pn3=new Panel(new GridLayout(1,3));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txta);
pn2.add(lb2);

pn2.add(txtb);
pn2.add(lb3);
pn2.add(txtkq);
pn3.add(kq);
pn3.add(reset);
pn3.add(thoat);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==kq)

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 16


THỰC HÀNH LẬP TRÌNH JAVA
{
int a=Integer.parseInt(txta.getText());
int b=Integer.parseInt(txtb.getText());
if(a!=0) txtkq.setText(Float.toString((float)-b/a) );
else if(b==0) txtkq.setText("Pt vo so nghiem");
else txtkq.setText("Pt vo nghiem");
}

if(e.getSource()==reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
if(e.getSource()==thoat) System.exit(0);
}
public Bai19(String st)
{
super(st);
GUI();
}
public static void main (String[] args) {
new Bai19("Giai pt bac 1");
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 17


THỰC HÀNH LẬP TRÌNH JAVA
2. Minh họa các phép toán:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai20 extends Frame implements ActionListener
{

Label lb,lb1,lb2,lb3;
TextField txta,txtb,txtkq;
Button cong,tru,nhan,chia,exit,reset;
Panel pn,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(1,1));
pn2=new Panel(new GridLayout(3,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn4=new Panel(new FlowLayout(FlowLayout.CENTER));

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);

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 18


THỰC HÀNH LẬP TRÌNH JAVA
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
add(pn);
//setSize(400,300);
this.setBounds(200,200,400,300);
//pack();
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
else if(e.getSource()==exit) System.exit(0);
else
{
double a=Double.parseDouble(txta.getText());
double b=Double.parseDouble(txtb.getText());
if (e.getSource()==cong) txtkq.setText(Double.toString(a+b));
if (e.getSource()==tru) txtkq.setText(Double.toString(a-b));
if (e.getSource()==nhan) txtkq.setText(Double.toString(a*b));
if (e.getSource()==chia) txtkq.setText(Double.toString(a/b));
}
}
public Bai20(String st)
{
super(st);
GUI();
}
public static void main (String[] args) {
new Bai20("Minh hoa cac phep toan");
}
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A


Trang 19


THỰC HÀNH LẬP TRÌNH JAVA
3. In ra các số nguyên tố nhỏ hơn hoặc bằng số n cho trước:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai21 extends Frame implements ActionListener
{
//String chuoi1=new String();
//String chuoi2=new String(" ");
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button tim,reset,exit;
Panel pn,pn1,pn2,pn3;
public void GUI1(){
lb=new Label("Cac so nguyen to <=n ");
lb1=new Label("Nhap n:");
lb2=new Label("KQ:");
txtnhap=new TextField();
txtkq=new TextField();
txtkq.setEditable(false);
tim=new Button("Tim");
exit=new Button("Exit");
reset=new Button("Reset");
tim.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);

pn=new Panel(new GridLayout(3,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(2,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txtnhap);
pn2.add(lb2);
pn2.add(txtkq);
pn3.add(tim);
pn3.add(reset);
pn3.add(exit);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
this.setBounds(200,200,400,300);
this.setVisible(true);
}
public boolean Test(int t)
{
if(t==0||t==1) return false;
else for(int i=2;i<=(t/2);i++) if((t%i)==0) return false ;
return true;
}

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 20



THỰC HÀNH LẬP TRÌNH JAVA
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txtnhap.setText("");
txtkq.setText("");
}
if(e.getSource()==exit) System.exit(0);
if(e.getSource()==tim)
{
int n=Integer.parseInt(txtnhap.getText());
String chuoi1="";
if(n>=2)for(int i=2;i<=n;i++) if(Test(i)) chuoi1+=" "+i;
/* {chuoi1=chuoi1.concat(Integer.toString(i));
chuoi1=chuoi1.concat(chuoi2);
}*/
txtkq.setText(chuoi1);
//chuoi1="";
}
}
public Bai21(String st){
super(st);
GUI1();
}
public static void main (String[] args) {
new Bai21("Tim so nguyen to");
}
}


TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 21


THỰC HÀNH LẬP TRÌNH JAVA
4. Kiểm tra một số có thuộc dãy Fibonaci hay không?
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai22 extends Frame implements ActionListener{
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button ok,reset,exit;
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
public Bai22(String st)
{
super(st);
lb=new Label("Kiem tra a co thuoc day Fibonaci");
lb1=new Label("Nhap a:");
lb2=new Label("KQ: ");
txtnhap=new TextField(20);
txtkq=new TextField(20);
ok=new Button("OK");
reset=new Button("Reset");
exit=new Button("Exit");
ok.addActionListener(this);

reset.addActionListener(this);
exit.addActionListener(this);
//pn=new Panel(new GridBagLayout());
gbs.insets=new Insets(2, 2, 2, 2);
gbs.fill=GridBagConstraints.BOTH;

gbs.gridx=1;
gbs.gridy=1;
gbs.gridwidth=6;
gbs.gridheight=1;
gb.setConstraints(lb, gbs);
gbs.gridx=1;
gbs.gridy=2;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb1, gbs);
gbs.gridx=3;
gbs.gridy=2;
gbs.gridwidth=4;
gbs.gridheight=1;
//gbs.ipadx=4;
gb.setConstraints(txtnhap, gbs);
gbs.gridx=1;
gbs.gridy=3;

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 22



THỰC HÀNH LẬP TRÌNH JAVA
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb2, gbs);
gbs.gridx=3;
gbs.gridy=3;
gbs.gridwidth=4;
gbs.gridheight=1;
gb.setConstraints(txtkq, gbs);
//gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(ok, gbs);
gbs.gridx=3;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(reset, gbs);
gbs.gridx=5;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(exit, gbs);
pn.add(lb);
pn.add(lb1);

pn.add(txtnhap);
pn.add(lb2);
pn.add(txtkq);
pn.add(ok);
pn.add(reset);
pn.add(exit);
pn.setLayout(gb);
this.add(pn);
this.setSize(400, 200);
setVisible(true);
}
public boolean fibo(int n)
{
int x=0,y=1,z=0;
if(n!=0)
{while(z{
x=y;
y=z;
z=x+y;

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 23


THỰC HÀNH LẬP TRÌNH JAVA
}
if(z==n) return true;
}

return false;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit) System.exit(0);
else if(e.getSource()==reset) {
txtnhap.setText("");
txtkq.setText("");
}
else {
int a=Integer.parseInt(txtnhap.getText());
if(fibo(a)) txtkq.setText(a+" la so FIBONACI");
else txtkq.setText(a+" khong la so FIBONACI");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Bai22("Kiem tra fibo");
}
}

5. Mô tả máy tính điện tử cá nhân
package baocao;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Bai23 extends JFrame implements ActionListener{
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
final int MAX_INPUT_LENGTH=20;

final int INPUT_MODE=0;
final int RESULT_MODE=1;
final int ERROR_MODE=2;
int displayMode=INPUT_MODE;
boolean ClearOnNextDigit,percent;
double lastnumber=0;

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 24


THỰC HÀNH LẬP TRÌNH JAVA
String lastoperator="0";

private JTextField txtkq=new JTextField("0",20);
private JButton jbnButtons[];
JPanel jpn=new JPanel();
public Bai23(String s)
{
super(s);
jbnButtons=new JButton[23];
gbc.insets=new Insets(3, 3, 3, 3);
gbc.fill=GridBagConstraints.BOTH;
for(int i=0;i<=9;i++) jbnButtons[i]= new JButton(String.valueOf(i));

jbnButtons[10]=new
jbnButtons[11]=new
jbnButtons[12]=new
jbnButtons[13]=new

jbnButtons[14]=new
jbnButtons[15]=new
jbnButtons[16]=new
jbnButtons[17]=new
jbnButtons[18]=new
jbnButtons[19]=new
jbnButtons[20]=new
jbnButtons[21]=new
jbnButtons[22]=new

JButton("+/-");
JButton(".");
JButton("=");
JButton("/");
JButton("*");
JButton("-");
JButton("+");
JButton("sqrt");
JButton("1/x");
JButton("%");
JButton("Backspace");
JButton("CE");
JButton("C");

gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight=2;
gbc.gridwidth=5;
gb.setConstraints(txtkq, gbc);
gbc.gridx=0;

gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[20], gbc);
gbc.gridx=2;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[21], gbc);
gbc.gridx=4;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[22], gbc);
gbc.gridx=0;
gbc.gridy=3;

TRẦN THANH DUY_LỚP 09T4_NHÓM 11A

Trang 25


×