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");
}
}
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 void tg(){
float tg;
if(((a
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)
{
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
}
}
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
}
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)
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
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
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;i
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
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;
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");
}
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));
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
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
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);
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);
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;
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;