Professional Documents
Culture Documents
Bai dau tien: public class abc { public abc() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } }
public static void main(String[] args) { nguoi b = new nguoi("ngjuyen van a",30); sinhvien c = new sinhvien("nguyen van c",20,"002"); System.out.println(c.laytuoi()); System.out.println(c.layma()); } private void jbInit() throws Exception { } } public class nguoi { protected String hoten; protected int tuoi; int laytuoi() { return tuoi; } String layten() { return hoten; } public nguoi(String hoten, int tuoi) { this.hoten=hoten; this.tuoi=tuoi; } } public class sinhvien extends nguoi{ protected String MASV; String layma() { return MASV; }
Page 1
///////////////////////////////////// Ke thua tu JFrane: public class frame { public static void main(String[] args) { myframe h=new myframe(); } } import java.awt.*; Khnh Chi Email : khanhchi0285@gmail.com Page 2
Page 8
Page 11
Page 13
4)so_synchronized
public class Data { private boolean key=false; public synchronized void xuat(int i) { if(i%2==0) { while(key==false) //In so le { try { wait(); }catch(Exception e) { } } key=false; } else { while(key==true) //In so chan { try { wait(); }catch(Exception e) { } } key=true; } System.out.println( i + " , "); notifyAll(); } }
public class main { public static void main(String[] args) { data d= new data(); sochan t1=new sochan(d); sole t2=new sole(d); t1.start(); t2.start(); } }
Page 15
Page 19
*********************GIAODIENCLIENT.JAVA*****************
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Giaodienclient extends JFrame implements ActionListener { JTextField ta,tb,tc,tds; JLabel la,lb,lc,lds; JButton tinh; public Giaodienclient() { super("hjhjg"); Container ct=getContentPane(); ct.setLayout(new GridLayout(5,2)); la=new JLabel("a = "); ct.add(la); ta=new JTextField(10); ct.add(ta); lb=new JLabel("b = "); ct.add(lb); tb=new JTextField(10); ct.add(tb); lc=new JLabel("c = "); ct.add(lc); tc=new JTextField(10);
Page 20
*************************SERVER.JAVA*****************************
import java.io.*; import java.net.*; public class server { public static void main(String[] args)throws IOException { ServerSocket ss=new ServerSocket(5000); Socket cs=ss.accept(); PrintWriter out=new PrintWriter(cs.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream())); //System.out.println(in.readLine()); while(true) { String a=in.readLine();
Page 21
Page 22
8)TONG HAI SO
********************MAIN.JAVA******************
package Tong2So; public class Main { public static void main(String[] args) { // TODO code application logic here
Page 23
Page 24
6)TIMER_TIMERTASK
//bai toan cong n so voi thoi gian cho truoc ***********MAIN.JAVA*******************
package Timer_TimerTask; import java.util.*; public class Main { public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); System.out.print("Nhap so bai can lam: "); int sobai=sc.nextInt(); Timer t1=new Timer(); t1.schedule(new PhepCong(sobai), 0); Timer t2=new Timer(); t2.schedule(new ThongBao(),sobai*1000,2*1000); } }
**************************PHEPCONG.JAVA***************************
package Timer_TimerTask; import java.io.*; import java.util.*; public class PhepCong extends TimerTask { int sobai,kq; BufferedReader stdin; public PhepCong(int n) { sobai=n; stdin=new BufferedReader(new InputStreamReader(System.in)); } public void run() {
Page 25
*********************THONGBAO.JAVA********************
package Timer_TimerTask; import java.util.*; import java.awt.Toolkit; //import java.io.*; public class ThongBao extends TimerTask { int num=3; Toolkit tk; public ThongBao() { tk=Toolkit.getDefaultToolkit(); } public void run() { if(num>0) { tk.beep(); num --; } else { System.out.println("Het gio !!"); System.exit(0); } } }
Page 26