I-Tự Tạo Một Java Method Và Passing Values Vào Method
Đã Tạo.
1.1- Tự tạo method để
cộng các con số tăng liên tục total +=i.
class rextester{
public static void printTotal(){  // tự tạo method nầy và phải co
static
int total = 0;
for(int i = 1 ;
i<=6; i++){
total +=i ;
System.out.println(total);}
}
public static void main(String[] args){
             printTotal();} // call method đã tự tạo 
}
SUCCESS.
Output.
Compilation
time: 1.03 sec, absolute running time: 0.14 sec, 
cpu time: 0.11
sec, memory peak: 23 Mb, absolute service time: 1.19 sec
 
1
3
6
10
15
21
1.2- PASSING VALUES TO THE PARAMETERS OF
JAVA METHOD
     * Value là integer
class rextester{
public static void passMethod(int x){ // tự tạo method nầy và phải có static
System.out.println("Passed
value is :" + x ); }
public static void main(String[] args)
{
      int
x = 5;  
      passMethod(x); } // call và pass integer vào method đã tạo 
} 
SUCCESS.
Output.
Compilation
time: 1.04 sec, absolute running time: 0.14 sec, 
cpu time:
0.09 sec, memory peak: 22 Mb, absolute service time: 1.19 sec
Passed value is :5
 
  * Value là string 
class rextester{
 public static void passMethod(String str){
System.out.println("Passed value is :" + str );
}
public static void main(String[] args)
{
      String str = "Hello My
Friends";  
      passMethod(str); } // call và pass string vào method
đã tạo
}
 SUCCESS.
Output.
Compilation time: 0.94 sec, absolute running time: 0.24 sec, 
cpu time: 0.12 sec, memory peak: 23 Mb, absolute service time: 1.18 sec
Passed value is :Hello My Friends
* Value là  name
class rextester{
      public static void Person(String name){
// tự tạo method nầy.
      System.out.println("Passed Name is
:" + name ); }
 
    public static void main(String []args){
     String name=
"Henry";
              Person(name);}  // call và pass string vào method đã
tạo           
} 
SUCCESS.
Output.
Compilation
time: 1.03 sec, absolute running time: 0.15 sec, 
cpu time: 0.1
sec, memory peak: 23 Mb, absolute service time: 1.18 sec
Passed Name is :Henry
II-Tư tạo nhiều
methods và pass data vào những methods đã tạo.
 2.1 
   
  class Rextester{  
     public static void Employee(String name){
         System.out.println("Name:"+ name );}
     public static void empAge(int x){
         System.out.println("Age:" + x );}
     public static void
main(String args[]){
         String
name =  "Xaoke";int x =26;
         Employee(name);empAge(x); }
     }
SUCCESS.
Output. Compilation time: 0.94 sec, absolute
running time: 0.14 sec, 
cpu time: 0.09
sec, memory peak: 23 Mb, absolute service time: 1.09 sec
Name:Xaoke
Age:26
2.2
   
class Rextester{  
 public static void Test(String name,int x,String skill){
System.out.println(name
+" "+ x +" "+ skill);}
public static void main(String[] args){
String name=
"Henry"; int x=26; String skill="Programmer";
Test(name,x,skill);}
}
SUCCESS.
output
Compilation time: 0.84 sec, absolute running time: 0.13 sec,
 cpu time: 0.06 sec, memory
peak: 18 Mb, absolute service time: 0.98 sec
Henry 26 Programmer
2.3
 class Rextester{  
 public static void Test1(String
name1,int x1,String skill1){
 System.out.println(name1
+" "+ x1 +" "+ skill1);}
 public static
void Test2(String name2,int x2,String skill2){
 System.out.println(name2
+" "+ x2 +" "+ skill2);}
  public
static void main(String[] args){
String name1= "Henry"; int x1=26; String
skill1="Programmer";
String name2= "Pano"; int x2=69; String
skill2="Engineer";
Test1(name1,x1,skill1);Test2(name2,x2,skill2); }
}
SUCCESS.
output
Compilation time: 0.83 sec, absolute running time: 0.13 sec,
 cpu time: 0.06 sec, memory
peak: 18 Mb, absolute service time: 0.97 sec
Henry 26 Programmer
Pano 69 Engineer
*********************************
 
 
No comments:
Post a Comment