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;iy) 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
- Xem thêm -