mỘt sỐ bÀi vÍ dỤ java

29
MỘT SỐ BÀI VÍ DỤ JAVA /* * chiendaica; * DTVTk8B; */ /* * tao mang so nguyen mot chieu tạo thread thực hiện tìm max,min * tính trung bình ptu ở vị trí chẵn,lẻ */ package baikiemtra; import java.io.*; import java.lang.*; public class MangMotChieu { static int A[]=new int[50]; static int length; public static void main(String[] args) { System.out.print("Hay nhap so phan tu cua mang: "); SoNguyen x=new SoNguyen(); length=x.nhapso(); for(int i=0;i<length;i++) { System.out.print("A["+i+"]= ") ; A[i]=x.nhapso(); } TBCThread t1=new TBCThread(); t1.start(); MaxThread t2=new MaxThread(); t2.start(); MinThread t3=new MinThread(); t3.start(); TBChanThread t4=new TBChanThread();

Upload: nhungk8b

Post on 22-Oct-2015

24 views

Category:

Documents


1 download

DESCRIPTION

ffdgf

TRANSCRIPT

MỘT SỐ BÀI VÍ DỤ JAVA/* * chiendaica; * DTVTk8B; *//* * tao mang so nguyen mot chieu tạo thread thực hiện tìm max,min * tính trung bình ptu ở vị trí chẵn,lẻ */package baikiemtra;import java.io.*;import java.lang.*;public class MangMotChieu { static int A[]=new int[50]; static int length; public static void main(String[] args) { System.out.print("Hay nhap so phan tu cua mang: "); SoNguyen x=new SoNguyen(); length=x.nhapso(); for(int i=0;i<length;i++) { System.out.print("A["+i+"]= ") ; A[i]=x.nhapso(); } TBCThread t1=new TBCThread(); t1.start(); MaxThread t2=new MaxThread(); t2.start(); MinThread t3=new MinThread(); t3.start(); TBChanThread t4=new TBChanThread(); t4.start(); TBLeThread t5=new TBLeThread(); t5.start(); } }class SoNguyen { int giatri;

int nhapso() { DataInputStream str=new DataInputStream(System.in); try { this.giatri=Integer.parseInt(str.readLine()); } catch(IOException e){this.giatri=0;} return this.giatri; } }class TBCThread extends Thread { public void run() { int s=0; float SUM; for(int i=0;i<MangMotChieu.length;i++) { s=s+MangMotChieu.A[i]; } SUM=(float)(s/MangMotChieu.length); System.out.println("Trung binh cong cac phan tu cua mang la: "+SUM); } }class MaxThread extends Thread { public void run() { int Max=MangMotChieu.A[0]; for(int i=0;i<MangMotChieu.length;i++) { if(Max<MangMotChieu.A[i]) Max=MangMotChieu.A[i]; } System.out.println("Max= "+Max); } }class MinThread extends Thread { public void run() { int Min=MangMotChieu.A[0];

for(int i=0;i<MangMotChieu.length;i++) { if(Min>MangMotChieu.A[i]) Min=MangMotChieu.A[i]; } System.out.println("Min= "+Min); } }class TBChanThread extends Thread { public void run() { int s1=0; int dem1=0; float SUM1; for(int i=0;i<MangMotChieu.length;i+=2) { s1=s1+MangMotChieu.A[i]; dem1++; } SUM1=(float)(s1/dem1); System.out.println("Trung binh cong cac phan tu o vi tri chan la: "+SUM1); } }class TBLeThread extends Thread { public void run() { int s=0; int dem=0; float SUM=0; for(int i=1;i<MangMotChieu.length;i+=2) { s=s+MangMotChieu.A[i]; dem++; } SUM=(float)s/dem; System.out.println("Trung binh cong cac phan tu o vi tri le la: "+SUM); } }************************************************************************

/* * chiendaica;

* DTVT k8B *//* * tạo mảng 2 chiều rồi tạo các luồng tìm max các hàng max các cột và đưa vào một ma trận * tìm max và min của toàn ma tran, * tìm các số nguyên tố trong ma trận * tính trung bình cộng đường chéo. */package baikiemtra;import java.io.*;import java.lang.*;public class MangHaiChieu { static int length; static int[][] A=new int[50][50]; static int[] B=new int[50]; static int[] C=new int [50]; public static void main(String[] args) { Mang x=new Mang(); System.out.print("Chon co ma Tran NxN :"); length=x.nhapso(); for(int i=0;i<length;i++) { for(int j=0;j<length;j++) { System.out.print("\t A["+i+"]["+j+"]= "+" "); A[i][j]=x.nhapso(); } } MaxHang t1=new MaxHang(); t1.start(); MaxCot t2=new MaxCot(); t2.start(); MaxMatrix t3=new MaxMatrix(); t3.start(); MinMatrix t4 = new MinMatrix(); t4.start(); TBCheo t6=new TBCheo(); t6.start(); NguyenTo t5=new NguyenTo(); t5.start();

} }class Mang { int giatri; int nhapso() { try { DataInputStream str=new DataInputStream(System.in); this.giatri=Integer.parseInt(str.readLine()); } catch(IOException e){this.giatri=0;} return this.giatri; } boolean ktngto(int a) { int dem=0; if(a==1) return true; for(int i=2;i<=a/2;i++) { if((a%i)==0) { dem++; } } if(dem==0) return true; else return false; } }

class MaxHang extends Thread { public void run() { int k = 0; System.out.print("MAX CUA CAC HANG :"); for(int i=0;i<MangHaiChieu.length;i++) {

int MAXHANG= MangHaiChieu.A[0][0]; for(int j=0;j<MangHaiChieu.length;j++) { if(MAXHANG<MangHaiChieu.A[i][j]) MAXHANG=MangHaiChieu.A[i][j]; } MangHaiChieu.B[k]=MAXHANG; k++; //System.out.println("MAX cua hang thu "+i+" la: "+MAXHANG); } for(k=0;k<MangHaiChieu.length;k++) {System.out.print(MangHaiChieu.B[k]+" ");} System.out.println(" "); } }class MaxCot extends Thread { public void run() { int k=0; System.out.print("MAX CUA CAC COT : "); for(int j=0;j<MangHaiChieu.length;j++) { int MAXCOT = MangHaiChieu.A[0][0]; for(int i=0;i<MangHaiChieu.length;i++) { if(MAXCOT<MangHaiChieu.A[i][j]) MAXCOT=MangHaiChieu.A[i][j]; } // System.out.println("Max cua cot thu "+j+" la: "+MAXCOT); MangHaiChieu.C[k]=MAXCOT; k++; } for(k=0;k<MangHaiChieu.length;k++) {System.out.print(MangHaiChieu.C[k]+ " ");} System.out.println(" "); } }class MaxMatrix extends Thread { public void run()

{ System.out.print(" MAX CUA MA TRAN DA CHO LA: "); int MAX=MangHaiChieu.A[0][0]; for(int i=0;i<MangHaiChieu.length;i++) { for(int j=0;j<MangHaiChieu.length;j++) { if(MAX<MangHaiChieu.A[i][j]) MAX=MangHaiChieu.A[i][j]; } } System.out.println(MAX); } }class MinMatrix extends Thread { public void run() { int MIN=MangHaiChieu.A[0][0]; for(int i=0;i<MangHaiChieu.length;i++) { for(int j=0;j<MangHaiChieu.length;j++) { if(MIN>MangHaiChieu.A[i][j]) MIN=MangHaiChieu.A[i][j]; } } System.out.println("MIN CUA MANG DA CHO LA: "+MIN); } }class NguyenTo extends Thread { public void run() { Mang ch=new Mang(); System.out.print("NHUNG SO NGUYEN TO TRONG MANG LA: "); for(int i=0;i<MangHaiChieu.length;i++) for(int j=0;j<MangHaiChieu.length;j++) { if(ch.ktngto(MangHaiChieu.A[i][j])) System.out.print(MangHaiChieu.A[i][j]); } System.out.println(" "); }

}class TBCheo extends Thread { public void run() { float TBC; int SUM=0; for(int i=0;i<MangHaiChieu.length;i++) { for(int j=0;j<MangHaiChieu.length;j++) { if(i==j) SUM=SUM+MangHaiChieu.A[i][j]; } } TBC=(float)(SUM/MangHaiChieu.length); System.out.println("TRUNG BINH CONG DUONG CHEO CUA MA TRAN LA: "+TBC); } } ************************************************************************/* * To change this template, choose Tools | Templates * and open the template in the editor. */package baikiemtra;import java.io.*;

/** * * @author chienkute1 */public class SinhVien { public static void main(String[] args) { // LopSinhVien[] SV=new LopSinhVien[200]; //float[] A=new float[50]; // int k=0; LopSinhVien x=new LopSinhVien(); int soluong; System.out.print("MOI NHAP SO LUONG SV: ");

DataInputStream st=new DataInputStream(System.in); try { soluong=Integer.parseInt(st.readLine()); }catch(IOException e){System.out.println("Error "+e); soluong=0;} String[] ten=new String[soluong]; String[] ma=new String[soluong]; float[] diem_1=new float [soluong]; float[] diem_2=new float [soluong]; float[] diem_3=new float [soluong]; float[] diem_tb=new float [soluong]; for(int i=1;i<=soluong;i++) { System.out.println("***********THONG TIN SV THU "+i+"**************"); x.nhap(); ten[i-1]=x.hoten; ma[i-1]=x.masv; diem_1[i-1]=x.diem1; diem_2[i-1]=x.diem2; diem_3[i-1]=x.diem3; diem_tb[i-1]=x.diemtb(); System.out.println(" "); System.out.println(" "); } System.out.println("*************DANH SACH HOC SINH GIOI***************"); for(int i=0;i<soluong;i++) { if(diem_tb[i]>=8.0) { System.out.println("THONG TIN SINH VIEN THU "+(i+1)+" "); //System.out.println("HO VA TEN: "+ten[i]); System.out.println("MA SINH VIEN: "+ma[i]); System.out.println("DIEM 1: "+diem_1[i]); System.out.println("DIEM_2: "+diem_2[i]); System.out.println("DIEM 3: "+diem_3[i]); System.out.println("DIEM TRUNG BINH: "+diem_tb[i]); } } System.out.print("NHAP HO TEN SINH VIEN CAN TIM: ");

String f = null; try { f=st.readLine(); } catch(IOException e){System.out.println("Error "+e);} for(int i=0;i<soluong;i++) { if(f.equals(ten[i])) { System.out.println("THONG TIN SINH VIEN THU "+(i+1)+" "); //System.out.println("HO VA TEN: "+ten[i]); System.out.println("MA SINH VIEN: "+ma[i]); System.out.println("DIEM 1: "+diem_1[i]); System.out.println("DIEM_2: "+diem_2[i]); System.out.println("DIEM 3: "+diem_3[i]); System.out.println("DIEM TRUNG BINH: "+diem_tb[i]); } } } }class LopSinhVien { DataInputStream str=new DataInputStream(System.in); // InputStreamReader isr=new InputStreamReader(System.in); String hoten; String masv; float diem1,diem2,diem3; public void nhap() { try { System.out.print("MOI NHAP HO VA TEN: "); this.hoten=str.readLine(); System.out.print("MOI NHAP VAO MA SV: "); this.masv=str.readLine(); }catch(Exception e){System.out.println("Error" +e);} try { System.out.print("MOI NHAP VAO DIEM 1: "); this.diem1=Float.parseFloat(str.readLine()); System.out.print("MOI NHAP VAO DIEM 2: ");

this.diem2=Float.parseFloat(str.readLine()); System.out.print("MOI NHAP VAO DIEM 3: "); this.diem3=Float.parseFloat(str.readLine()); }catch(Exception e){System.out.println("Error "+e);} } float diemtb() { float DTB; DTB=(float)(this.diem1+this.diem2+this.diem3)/3; return DTB; } }***********************************************************************/* * To change this template, choose Tools | Templates * and open the template in the editor. * Viết chương trình nhập vào 1 xâu kí tự; viết phương thức timA() cho phép đếm số kí tự * A trong xâu; phương thức chuẩn hóa để chuẩn hóa xâu( xâu đã chuẩn hóa gồm các từ chỉ cách nhau 1 dấu cách * và các chữ cái đầu mỗi từ viết hoa */package baikiemtra;import java.io.*;

/** * * @author chienkute */public class XauKiTu { public static void main(String[] args) { String str; String xauchuan; int sokitu=0; xau x=new xau(); System.out.print("MOI NHAP VAO 1 XAU: "); str= x.nhap(); sokitu=x.demkitu('A'); System.out.println("SO KI TU A TRONG XAU LA: "+sokitu); //xauchuan=x.chuanhoa(str);

System.out.println("XAU SAU KHI CHUAN HOA: "+x.chuanhoa(str));; } }class xau { String xau; String nhap() { DataInputStream st=new DataInputStream(System.in); try { xau=st.readLine(); }catch(IOException e){} return xau; } int demkitu(char c) { int dem=0; for(int i=0;i<xau.length();i++) { if(xau.charAt(i)==c) dem++; } return dem; } String chuanhoa(String xau) { xau=xau.trim();//cat khoang trong o dau va cuoi xau //loai bo nhieu dau cach giua 2 tu chi giu lai 1 int i=0; while(i<xau.length()) { if(xau.charAt(i)==' ' && xau.charAt(i+1)== ' ') xau=xau.substring(0,i)+xau.substring(i+1);//substring(int i) cat xau tu vi tri i dem het; substring(0,i) cat tu vi tri i 0 de else i++;} //doi chu cai dau moi chu sang chu hoa xau=xau.substring(0,1).toUpperCase()+xau.substring(1); System.out.print(xau); String xau1; for(i=1;i<xau.length();i++) { if(xau.charAt(i)==' '&& xau.charAt(i+1)!= ' '){

xau=xau.substring(0,i+1)+xau.substring(i+2,1).toUpperCase()+xau.substring(i+3); // xau=xau1; } //return xau; } return xau;} } ***********************************************************************/* xây dựng lớp khách hàng:maxkhach, ho,ten.dịa chỉ,số tiền gốc,số tiền lãi/thang/1trieeu,số tháng gưi * nhập và in danh sách khách hàng * in ra danh sách khách hàng và tổng tiền lãi * in ra danh sách khách hàng sắp xếp theo thứ tự giảm dần tiền lãi */package baitap_string;import java.io.*;

/** * * @author chienkute */class Chung1

{ String xau; String nhapxau() { DataInputStream is=new DataInputStream(System.in); try { this.xau=is.readLine(); } catch(IOException e){} return this.xau; } float nhapso() { float s=0;

try { s=Float.valueOf(nhapxau()).floatValue(); } catch(NumberFormatException e){} return s; } }class KhachHang { String makh,ho,ten,diachi,ngaysinh; float goc,lai,month,tong; KhachHang(String makh,String ho,String ten,String diachi,String ngaysinh,float goc,float lai,float month,float tong) { this.makh=makh; this.ho=ho; this.ten=ten; this.diachi=diachi; this.ngaysinh=ngaysinh; this.goc=goc; this.lai=lai; this.month=month; } void nhap() { Chung1 x=new Chung1(); System.out.print("NHAP MA KHACH HANG: "); makh=x.nhapxau(); System.out.print("NHAP HO: "); ho=x.nhapxau(); System.out.print("NHAP TEN: "); ten=x.nhapxau(); System.out.print("NHAP DIA CHI: "); diachi=x.nhapxau(); System.out.print("NHAP NGAY SINH: "); ngaysinh=x.nhapxau(); System.out.print("NHAP TIEN GOC : "); goc=x.nhapso(); System.out.print("NHAP TIEN LAI/THANG/1TRIEU: "); lai=x.nhapso(); System.out.print("NHAP SO THANG GUI: "); month=x.nhapso();

} void in() { System.out.println(makh+" "+ho+" "+ten+" "+diachi+" "+ngaysinh+" "+goc+" "+lai+" "+month+" "+tong); } float tong() { tong=(float)((goc/1000000)*lai*month); return tong; } }public class KhachHangMain { public static void main(String[] args) { KhachHang[] m=new KhachHang[50]; Chung1 n=new Chung1(); int a = 0; System.out.print("MOI NHAP SO KHACH HANG: "); try { a=Integer.valueOf(n.nhapxau()).intValue(); } catch(NumberFormatException e){} for(int i=0;i<a;i++) { m[i]=new KhachHang(" "," "," "," "," ",0,0,0,0); m[i].nhap(); m[i].tong(); } System.out.println("***********DANH SACH KHACH HANG**************"); System.out.println("MAKHACHHANG HO TEN DIACHI NGAYSINH GOC LAI MONTH TONG"); for(int i=0;i<a;i++) { m[i].in(); } //sap xep theo thu tu tong tien lai giam dan KhachHang[] g=new KhachHang[50]; g[1]=new KhachHang(" "," "," "," "," ",0,0,0,0);

System.out.println("********DANH SACH THEO THU TU TONG LAI GIAM DAN*********"); System.out.println("MAKHACHHANG HO TEN DIACHI NGAYSINH GOC LAI MONTH TONG"); for(int i=0;i<a-1;i++) for(int j=i+1;j<a;j++) { if(m[i].tong()<m[j].tong()) { g[1]=m[i]; m[i]=m[j]; m[j]=g[1]; } } for(int i=0;i<a;i++) { m[i].in(); } } }************************************************************************/* * nhap vao mang nxn tim so nguyen to lon nhat và nhỏ nhất cua mang * nếu không có nguyên tố thì max=min=-1 */package baitap_string;import java.io.*;

/** * * @author chienkute */class Chung4 { int nhapso() { int s=0; DataInputStream is=new DataInputStream(System.in); try { s=Integer.parseInt(is.readLine()); }

catch(IOException e){} return s; } boolean ktngto(int a) { int dem=0; if(a==1) return true; for(int i=2;i<=a/2;i++) { if((a%i)==0) { dem++; } } if(dem==0) return true; else return false; } } public class Bai5 { public static void main(String[] args) { Chung4 c=new Chung4(); System.out.println("Nhap ma tran co nxm: "); System.out.print("nhap n: "); int n=c.nhapso(); System.out.print("nhap m: "); int m=c.nhapso(); int[][] A=new int[n][m]; int z=0; int max=-1; int min=-1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { System.out.print("A["+i+"]["+j+"]= "); A[i][j]=c.nhapso(); if(c.ktngto(A[i][j])) { z=1; if(min==-1) min=A[i][j];

if(min>A[i][j]) min=A[i][j]; if(max<A[i][j]) max=A[i][j]; } } if(z==1) { System.out.println("so nguyen to lon nhat trong mang la:"+max); System.out.println("so nguyen to nho nhat trong mang la: "+min); } if(z==0) { System.out.println("so nguyen to lon nhat trong mang la:-1"); System.out.println("so nguyen to nho nhat trong mang la:-1"); } } }*******************************************************************/* * cho mang mot chieu * tim max va gia tri max */package baitapmang;

/** * * @author chienkute */public class Bai2 {public static void main(String[] args) {int [] a ={3,1,17,0,10};int N=5,k,max,stt=0;max=a[0];for (k=0;k<N;k++)if (max < a[k]){max=a[k];stt=k;}System.out.println("gia tri lon nhat cua day la:"+max);System.out.print("vi tri cua so co gia tri max la:");//for (k=0;k<N;k++)//if(max==a[k])System.out.print(stt +" ");}

}*******************************************************************/* * viet chuong trinh nhap vao mot mang * va in ra mang theo thu tu giam dan */package baitapmang;import java.io.*;

/** * * @author chienkute */public class bai1 { static int[] A=new int[100]; static int n,m; public static void main(String[] args) { System.out.print("Moi nhap vao so phan tu cua mang: "); Mang x=new Mang(); n=x.nhap(); for(int i=0;i<n;i++) { System.out.print("A["+i+"]= "); A[i]=x.nhap(); } for(int i=0;i<(n-1);i++) for(int j=(i+1);j<n;j++) { if(A[i]<A[j]) { m=A[j]; A[j]=A[i]; A[i]=m; } } System.out.print("Mang sap xep theo thu tu giam dan la: "); for(int i=0;i<n;i++) {System.out.print(A[i]+" ");} System.out.println(" "); } }

class Mang { int giatri; int nhap() { DataInputStream is=new DataInputStream(System.in); try { this.giatri=Integer.parseInt(is.readLine()); } catch(IOException e){System.out.println("Error "+e);} return this.giatri; } }********************************************************************

/* viết chương trình gửi 1 xâu kí tự từ client tới server * khi server nhận dc thì gửi xâu "da nhan duoc" tới client. */Chương trình clientpackage Bai1;import java.io.*;import java.net.*;

/** * * @author chienkute */public class Client { public static void main(String[] args) { try { Socket s=new Socket("127.0.0.1",12345); InputStream in=s.getInputStream(); OutputStream out=s.getOutputStream(); System.out.print("your text: "); Chung c=new Chung(); String st=c.nhap(); byte[] b=new byte[50];//khai bao mot mang byte

b=st.getBytes();//chuyen 1 xau thanh mang byte out.write(b); byte[] a=new byte[50]; in.read(a); String nhan=new String(a);//chuyen byte thanh xau System.out.print(nhan); } catch(IOException e){} } }class Chung { String giatri; String nhap() { DataInputStream is=new DataInputStream(System.in); try { this.giatri=is.readLine(); } catch(IOException e){} return giatri; } }Chương trình Serverpackage Bai1;import java.io.*;import java.net.*;

/** * * @author chienkute */public class Server { public static void main(String[] args) { while(true) { try { ServerSocket ss=new ServerSocket(12345);

Socket s=ss.accept(); Nhan t=new Nhan(s); t.start(); } catch(IOException e){} } } }class Nhan extends Thread { Socket c; Nhan(Socket s) { this.c=s; } public void run() { try { OutputStream out=c.getOutputStream(); InputStream in=c.getInputStream(); byte[] b=new byte[50]; byte[] a=new byte[50]; in.read(b); String st2=new String(b); System.out.println(st2); } catch(IOException e){} } }**************************************************************Chúc mọi người ôn thi tốt! ai thắc mắc có thể mail cho mình [email protected] nhé