intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài thực hành ngôn ngữ lập trình Java

Chia sẻ: Huỳnh Thị Thùy Dương | Ngày: | Loại File: DOCX | Số trang:56

173
lượt xem
69
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Các kiến thức cơ bản, cấu trúc chương trình Java, lập trình AWT-SWING, lập trình Applets là những nội dung chính trong "Bài thực hành ngôn ngữ lập trình Java". Mời các bạn cùng tham khảo để có thêm tài liệu phục vụ nhu cầu học tập và nghiên cứu.

Chủ đề:
Lưu

Nội dung Text: Bài thực hành ngôn ngữ lập trình Java

  1. THỰC HÀNH LẬP TRÌNH JAVA BÀI THỰC HÀNH NGÔN NGỮ LẬP TRÌNH JAVA CÁC KIẾN THỨC CƠ BẢN­CẤU TRÚC CHƯƠNG TRÌNH  JAVA Cấu trúc lựa chọn: 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
  2. THỰC HÀNH LẬP TRÌNH JAVA 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
  3. THỰC HÀNH LẬP TRÌNH JAVA } 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
  4. THỰC HÀNH LẬP TRÌNH JAVA 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 && b0 && 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()); } } TRẦN THANH DUY_LỚP 09T4_NHÓM 11A Trang 4
  5. THỰC HÀNH LẬP TRÌNH JAVA 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() { } public boolean namnhuan(int nam) { if((nam%4==0 && nam%100!=0)||(nam%400==0)) return true; else return false; } public void nhap() { 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; } } TRẦN THANH DUY_LỚP 09T4_NHÓM 11A Trang 5
  6. THỰC HÀNH LẬP TRÌNH JAVA public static void main (String[] args) { Bai5 temp= new Bai5(); temp.nhap(); temp.inra(); } } Cấu trúc lặp 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
  7. THỰC HÀNH LẬP TRÌNH JAVA 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); } public void tinh() { float s=15.0f; for(int i=1;i
  8. THỰC HÀNH LẬP TRÌNH JAVA 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
  9. THỰC HÀNH LẬP TRÌNH JAVA 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
  10. THỰC HÀNH LẬP TRÌNH JAVA { 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(); } } 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(); } public boolean check() { int i=2; if (n == 0 || n == 1) return false; while (i
  11. THỰC HÀNH LẬP TRÌNH JAVA if (a.check()) System.out.println(n + " la so nguyen to"); else System.out.println(a.get() + " ko phai la so nguyen to"); } } 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
  12. THỰC HÀNH LẬP TRÌNH JAVA 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); } } 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; TRẦN THANH DUY_LỚP 09T4_NHÓM 11A Trang 12
  13. THỰC HÀNH LẬP TRÌNH JAVA else for(int i=2;i
  14. THỰC HÀNH LẬP TRÌNH JAVA 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
  15. THỰC HÀNH LẬP TRÌNH JAVA 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
  16. THỰC HÀNH LẬP TRÌNH JAVA 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 16
  17. THỰC HÀNH LẬP TRÌNH JAVA LẬP TRÌNH AWT­SWING 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) { TRẦN THANH DUY_LỚP 09T4_NHÓM 11A Trang 17
  18. THỰC HÀNH LẬP TRÌNH JAVA if(e.getSource()==kq) { 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 18
  19. THỰC HÀNH LẬP TRÌNH JAVA 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); pn.add(pn1); TRẦN THANH DUY_LỚP 09T4_NHÓM 11A Trang 19
  20. THỰC HÀNH LẬP TRÌNH JAVA 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 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2