Welcome to TTKS/KTQN

CLICK HERE TO OPEN

Saturday, July 18, 2015

JAVA PROGRAM CÓ 2 INNER CLASSES

JAVA PROGRAM HAVING 2 INNER CLASSES.
   class Inner1 { … } and  class Inner2 { … }
 
1-  Compiler của Ideone.com phải dùng outer class là class Main.
 
public class Main {  
        String outerStr = " String in the class outer : THÂN CHÀO QUÝ BAN !\n"  ;
class Inner1 {
         String inner1Str = " String in the class Inner1 : \n" ;     
        String display1(String parameter) { 
        return   outerStr+ inner1Str + parameter ;  }       
        }   
    public static void main(String[] args) {
        Main outer = new Main();  
                        outer.run() ;
        Main outer1 = new Main();  
                         outer1.go() ;  }   
       void run() {  
        Inner1 local = new Inner1();
        System.out.println(local.display1(" Hola Amigaso ! COMMO ESTA ?"));
        }
       void go() {  
        Inner2 in2 = new Inner2();
        System.out.println(in2.display2("Hello Friends ! HOW ARE YOU ?"));
        }
 
class Inner2 { 
        String inner2Str =  "  String in the class Inner2 :\n" ;     
        String display2(String parameter) { 
        return inner2Str +  parameter ;   }        
 
     }
}
Output.
SUCCESS
String in the class outer : THÂN CHÀO QUÝ BAN !
 String in the class Inner1 : 
 Hola Amigaso ! COMMO ESTA ?
  String in the class Inner2 :
Hello Friends ! HOW ARE YOU ?
                   
2- Compiler của Rextester.com
 
class Rextester {  
        String outerStr = " String in the class outer : THÂN CH̀ÀO QUÝ BAN ! \n"  ;
class Inner1 {
         String inner1Str = " String in the class Inner1 :\n" ;     
        String display1(String parameter) { 
        return  outerStr + inner1Str + parameter }       
        }   
    public static void main(String[] args) {
        Rextester outer = new Rextester();  
                        outer.run() ;
        Rextester outer1 = new Rextester();  
                         outer1.go() ;  }   
       void run() 
        Inner1 local = new Inner1();
        System.out.println(local.display1(" Hola Amigaso ! COMMO ESTA ?"));
             }
       void go() 
        Inner2 in2 = new Inner2();
        System.out.println(in2.display2("Hello Friends ! HOW ARE YOU ?"));
        }
class Inner2 { 
        String inner2Str =  "  String in the class Inner2 :\n" ;     
        String display2(String parameter) { 
        return inner2Str +  parameter  }              
     }
}
output.
Compilation time: 0.82 sec, absolute running time: 0.14 sec,
 cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.97 sec
String in the class outer : THÂN CHÀO QUÝ BAN !
 String in the class Inner1 : 
 Hola Amigaso ! COMMO ESTA ?
  String in the class Inner2 :
Hello Friends ! HOW ARE YOU ?

Saturday, July 4, 2015

CALLING A JAVA METHOD FROM ANOTHER METHOD

HOW TO CALL A METHOD FROM ANOTHER METHOD WITHIN THE SAME MEMBER CLASS OR INNER CLASS.
Ghi chú hướng dẫn.
Trong main method, chúng ta có thể call những methods ở ngoài với điều kiện những methods đó phải static.
Nếu những methods ở ngoài không static thì phải taọ object cho class rồi mới call được .
Thí dụ: 1- non –static void run() ở trong class Rextester.
                    Rextester outer = new Rextester();  
                                    outer.run();
            2- non-static String display() ở trong class Inner. 
                    Inner  In = new Inner();
                                In.display();                    
                             -----------------
 1-Tạo method mới rồi dùng nó để call method khác .
class Rextester {  
        String outerStr = " This is the String of Outer class :"  ;
 class Inner {
        String innerStr =  "This is the String of Inner class :" ;     
 
/* This method display() will be called by method run() below */       
        String display(String parameter) {        
               return  innerStr +  outerStr + parameter ;}       
    }
public static void main(String[] args) {
 /*  create an object inside main method and call 
          method run() from main method*/
        Rextester outer = new Rextester();  
                        outer.run();
    }   
       void run() { 
             /* create an object outside main method and call 
                  method display()written inside class Inner*/
                       Inner  In = new Inner();
        System.out.println(In.display(" Hola Amigasos ! Chào Quý Ban "));
    }
 }
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
 
This is the String of Inner class : This is the String of Outer class : Hola Amigasos ! Chào Quý Ban. 
 
 
 
2- Không tạo method mới.Call trực tiếp từ main method.
class Rextester {   
 int x = 2015;  
 private String str1 = "NHƯNG MUÀ HÈ QUÁ KHỨ,HUÊ";  
 public String str2 = "DO YOU STILL REMEMBER ?";  
 protected String str3 = "\n CHIA TAY TỪ THUỎ RA TRƯÒNG.\n CUỐI ĐƠI NỐI LAI BỐN PHƯONG MỘT NHÀ.";  
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();
 System.out.println(" KÍNH CHÚC QUÝ BAN KHOE MANH LUÔN. ");
}}
OUTPUT.
2015
 NHƯNG MUÀ HÈ QUÁ KHỨ,HUÊ
 DO YOU STILL REMEMBER ?
 Ơ CUÔI CÙNG ĐÃ́T. 
 ĐOÀN TRAI NON HỚN HỞ RỦ NHAU VỀ.
 CHÍN MƯOI NGÀY NHẢY NHÓT Ở MIỀN QUÊ,….>
 CHIA TAY TỪ THUỎ RA TRƯÒNG.
 CUỐI ĐƠI NỐI LAI BỐN PHƯONG MỘT NHÀ.
 KÍNH CHÚC QUÝ BAN KHOE MANH LUÔN.
 

Saturday, June 13, 2015

CONSTRUCTOR IN JAVA

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