You are on page 1of 26

Thc hnh java

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; }

Khnh Chi Email : khanhchi0285@gmail.com

Page 1

Thc hnh java


public sinhvien(String hoten,int tuoi, String MASV) { super(hoten,tuoi); this.MASV=MASV; this.hoten=hoten; this.tuoi=tuoi; } } /////////////////////////////////////////////////

1)CUU CHUONG 2-9


import java.awt.event.*; import java.awt.*; import javax.swing.*; public class CuuChuong2_9 extends JApplet { // JLabel jl=new JLabel("Nhap so : "); //Khai bao nhan cua JLabel // JTextField jt=new JTextField(10); //Khai bao kich thuoc JTextField la 10 JTextArea ja=new JTextArea(); //Khai bao JTextArea public void init() { this.getContentPane().setLayout(new FlowLayout()); // this.add(jl); // add Label vao giao dien // this.add(jt); //add TextField vao giao dien // jt.addActionListener(this); //Lay su kien sau khi nhap 1 so bat ky vao TextField this.add(ja); // add Label vao giao dien this.setSize(640, 400); this.setVisible(true); for(int i=2;i<=9;i++) { for(int j=1;j<=10;j++) { ja.append( i + "*" + j + "=" + i*j); ja.append("\t"); } ja.append("\n\n"); } } }

///////////////////////////////////// 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

Thc hnh java


import javax.swing.*; public class myframe extends JFrame{ JLabel nhan; JTextField tx; JButton nut; public myframe() { super("day la frame"); Container ct=getContentPane(); ct.setLayout(new FlowLayout()); nhan=new JLabel("nhap ten"); tx=new JTextField(10); nut=new JButton("Cong"); ct.add(nhan); ct.add(tx); ct.add(nut); setSize(200,250); setVisible(true); } } /////////////////////////////////////////////////////////////////////// import java.awt.*; import javax.swing.*; import java.awt.event.*; public class khungframe extends JFrame implements ActionListener { JLabel nhan1; JLabel nhan2; JLabel nhan3; JTextField tx1; JTextField tx2; JTextField tx3; JButton nut1; JButton nut2; public khungframe() { super("day la frame"); Container ct=getContentPane(); ct.setLayout(new FlowLayout()); nhan1=new JLabel("nhap so 1"); tx1=new JTextField(10); nhan2=new JLabel("nhap so 2"); tx2=new JTextField(10); nhan3=new JLabel("Tong"); tx3=new JTextField(10); Khnh Chi Email : khanhchi0285@gmail.com Page 3

Thc hnh java


nut1=new JButton("Add"); nut2=new JButton("Reset"); ct.add(nhan1); ct.add(tx1); ct.add(nhan2); ct.add(tx2); ct.add(nhan3); ct.add(tx3); ct.add(nut1); nut1.addActionListener(this);//xu li su kien cho nut 1 ct.add(nut2); nut2.addActionListener(this);//xu li su kien cho nut 2 setSize(200,250); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == nut1) { int so1, so2, tong; so1 = Integer.parseInt(tx1.getText()); so2 = Integer.parseInt(tx2.getText()); tong = so1 + so2; tx3.setText(String.valueOf(tong)); } if(e.getSource()==nut2) { //xu ly su kien cho nut reset tx1.setText(""); tx2.setText(""); tx3.setText(""); } } } ////////////////////////////////// public class khungmau { public khungmau() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } Khnh Chi Email : khanhchi0285@gmail.com Page 4

Thc hnh java


} private void jbInit() throws Exception { } } public class mainframe { public static void main(String[] args) { khungframe h = new khungframe(); } } /////////////////////////////// Lap trinh TCP. Giai phuongt rinh bac 1 nhap tu ban phim import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class client { public static void main(String[] args) throws IOException { Socket cs=new Socket("localhost",1234); PrintWriter Out= new PrintWriter(cs.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream())); String a1; String b1; Scanner sc =new Scanner(System.in); a1=sc.nextLine(); b1=sc.nextLine(); sc.nextLine(); sc.netInt(); sc.netFloat(); in.close(); Out.close(); cs.close(); } } import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.applet.Applet; Khnh Chi Email : khanhchi0285@gmail.com Page 5

Thc hnh java


import java.awt.event.*; public class server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(1234); Socket cs = ss.accept(); PrintWriter out = new PrintWriter(cs.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(cs. getInputStream())); while (true) { String a = in.readLine(); String b = in.readLine(); float a1, b1; a1 = Float.parseFloat(a); b1 = Float.parseFloat(b); String kq=" "; if (a1 == 0 && b1 != 0) kq = (" phuong trinh vo nghiem"); if (a1 == 0 && b1 == 0) kq = ("phuong trinh vo so nghiem"); if (a1 != 0) kq = ("nghiem cua phuong trinh x= " + (b1 / a1)); out.println(kq); //System.out.println(kq); in.close(); out.close(); cs.close(); ss.close(); break; } } } ////////////////////////////////////// Giai Phuong trinh bac2 bang Applet import java.awt.*; import javax.swing.*; import java.applet.Applet; import java.awt.event.*; import java.lang.*; public class gpt extends JApplet implements ActionListener { JLabel ja = new JLabel("A= "); JLabel jb = new JLabel("B= "); JLabel jc = new JLabel("C= "); JLabel jds = new JLabel("DS= "); Khnh Chi Email : khanhchi0285@gmail.com Page 6

Thc hnh java


JTextField jta = new JTextField(); JTextField jtb = new JTextField(); JTextField jtc = new JTextField(); JTextField jtds = new JTextField(); JButton jbOK = new JButton("OK"); JButton jbcl = new JButton("Clear"); public void init() { Container ct = getContentPane(); ct.setLayout(new GridLayout(5,2)); ct.add(ja); ct.add(jta); ct.add(jb); ct.add(jtb); ct.add(jc); ct.add(jtc); ct.add(jds); ct.add(jtds); ct.add(jbOK); jbOK.addActionListener(this); ct.add(jbcl); jbcl.addActionListener(this); jta.addActionListener(this); jtb.addActionListener(this); jtc.addActionListener(this); jtds.setEditable(false); this.setSize(200,300); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == jbOK) { float a=0, b=0, c=0, delta=0; try { a = Float.parseFloat(jta.getText()); b = Float.parseFloat(jtb.getText()); c = Float.parseFloat(jtc.getText()); } catch (Exception f) {} delta = (b*b)-(4*a*c); if (delta < 0) jtds.setText("vo nghiem"); Khnh Chi Email : khanhchi0285@gmail.com Page 7

Thc hnh java


else if (delta == 0) jtds.setText("" + -b / 2 * a); else jtds.setText("x1= " + (-b + Math.sqrt(delta)) /( 2 * a )+ "x2= " +(-b - Math.sqrt(delta)) /( 2 * a )); } if (e.getSource() == jbcl) { jta.setText(" "); jtb.setText(" "); jtc.setText(" "); jtds.setText(" "); } if (e.getSource() == jta){ float a=0, b=0, c=0, delta=0; try { a = Float.parseFloat(jta.getText()); b = Float.parseFloat(jtb.getText()); c = Float.parseFloat(jtc.getText()); } catch (Exception f) {} delta = (b*b)-(4*a*c); if (delta < 0) jtds.setText("vo nghiem"); else if (delta == 0) jtds.setText("" + -b / 2 * a); else jtds.setText("x1= " + (-b + Math.sqrt(delta)) /( 2 * a )+ "x2= " +(-b - Math.sqrt(delta)) /( 2 * a )); } if (e.getSource() == jtb){ float a=0, b=0, c=0, delta=0; try { a = Float.parseFloat(jta.getText()); b = Float.parseFloat(jtb.getText()); c = Float.parseFloat(jtc.getText()); } catch (Exception f) {} delta = (b*b)-(4*a*c); if (delta < 0) jtds.setText("vo nghiem"); else if (delta == 0) jtds.setText("" + -b / 2 * a); else jtds.setText("x1= " + (-b + Math.sqrt(delta)) /( 2 * a )+ "x2= " +(-b - Math.sqrt(delta)) /( 2 * a )); }

Khnh Chi Email : khanhchi0285@gmail.com

Page 8

Thc hnh java


if (e.getSource() == jtc){ float a=0, b=0, c=0, delta=0; try { a = Float.parseFloat(jta.getText()); b = Float.parseFloat(jtb.getText()); c = Float.parseFloat(jtc.getText()); } catch (Exception f) {} delta = (b*b)-(4*a*c); if (delta < 0) jtds.setText("vo nghiem"); else if (delta == 0) jtds.setText("" + -b / 2 * a); else jtds.setText("x1= " + (-b + Math.sqrt(delta)) /( 2 * a )+ "x2= " +(-b - Math.sqrt(delta)) /( 2 * a )); } } } ///////////////////////// Giai Phuong trinh bac nhat dung TCP truc tiep: // File Client.java import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.*; public class client { public static void main(String[] args) throws IOException { Socket cs=new Socket("localhost",1234); PrintWriter Out= new PrintWriter(cs.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream())); String a1="5"; String b1="4"; Out.println(a1); Out.println(b1); String kq=in.readLine(); System.out.println(kq); in.close(); Out.close(); cs.close(); } } Khnh Chi Email : khanhchi0285@gmail.com Page 9

Thc hnh java


//File server.java import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.applet.Applet; import java.awt.event.*; public class server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(1234); Socket cs = ss.accept(); PrintWriter out = new PrintWriter(cs.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(cs. getInputStream())); while (true) { String a = in.readLine(); String b = in.readLine(); float a1, b1; a1 = Float.parseFloat(a); b1 = Float.parseFloat(b); String kq=" "; if (a1 == 0 && b1 != 0) kq = (" phuong trinh vo nghiem"); if (a1 == 0 && b1 == 0) kq = ("phuong trinh vo so nghiem"); if (a1 != 0) kq = ("nghiem cua phuong trinh x= " + (b1 / a1)); out.println(kq); //System.out.println(kq); in.close(); out.close(); cs.close(); ss.close(); break; } } }

//////////////////////////// Khnh Chi Email : khanhchi0285@gmail.com Page 10

Thc hnh java


Client Gui 2 so di, server tra 2 so ve dung UDP // File client.java import java.*; import java.net.*; import java.io.*; public class client { public client() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) throws IOException { DatagramSocket ds=new DatagramSocket(); byte[] b1=new byte[256]; byte[] b2=new byte[256]; String st1="3"; String st2="5"; b1=st1.getBytes(); b2=st2.getBytes(); InetAddress ad=InetAddress.getByName("localhost"); DatagramPacket dp1= new DatagramPacket(b1,b1.length,ad,1234); DatagramPacket dp2=new DatagramPacket(b2,b2.length,ad,1234); ds.send(dp1); ds.send(dp2); dp1=new DatagramPacket(b1,b1.length); ds.receive(dp1); String tong=new String(dp1.getData()).trim(); System.out.println(tong); ds.close(); } private void jbInit() throws Exception { } }

Khnh Chi Email : khanhchi0285@gmail.com

Page 11

Thc hnh java


// File server.java import java.*; import java.net.*; import java.io.*; import javax.swing.*; public class sever { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(1234); while (true) { byte[] buf1 = new byte[256]; DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length); ds.receive(dp1); String re = new String(dp1.getData()).trim(); int a = Integer.parseInt(re); byte[] buf2 = new byte[256]; DatagramPacket dp2 = new DatagramPacket(buf2, buf2.length); ds.receive(dp2); String re2 = new String(dp2.getData()).trim() ; int b = Integer.parseInt(re2); String kq = String.valueOf(a + b); buf1 = kq.getBytes(); InetAddress adr = dp1.getAddress(); int inport = dp1.getPort(); dp1 = new DatagramPacket(buf1, buf1.length, adr, inport); ds.send(dp1); ds.close(); } } }

.///////////////////////////// Khnh Chi Email : khanhchi0285@gmail.com Page 12

Thc hnh java Thread : In so chan le:


// File data.java import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class data { public boolean key = false; public synchronized void xuat(int i) { if (i%2 == 0) { while (key == false) { try { wait(); } catch (Exception e) {} } key = false; } else { while(key==true) { try { wait(); } catch(Exception e){} } key=true; } System.out.println(i+","); notifyAll(); } }

Khnh Chi Email : khanhchi0285@gmail.com

Page 13

Thc hnh java


// File sochan.java import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class sochan extends Thread { private data d; public sochan(data d) { this.d = d; } public void run() { for(int i=2;i<=10;i+=2) d.xuat(i); } } // File sole.java import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class sole extends Thread { private data d; public sole(data d) { this.d=d; } public void run() { for(int i=1;i<=9;i+=2) { d.xuat(i); } } }

/////////////////////////// Khnh Chi Email : khanhchi0285@gmail.com Page 14

Thc hnh java

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(); } }

Khnh Chi Email : khanhchi0285@gmail.com

Page 15

Thc hnh java


//Vd: Vit chng trnh in s t 110. Nhng c 2 tiu trnh, 1 tiu trnh in s chn, 1 tiu trnh in s l. // File sochan.java import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class sochan extends Thread { public void run() { for(int i=2;i<=10;i+=2) System.out.println(i+","); yield(); } } // File sole.java import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class sole extends Thread { public void run() { for(int i=1;i<=9;i+=2) { System.out.println(i + ","); yield(); } } } // File sole.java public class main { public static void main(String[] args) { sochan t1=new sochan(); sole t2=new sole(); t1.start(); t2.start(); Khnh Chi Email : khanhchi0285@gmail.com Page 16

Thc hnh java


} } ///////////////////////////////////////////////////////////////////////////////////////////// Chng trnh mu ca Client v Server. (Dng giao thc TCP). //Client import java.io.*; import java.net.*; public class client { public static void main(String[] args) throws IOException { Socket cs=new Socket("localhost",1234); PrintWriter Out= new PrintWriter(cs.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream())); String str="Hello word"; Out.println(str); System.out.println(in.readLine()); in.close(); Out.close(); cs.close(); } } //Server import java.io.*; import java.net.*; public class server { public static void main(String[] args) throws IOException { ServerSocket ss=new ServerSocket(1234); Socket cs= ss.accept(); PrintWriter out =new PrintWriter(cs.getOutputStream(),true); BufferedReader in =new BufferedReader(new InputStreamReader(cs.getInputStream())); System.out.println(in.readLine()); String str="chao client"; out.println(str); in.close(); out.close(); cs.close(); ss.close(); Khnh Chi Email : khanhchi0285@gmail.com Page 17

Thc hnh java


} } /////////////////////////////////////////////////////////////////// //Cc v d c bn. Vd: Vit chng trnh Hello. //Client import java.io.*; import java.net.*; public class client { public static void main(String[] args) throws IOException { Socket cs=new Socket("localhost",1234); PrintWriter Out= new PrintWriter(cs.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream())); String str="Hello word"; Out.println(str); System.out.println(in.readLine()); in.close(); Out.close(); cs.close(); } } //Server import java.io.*; import java.net.*; public class server { public static void main(String[] args) throws IOException { ServerSocket ss=new ServerSocket(1234); Socket cs= ss.accept(); PrintWriter out =new PrintWriter(cs.getOutputStream(),true); BufferedReader in =new BufferedReader(new InputStreamReader(cs.getInputStream())); System.out.println(in.readLine()); String str="chao client"; out.println(str); in.close(); out.close(); cs.close(); ss.close(); } } Khnh Chi Email : khanhchi0285@gmail.com Page 18

Thc hnh java


./////////////////////////////////////////////////////// Hello1 //Client import java.io.*; import java.net.*; public class client { public static void main(String[] args) throws IOException { Socket cs=new Socket("localhost",1234); PrintWriter Out= new PrintWriter(cs.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream())); String str="Hello word"; Out.println(str); in.close(); Out.close(); cs.close(); } } //Server import java.io.*; import java.net.*; public class server { public static void main(String[] args) throws IOException { ServerSocket ss=new ServerSocket(1234); Socket cs= ss.accept(); PrintWriter out =new PrintWriter(cs.getOutputStream(),true); BufferedReader in =new BufferedReader(new InputStreamReader(cs.getInputStream())); System.out.println(in.readLine()); in.close(); out.close(); cs.close(); ss.close(); } }

Khnh Chi Email : khanhchi0285@gmail.com

Page 19

Thc hnh java


/////////////////////////////////////////////////////////////////////////////////// Giai Phuong trin bac 2 bang TCP

5)GIAI PHUONG TRINH BAC HAI


**********************CLIENT.JAVA*****************
import java.io.*; import java.net.*; public class client { public static void main(String[] args)throws IOException { Socket cs=new Socket("localhost",5000); PrintWriter out=new PrintWriter(cs.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream())); String a="4"; String b="5"; String c="1"; out.println(a); out.println(b); out.println(c); String kq=in.readLine(); System.out.println(kq); in.close();out.close();cs.close(); } }

*********************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);

Khnh Chi Email : khanhchi0285@gmail.com

Page 20

Thc hnh java


ct.add(tc); lds=new JLabel("DS = "); ct.add(lds); tds=new JTextField(10); ct.add(tds); tinh=new JButton("Ket qua"); ct.add(tinh); tinh.addActionListener(this); setSize(300,400); setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==tinh) { try{ Socket cs=new Socket("localhost",5000); PrintWriter out=new PrintWriter(cs.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream())); //System.out.println(in.readLine()); String a=ta.getText(); String b=tb.getText(); String c=tc.getText(); out.println(a); out.println(b); out.println(c); tds.setText(in.readLine()); in.close();out.close();cs.close(); }catch(IOException e){} } } public static void main(String[] args) { Giaodienclient giaodienclient = new Giaodienclient(); } }

*************************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();

Khnh Chi Email : khanhchi0285@gmail.com

Page 21

Thc hnh java


String b=in.readLine(); String c=in.readLine(); float x,y,z,del; x=y=z=del=0; x=Float.parseFloat(a); y=Float.parseFloat(b); z=Float.parseFloat(c); String kq=""; del=(float)(y*y)-(4*x*z); // System.out.println(del); if(del<0) kq = "pt vo nghiem" ; else if(del==0) kq="PT co nghiem kep : x1=x2="+(float)(-y/(2*x)); else kq="PT co 2 nghiem : x1="+(float)((-y+Math.sqrt(del))/(2*x))+ "x2="+(float)((-y-Math.sqrt(del))/(2*x)); out.println(kq); break; } in.close();out.close();cs.close();ss.close(); } }

Xu li su kien trong java

7)TINH CHU VI DIEN TICH HINH CHU NHAT


***************************MAIN.JAVA*****************
package tinhchuvidientich_hcn; public class Main { public static void main(String[] args) { // TODO code application logic here MyFrame m=new MyFrame(); } } ************************MYFRAME.JAVA****************** package tinhchuvidientich_hcn; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyFrame extends JFrame implements ActionListener { JLabel l1,l2,l3,l4; JTextField t1,t2,t3,t4; JButton b1; public MyFrame() { super("Chuong trinh tinh chu vi, dien tich cua hinh chu nhat"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container ct=getContentPane();

Khnh Chi Email : khanhchi0285@gmail.com

Page 22

Thc hnh java


ct.setLayout(new GridLayout(5,2)); l1=new JLabel("Chieu dai"); ct.add(l1); t1=new JTextField(10); ct.add(t1); l2=new JLabel("Chieu rong"); ct.add(l2); t2=new JTextField(10); ct.add(t2); l3=new JLabel("Dien tich"); ct.add(l3); t3=new JTextField(10); ct.add(t3); l4=new JLabel("Chu vi"); ct.add(l4); t4=new JTextField(10); ct.add(t4); b1=new JButton("Tinh"); ct.add(b1); //Button lang nghe su kien tu TextField b1.addActionListener(this); setSize(300, 400); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { int cd,cr,dt,cv; cd=Integer.parseInt(t1.getText()); cr=Integer.parseInt(t2.getText()); dt=cd*cr; cv=(cd+cr)*2; t3.setText(String.valueOf(dt)); t4.setText(String.valueOf(cv)); } } }

8)TONG HAI SO
********************MAIN.JAVA******************
package Tong2So; public class Main { public static void main(String[] args) { // TODO code application logic here

Khnh Chi Email : khanhchi0285@gmail.com

Page 23

Thc hnh java


Tong t=new Tong(); //Tong_1 t1=new Tong_1(); } } *************************TONG.JAVA********************* package Tong2So; import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Tong extends JFrame implements ActionListener { JLabel s1,s2,kq; JTextField t1,t2,tg; JButton A,B; public Tong() { super("Tong 2 so "); Container ct=getContentPane(); ct.setLayout(new GridLayout(4,2)); s1=new JLabel("Nhap so 1 : "); ct.add(s1); t1=new JTextField(10); ct.add(t1); s2=new JLabel("Nhap so 2 : "); ct.add(s2); t2=new JTextField(10); ct.add(t2); kq=new JLabel("Tong : "); ct.add(kq); tg=new JTextField(10); ct.add(tg); A=new JButton("Tinh"); ct.add(A); //Button lang nghe su kien tu TextField A.addActionListener(this); B=new JButton("Reset"); ct.add(B); //Button lang nghe su kien tu TextField B.addActionListener(this); setSize(300, 400); setVisible(true); } public void actionPerformed(ActionEvent e)

Khnh Chi Email : khanhchi0285@gmail.com

Page 24

Thc hnh java


{ if(e.getSource()==A) { int cd,cr,dt; cd=Integer.parseInt(t1.getText()); cr=Integer.parseInt(t2.getText()); dt=cd+cr; tg.setText(String.valueOf(dt)); } if(e.getSource()==B) { t1.setText(""); t2.setText(""); tg.setText(""); } } }

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() {

Khnh Chi Email : khanhchi0285@gmail.com

Page 25

Thc hnh java


kq=0; System.out.println("Hay lam" + sobai + "bai toan cong !"); for(int i=1;i<=sobai;i++) { int a=(int)(Math.random()*10); int b=(int)(Math.random()*10); System.out.println(a + "+" + b+ "="); try { kq=Integer.parseInt(stdin.readLine()); }catch(IOException e){} if(kq==a+b) { System.out.println("Dung"); } else { System.out.println("Sai"); } } } }

*********************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); } } }

Khnh Chi Email : khanhchi0285@gmail.com

Page 26

You might also like