WELCOME TO BLOGGER VQGĐC

THÂN CHÀO QUÝ BẠN
CLICK HERE TO OPEN

Tất cả hình ảnh, những hoạt động cùng cơ sở Định Chuẩn rồi cũng cùng với thời gian rơi vào khoảng không
Nếu còn gì rớt lại chỉ là những tình cảm của những con người đã một thời làm việc chung dưới một mái nhà
mà nay đả tản mác khắp bốn phương trời
Ninh Vũ / Phòng Thí Nghiệm VQGĐC

Tuesday, June 23, 2015

CÁCH DÙNG JAVA NESTED CLASSES

NESTED CLASSES-HOW TO USE THEM ?
Ghi chú hướng dẫn.
Java  programming chấp nhận trong mỗi class, chúng ta có thể viết thêm một hay nhiều classes khác có một tên chung là nested class.
Có 2 loại nested class :  
* static nested class và non- static nested class.
* non- static nested class còn được gọi là INNER CLASS.
* class ngoài cùng gọi là OUTER CLASS.
* INNER CLASS viết trong OUTER CLASS.
* Trong bài nầy,người viết chọn OUTER CLASS là class Rextester để có thể xử dụng compiler của Rextester.com                                 
         
            class Outer {
                ………..
            class static Nested {
                 …. . ….  ..    }
            class Inner {
                 …. . ….       }
            }
 
Trong bài nầy chúng quan sát thí dụ cách xử dụng hai loại nested classes.
Tại sao chúng ta cần phải có nested class? Có vài lý do nhưng lý do chính là dể quản lý các code mặc dầu program hoạt động hơi chậm vì có nhiều classes.
                        ---------------------------
Thí dụ đơn giản nested class.
1-Method hay function không có return
     class Rextester{
   static class Nested{  
   void display(){    // Method having no return 
System.out.println(" Function display() is inside class Nested");}
}
    public static void main(String[] args){
           Nested ns= new Nested(); 
                        ns.display(); }
}
output.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.09 sec, memory peak: 21 Mb, absolute service time: 0.99 sec
 
Function display() is inside class Nested
 
2-Method hay function có return
 
   class Rextester{   
static class Nested{  // This is a nested class inside class Rextester   
 public String str =" Example of Nested Static Class in Java";    
 public String display(){   //  Method having return type là String
            return str;  }
    }  
  public static void main(String args[]){
        Nested ns = new Nested();
        System.out.println(ns.display());
    }
}
output
Compilation time: 0.73 sec, absolute running time: 0.14 sec, 
cpu time: 0.08 sec, memory peak: 23 Mb, absolute service time: 0.87 sec
 
Example of Nested Static Class in Java
 
3- Class có chứa 2 nested classes
 
     class Rextester{
   static class Nested{  // nested class number 1
   public String str =" Hello Friends ! Đây thí dụ : Nested Static Class in Java";
   public String display(){      // Method có return String type
            return str;}
  } 
 
    static class xaoKe{  // nested class number 2
   public String str = " Holla Amigasos ! This is : Nested Static xaoKe Class in Java";
   public String showUp(){      // Method có return String type
            return str ;}
  }  
 
public static void main(String args[]){
      Nested ns = new Nested();
        System.out.println(ns.display());      
       xaoKe xk= new xaoKe();
        System.out.println(xk.showUp()); }
}
output.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.08 sec, memory peak: 23 Mb, absolute service time: 0.98 sec
 
Hello Friends ! Đây thí dụ : Nested Static Class in Java
 Hola Amigasos ! This is : Nested Static xaoKe Class in Java
  
4-CLASS CÓ CHỨA NESTED CLASS VÀ INNER CLASS.    
    Muốn call method của INNER CLASS phải tạo object cho INNER CLASS có tên oi tự do chọn như sau.
                      outerClass.InnerClass oi = new outerClass.new InnerClass();
                         Rồi call method.
                         thí dụ  oi.display(); oi.showUp();
    Trong bài nầy,người viết chọn outerClass là class Rextester để có thể xử dụng compiler của Rextester.com                                 
                                       ------------------------
 class Rextester{
static class Nested{  
        void display(){    // Method having no return 
System.out.println(" Đây là method display() chứa trong class Nested");}
}
class Inner{  
 public  String str =" Đây là String chứa trong inner class";    
 public String showUp( ){     
            return str;}
 }
 public static void main(String[] args){
            Nested ns= new Nested();
            ns.display();           
     // call method showUp() như sau
Rextester.Inner  oi = new  Rextester().new Inner();
System.out.println(oi.showUp());}
}
OUTPUT.
Compilation time: 0.72 sec, absolute running time: 0.13 sec,
 cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
 
Đây là function chứa trong class Nested
 Đây là String chứa trong inner class
 
 
5- INNER CLASS
Method của INNER CLASS có thể xử dụng variables của OUTER CLASS.
Thí Dụ
 class Rextester {   
 int x = 2015;  
 private String str1 = "Henry Duong is living in California";  
 public String str2 = "Bolsa Markets  are very attractive !";  
 protected String str3 = "Hello Friends Around The World ! Please come to see them";  
 
class Inner {  
          void display() {  
 System.out.println( x + "\n " + str1 + "\n " + str2+ "\n " + str3);  }  
 }  
  public static void main(String[] args){         
Rextester.Inner  oi = new  Rextester().new Inner();
oi.display();}
}
OUTPUT.
Compilation time: 0.73 sec, absolute running time: 0.13 sec, 
cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
 
2015
 Henry Duong is living in California,USA
 Bolsa Markets are very attractive !
 Hello Friends Around The World ! Please come to see them.
 
Permanent link: http://rextester.com/GENA85861
 
 

Monday, June 15, 2015

JAVA CONSTRUCTOR.

JAVA CONSTRUCTORS-HOW TO USE THEM?
 GHI CHÚ HƯỚNG DẪN.
Constructor là một  “block of code”  có mục đích dành chỗ trong memory cho objects của Java class.
Constructor có ký hiệu viết giống hệt method ( còn gọi function ) nhưng không phải method vì không có return type.Còn method thì có return type.
Thí du đây là những constructors có syntax như sau : Rextester(){….  }; Baxao(){….  } ; Xaoke(){….  }; Box ();{….  } Car(){….  }; Dog(){….  } .
Nếu trong round brackets (… ) bỏ trống thì gọi default constructor.
Trong curly brackets{….  } là chỗ chúng ta muốn viết những gì để in ra .
Muốn vận hành constructor thì phải taọ một object cho class.
Thí dụ chúng ta chọn tên class Rextester thì object của class là : Rextester obj = new Rextester();
Nếu constructor có arguments thì trong object phải chứa variables mà chúng ta muốn pass tới arguments.
Thí dụ đây là constructor có 3 arguments chữ màu xanh lá cây : Rextester(int id1, int id2, String str).
Đây là object có chứa 3 variables chữ màu pink : Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! VQGĐC still survives but its name was changed after 1975" );
Vì constructor không phải là function hay method nên chúng ta không thể call nó trực tiếp.
Muốn constructor được vận hành(executed), chúng ta phải viết một method khác, thí dụ public void display().Trong method nầy chúng ta viết những gì đã viết trong constructor và ra lệnh cho display() in ra những thứ đó khi chúng ta call nó bằng cách viết như sau :                  obj3.display();
Muốn có obj3 , chúng ta phải taọ một object thí dụ tên là new Rextester(…….) mà trong round brackets(…….) có chứa những variables chúng ta muốn pass vào contructor.
Nếu trong constructor có 3 arguments thì chúng ta phải có đủ 3 variables để pass vào constructor.
Thí dụ đây là object obj3 có đủ 3 variables.
      Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.\nDO YOU KNOW WHY???" );      
 
                           -----------------------
   Thí dụ  Java constructors và cách xử dụng.
   
1-  CONSTRUCTOR CÓ DÙNG KEYWORD “THIS”.
  Chữ this dùng để   “refer to the object in the constructor ”.
 
class Rextester{
  int id1, id2;
  String str;
    //Đây là default constructor nên không có parameters
   Rextester(){
      System.out.println("Default constructor does’t have parameters.");}
    
   // Đây là parameterized constructor có 3 args.  
  public Rextester(int id1, int id2, String str){
       this.id1=id1; this.id2=id2; this.str=str;
      System.out.println("Parameterized constructor having 3 args.\n"); }
      
    // Đây là method để display3 args.
  public void display(){
  System.out.println( id1 + " " + id2 + " " + str);}
 
 public static void main(String args[]){
      
      //This invokes default constructor
      Rextester obj = new Rextester();
      
      // This  invokes the constructor of 3 args  
      Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.\nDO YOU KNOW WHY???" );      
                  obj3.display(); // calls the above function display
}
}
output.
Compilation time: 0.73 sec, absolute running time: 0.14 sec, 
cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.88 sec
 
Default constructor does’t have parameters.
Parameterized constructor having 3 args.
 
1967 2015  Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.
DO YOU KNOW WHY???
                     Permanent link: http://rextester.com/JWTM8681
 
2- CONSTRUCTOR KHÔNG  DÙNG KEYWORD “THIS”.
 
class Rextester {
  private String myVar;
  public Rextester(String str) {
      myVar = str;  } //Constructor having no keyword this.
  public String getString(){ //Đây là method có return type là String
      return myVar;}
 
  public static void main(String[] args) {
    Rextester ct = new Rextester("Hello Friends ! Thân chào quý ban"); 
    System.out.println(ct.getString());
  }
}
output
 
Hello Friends ! Thân chào quý ban
 
3- METHOD HAY FUNCTION CÓ RETURN TYPE LÀ STRING.
 
Ghi chú hướng dẫn.
Khi viết method hay function muốn có return type thi method phải có chữ String nếu muốn String type, phải có chữ int nếu muốn integer type.
Method phải có chữ static nếu muốn call function bằng cách viết display( ) trong main method.
Nếu không có chữ static thì sẽ xuất hiện error như đây.
“error: non-static method display(String) cannot be referenced from a static context”
Nếu không dùng chữ static thì phải tạo một object cho class rồi call method bằng cách nối object với display( ) trong main method như thí dụ trên.
                                                ----------------------
class Rextester {
  private String str;  // không cần
  public static String display( String str){
      return str ;}  // Đây là method có return type là String.
  public static void main(String[] args) {
    System.out.println(display("Hello Friends ! Thân chào quý ban") ); 
  }
}
output
Compilation time: 0.82 sec, absolute running time: 0.14 sec,
 cpu time: 0.08 sec, memory peak: 23 Mb, absolute service time: 0.97 sec
 
Hello Friends ! Thân chào quý ban
 

Saturday, June 6, 2015

UNDERSTANDING THREADS USED IN JAVA PROGRAM

TÌM HIỂU NHỮNG SỢI CHỈ DỆT THÀNH JAVA PROGRAM.
GHI CHÚ HƯỚNG DẪN. 
Người phát minh Java programming xem Java program giống như một tấm vải dệt bằng những sợi chỉ gọi là THREADS.
Thread là tên gọi của mỗi phần (part) vận hành theo một việc (task) riêng rẽ nhưng cùng một lúc trong Java program.Thread còn được gọi là lightweight process.Nếu không có thread thì không tạo được Java program.
Khi những Threads được vận hành ( executed) sẽ giống như những sợi chỉ dệt thành vãi trong ngành dệt vãi sợi để tạo ra fabric of program.
Trong mỗi Java program có thể chứa 2 hay nhiều threads và những threads nầy vận hành cùng một lúc khi được called để xử dụng tôi đa CPU, không cho CPU rảnh rỗi.

 Trong Java class có cái viết giống method gọi là constructor.Constructor phải có tên giống hệt tên của class dùng để dành chỗ trong memory cho object cho nên constructor còn được gọi là initialization of new object.
Constructor là một “block of code” có ký hiệu viết giống hệt method nhưng không phải method vì không có return type.Method thì có return ype.
Thí du. Rextester(); Baxao() ; Xaoke(); Box (); Car(); Dog()
Tất cả đều là constructors và phải có tên giống như tên của class.
Dùng constructor để tạo ra object bằng cách viết thêm chữ new trước constructor.
 new Baxao(); new Box ();  new Car(); rồi  viết dấu bằng  “=”  với tên của class.Thí du.
class Baxao{  } thi object của class sẽ là Baxao  bx = new Baxao();
bx là variable do tùy ý chúng ta chọn bất cứ chữ gì.
                                     ---------------------------
Each part of a Java program is called a thread. Each thread has a separate path of execution and can run concurrently, simultaneously with others .We put threads in Java program in order to keep the idle time of CPU to minimum.
    When the program starts running, the main thread and the GUI thread will  run simultaneously                                  
 Java classes have special method called constructor. Constructor must have the same name as the class and is used to initialize a new object. 
                                     -----------------------------
Có hai phương pháp để tạo ra Java thread . 
   * Runnable interface  còn được gọi Implement Runnable.
   *  Extending Thread class.
 Cả hai phương pháp đều cần phải tạo thread object là :
                     Thread thread = new Thread ().
1-Phương pháp Runnable interface .
     
  1.1-  Thí dụ.
class Rextester{  
  public static void main(String args[]){
            Thread thread1 = new Thread (){     
  public void run() { 
  System.out.println("Method 1:runnable interface.Thread is running \n Everything is inside main method");
      } };
             thread1.start();
     }
    }
Output.
    Compilation time: 0.83 sec, absolute running time: 0.13 sec, 
cpu time: 0.08 sec, memory peak: 22 Mb, absolute service time: 0.97 sec
Method 1:runnable interface.Thread is running 
 Everything is inside main method

1.2-Thí dụ.
  
/*class must have  2 words “implements Runnable” */
class Rextester implements Runnable{     
public void run(){
System.out.println("Hello Friends.Thread of Method Runnanble is running");}
public static void main(String args[]){
  Rextester T = new Rextester();
      Thread thread = new Thread(T);
      thread.start();
  }
}
Output.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.08 sec, memory peak: 21 Mb, absolute service time: 0.97 sec
 
Hello Friends.Thread of Method Runnanble is running


2- Phương pháp extending Thread class.
     
  2.1-  Thí dụ.
/*class must have 2 words “ extends Thread ”  */
class Rextester extends Thread{ 
    public void run(){
System.out.println("Method 2:using extends Thread \n Thread is running");} 
    public static void main(String args[]){
    Rextester t2 = new Rextester();
    t2.start();
}
 }
Output.
Compilation time: 0.72 sec, absolute running time: 0.13 sec, 
cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
Method 2:using extends Thread 
 Thread is running

2.2-Thí dụ xử dụng chữ “this” trong class extends Thread.
        Dùng chữ this để đề cập tới( refer to) object trong constructor.
       Tên của constructor phải giống hệt như tên của class. Constructror không phải là method mặc dầu viết giống method vì constructor không có return type.Còn method thì có.
 
class Rextester extends Thread {
   private String name;  
   public Rextester(String name) { // Đây là constructor
      this.name = name;}
   public void run() {  
      System.out.println(" Hello from thread : " + name + "!"); }
   public static void main(String args[]){  
  Rextester  thread = new Rextester(" Than chào Quý Ban-Holla Amigosos");
                    thread.start(); 
  System.out.println("Thread has been started");}
   }
OUTPUT.
Compilation time: 0.73 sec, absolute running time: 0.14 sec, 
cpu time: 0.1 sec, memory peak: 23 Mb, absolute service time: 0.88 sec
Thread has been started
 Hello from thread :  Than chào Quý Ban-Holla Amigosos!
 
GHI CHÚ.
Khi thread.start() vận hành thì có 2 threads xuất hiện :
  * một thread in ra câu Than chào Quý Ban-Holla Amigosos!
* một thread in ra câu Thread has been started.
Nếu class có chứa nhiều threads ,chúng ta sẽ không biết chắc chắn câu nào được in ra trước và câu nào được in ra sau.

3-Phương pháp 1 và 2 viết chung trong một class.
  Class Rextester extends Thread cũng vận hành được  phương pháp 1 
     (runnable interface).
         
    Thí dụ.
class Rextester extends Thread{ 
    public void run(){
System.out.println("Method 2:using extends Thread .Thread is running and remember run method is located outside main method");} 
    public static void main(String args[]){
    Rextester t2 = new Rextester();
               t2.start();
    
    Thread thread1 = new Thread (){     
  public void run() { 
  System.out.println("Method 1:runnable interface.Thread is running \n Everything is inside main method . Remember run method is embedded in Thread object");
      } };
              thread1.start();
     }
}
Output of method 1 and method 2.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.08 sec, memory peak: 25 Mb, absolute service time: 1.19 sec.

*Method 2:using extends Thread .Thread is running and remember run method is located outside main method
*Method 1:runnable interface.Thread is running .
 Everything is inside main method . Remember run method is embedded in Thread object.