THÂN CHÀO QUÝ BẠN
Blogger nầy chỉ tồn trử kiến thức và kinh nghiệm về nghề “Materials Testing”, “Textile Screen Printing” và “Internet Programming” của một kỹ sư đã phục vụ tại :
* Trung Tâm Khảo sát Kỹ Thuật Quân Nhu /QLVNCH “ & “ Viện Quốc Gia Định Chuẩn/VNCH”
* Xí nghiệp “Hiệp Hưng / VN” ( sãn xuất giày vãi cao su )
"Bradbury Company Inc/USA"(Textile Screenprinting,Imprinted Sportswear Programs) & “Sundance Graphics Inc / USA” ( in bông vãi sợi ) từ năm 1965 để dành cho thế hệ trẻ muốn học nghề.
Vạn Vật Thái Bình (PEACE ON EARTH).

Sunday, May 10, 2015

SOME JAVA PROGRAMS OF SALE RECEIPTS

NHỮNG HÓA ĐƠN BÁN HÀNG CÓ SALE TAX.
 1-Receipt of one item.
 public class Purchase {
 // We declare constant outside of main;all methods can access to it.
  final static double TAX_RATE = .08;
  public static void main(String[] args) {
      double itemprice = 10.25;
     // We calculate the total price here.
      double finalprice = itemprice*(1+TAX_RATE);
    System.out.println(" Your purchase  cost : "+finalprice+" with tax.");
} }
SUCCESS.
Compilation time: 0.94 sec, absolute running time: 0.24 sec,
cpu time: 0.12 sec, memory peak: 23 Mb, absolute service time: 1.19 sec
Output. Your purchase cost 11.07 with tax.
 
2-Receipt of 2 items.
 
class Rextester{  
  final static double TAX_RATE = 0.08;
  public static void main(String[] args) {
     double item1price = 10.25,item2price = 5.25,item3price = 2.25;
     double finalprice = (item1price+item2price+item3price)*(1+TAX_RATE);
 System.out.println("Your total purchase is:"+finalprice+" with tax.");
 }}
SUCCESS
Compilation time: 1.14 sec, absolute running time: 0.26 sec, 
cpu time: 0.13 sec, memory peak: 22 Mb, absolute service time: 1.41 sec
output
Your total purchase is:19.17 with tax.

3- Receipt for T-Shirts Shop.

    USING JAVA SCANNER
class ideone{
public static void main (String[] args) {
      System.out.println (" T-SHIRTS SHOP ESTABLISHED IN 1986 ");
      System.out.println (" Owner : Henry Baxao Xaoke ");
      System.out.println ("----------------------------------");
     int tsmall, tmed, tlarge, xlarge ;
     double price1 =1.5, price2 = 2.5, price3 = 3.25, price4= 3.75, total1, total2 ;
     Scanner scan = new Scanner (System.in);
     System.out.println (" *Muôn bao nhiêu Small,Medium,Large,Xlarge ? : ");
          tsmall = scan.nextInt(); tmed = scan.nextInt();
          tlarge = scan.nextInt(); xlarge = scan.nextInt();
System.out.println (" *Ban mua : " +tsmall+ " "+ "SM" +";"+tmed+" "+ "MD" +";"+tlarge+" " +"LG" +";"+ xlarge+ " " +"XLG");  
total1 =(tsmall*price1 + tmed*price2 + tlarge*price3 + xlarge*price4);
total2 =(tsmall*price1+ tmed*price2+ tlarge*price3+ xlarge*price4)*1.08;
System.out.printf (" *Giá tông công không có Tax :  %.2f%n ", total1);
System.out.printf (" *Giá tông công có Tax :  %.2f%n ", total2);
 System.out.println (" THANK YOU-CÁM ON" );
// Viết double total, nhưng nếu dùng format thì phải đổi ra %.2f%n mới chọn được // // những con số sau dấu chấm theo ý muốn.
}}
SUCCESS.
Outpt.
T-SHIRTS SHOP ESTABLISHED IN 1986 
 Owner : Henry Baxao Xaoke 
----------------------------------
 *Muôn bao nhiêu Small,Medium,Large,Xlarge ? : 
 *Ban mua : 8 SM;6 MD;4 LG;2 XLG
 *Giá tông công không có Tax :  47.50
  *Giá tông công có Tax :  51.30
  THANK YOU-CÁM ON
 
4-Receipt Written With Java Formatting.
       Using Java Formatter.
        class Rextester{      
       private double total = 0;
       private double total2 = 0;
       private Formatter f = new Formatter(System.out);
      
       public void printTitle(){
        f.format("%-15s %10s %15s \n", "ITEM", "QTY","PRICE");
        f.format("%-15s %10s %15s \n", "----","----" ,"----");}

       public void print(String name, int qty, double price){  
        f.format("%-15s %10d %15.2f \n", name, qty, price);total+=qty;
        total2+=price; }
      
       public void printTotal(){
       f.format("%-15s %10s %15.2f\n", "TAX", "   " , total2*0.08);
       f.format("%-15s %10s %15s\n",    "   ",  "----", "----");
       f.format("%-15s %10.1s %15.2f\n", "TOTAL", total, total2*1.08);}
       
       public static void main(String[] args){
       receipt.printTitle();
       receipt.print("T-shirts small ", 4, 12.00);
       receipt.print("Ceramic spoons", 2, 2.50);
       receipt.print("Plastic bucket",1, 4.25);
       receipt.print("Aluminium foil",2, 2.50);
       receipt.print("Saran", 2, 5.00);
       receipt.printTotal();
}}
SUCCESS . Compilation time: 0.73 sec, absolute running time: 0.14 sec,
cpu time: 0.06 sec, memory peak: 18 Mb,absolute service time: 0.88 sec
Output.
ITEM                   QTY           PRICE 
----                  ----            ---- 
T-shirts small           4           12.00 
Ceramic spoons           2            2.50 
Plastic bucket           1            4.25 
Aluminium foil           2            2.50 
Saran                    2            5.00 
TAX                                   2.10
                      ----            ----

TOTAL                   11           28.35

Tuesday, May 5, 2015

Creating Your Own Java Methods and Passing Values to Them

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



                   *********************************

Monday, May 4, 2015

HOW TO USE JAVA FORMATTING

Xử dụng Java Formatter f = new Formatter(System.out) & System.out.printf() Method.

* Formatter f = new Formatter(System.out) của Formatter class chứa trong Java 1.5 có nhiệm vụ tự động sắp đặt vị trí và điều chỉnh khoảng cách ( auto padding ) của chữ viêt và các con số theo ý chúng ta.
Vì chúng ta dùng Formatter để ouput PrintStream bytes nên Formatter phải có chứa  System.out .
public void printTitle(){  }  Để viết đầu đề headers và vẽ đường ngăn cách
public void print(){  }        Để viết tên món hàng, số lượng, giá cả
public void printTotal(){  } Để viết tổng cộng các món hàng, tổng cộng giá của các món hàng, tax .
*  public static void main(String[] args){  } Để vận hành receipt program.
 Dấu % nghĩa là bắt đầu formatting expression.
 Dấu %10 nghĩa là cần có một khoảng rộng 10 byte field (The value of field width is 10).Mỗi byte field là một chữ character.
 Dấu %10d. Sau byte field phải có một trong những chữ sau.
 s dùng cho string, d dùng cho decimal interger, f dùng cho floating point, t dùng cho time,date vv…
 %-15s : chọn dấu tr̀ư khi chúng ta lấy bên trái làm chuẩn cho output (left-justify).
 %10.2s : chỉ lấy 2 chữ số. %10.1s : chỉ lấy một chữ số. %10.0s : không lấy chữ số nào hết nghĩa là bỏ trống
Dấu  \n  gọi là escape sequence hay new line .
Muốn có con số tổng cộng các món hàng thì phải viết total += qty                                        
Muốn có con số tổng cộng giá cả của tất cả các  món hàng thì phải viết total += price                                        
Muốn có Total Tax cho tất cả các món hàng thì phải viết : final price * (1+TAX_RATE) mới cho kết quả đúng.
Thí dụ.
     double item1price = 10.25,item2price = 5.25,item3price = 2.25;
     double finalprice = (item1price+item2price+item3price)*(1+TAX_RATE);
 System.out.println("Your total purchase is:"+finalprice+" with tax.");
Output. Your total purchase is:19.17 with tax.


                                  ---------------------
Hai thí dụ sau đây giúp chúng ta biết cách xử dụng Java formatting để trình bày một bảng thống kê về kết quả thử nghiệm máu cho nên không không xử dụng những code về tổng cộng và sale tax
                                  ----------------------
Method 1. Cần có private Formatter f = new Formatter(System.out);

class Rextester{
private Formatter f = new Formatter(System.out);
public void printTitle(){
f.format("%-15s %10s %15s %15s\n", "YEAR","GLUCOSE","CHOLESTEROL","TRIGLYCERIDES");
f.format("%-15s %10s %15s %15s\n", "----","----" ,"----","----");}
public void print(String name, int qty, int chole,int trigly){
 f.format("%-15s %10d %15d %15d\n", name, qty, chole,trigly);}
public static void main(String[] args){
 System.out.println("DUONG, HENRY H" );
 System.out.println("BLOOD TEST PERFORMED AT SAN CLEMENTE HOSPITAL CA 92672" ); 
       Rextester result = new Rextester();
       result.printTitle();
       result.print("2012", 90, 191,86);
       result.print("2013", 94, 184, 131);
       result.print("2014", 91, 168, 124);
       result.print("2015", 87, 172, 114);
 System.out.println("Daily food : 80 percent vegetables of different kinds" );
    }}
SUCCESS.
Compilation time: 0.94 sec, absolute running time: 0.13 sec,
 cpu time: 0.09 sec, memory peak: 22 Mb, absolute service time: 1.07 sec
DUONG, HENRY H
BLOOD TEST PERFORMED AT SAN CLEMENTE HOSPITAL CA 92672
YEAR               GLUCOSE     CHOLESTEROL   TRIGLYCERIDES
----                  ----            ----            ----
2012                    90             191              86
2013                    94             184             131
2014                    91             168             124
2015                    87             172             114
Daily food : 80 percent vegetables of different kinds
 
Permanent link: http://rextester.com/PKBZ60989

Method 2 . Không cần Formatter f = new Formatter(System.out) nhưng cần có :
* System.out.printf() method.
* Tất cả functions/methods đều phải có public static.
System.out.printf() là một formatting method của PrintStream class chứa trong java.io package.

PrintSteam class có 2 formatting methods là System.out.printf() và System.out.format() để thay thế  System.out.println()

class Rextester{ 
public static void printTitle(){
System.out.printf("%-15s %10s %15s %15s\n", "YEAR","GLUCOSE","CHOLESTEROL","TRIGLYCERIDES");
System.out.printf("%-15s %10s %15s %15s\n", "----","----" ,"----","----");}
public static void print(String name, int qty, int chole,int trigly){
System.out.printf("%-15s %10d %15d %15d\n", name, qty, chole,trigly);}
public static void main(String[] args){
 System.out.println("DUONG, HENRY H" );
 System.out.println("BLOOD TEST PERFORMED AT SAN CLEMENTE HOSPITAL CA 92672" ); 
       printTitle();
       print("2012", 90, 191,86);
       print("2013", 94, 184, 131);
       print("2014", 91, 168, 124);
       print("2015", 87, 172, 114);
 System.out.println("Daily food : 80 percent vegetables of different kinds" );
 }}

*  Xử dụng System.out.printf() Method.Có output như sau.
SUCCESS.
Compilation time: 0.94 sec, absolute running time: 0.24 sec,
cpu time: 0.13 sec, memory peak: 22 Mb, absolute service time: 1.18 sec
DUONG, HENRY H
BLOOD TEST PERFORMED AT SAN CLEMENTE HOSPITAL CA 92672
YEAR               GLUCOSE     CHOLESTEROL   TRIGLYCERIDES
----                  ----            ----            ----
2012                    90             191              86
2013                    94             184             131
2014                    91             168             124
2015                    87             172             114
Daily food : 80 percent vegetables of different kinds

*  Xử dụng Formatter. Có output như sau.
Compilation time: 0.94 sec, absolute running time: 0.13 sec,
 cpu time: 0.09 sec, memory peak: 22 Mb, absolute service time: 1.07 sec.
DUONG, HENRY H
BLOOD TEST PERFORMED AT SAN CLEMENTE HOSPITAL CA 92672
YEAR               GLUCOSE     CHOLESTEROL   TRIGLYCERIDES
----                  ----            ----            ----
2012                    90             191              86
2013                    94             184             131
2014                    91             168             124
2015                    87             172             114
Daily food : 80 percent vegetables of different kinds
 
Permanent link: http://rextester.com/PKBZ60989


NHẬN XÉT VÀ SO SÁNH 2 METHODS.

Chúng ta thấy 2 outputs giống hệt  nhau.
* Nếu không xử dụng Formatter thì cpu time nhiều hơn,running time nhiều hơn,service time nhiều hơn.
* CPU time là thời gian CPU cần có để chuyển đổi instructions của program cho computer hay cho operating system.
Trong lúc program đang vận hành,CPU vẫn có những lúc nghĩ ngơi goị sitting idle để computer đón nhận data từ keyboard hay disk hoặc gửi data tới máy in.Đó là lý do cpu time luôn ít hơn thời gian vận hành program goị là running time.
Khi sọan một một program cần chú ý tới running time càng ít càng tốt bằng cách lựa chọn những code ít memory.


Tuesday, March 31, 2015

HOW TO CREATE A JAVA ARRAY

PHƯƠNG PHÁP VIẾT JAVA ARRAY.
Có 4 phương pháp.
      Syntax của Java array :
      1- String[]  ar1 = new String[]{"String1", "String2", "String3"} ;
      2- String[]  ar2 = new String[3] ;
      3- String[]  ar3 = {"Mango", "Papaya", "Apple"};
      4- String[]  ar4 = {"Henry: Engineer", "Pano: Engineer", "Brandone: CEO"};
                                    ----------------------
   class displayArray{
   public static void main(String[] args) {  
String[]  ar1 = new String[] {"Baxao", "Pano", "Juan"}; //cách thứ 1     
        String[]  ar2 = new String[3];  // cách thứ 2
                ar2[0] = "Henry";
                ar2[1] = "Pano";
                ar2[2] = "Juan";
        String[]  ar3 = {"Mango", "Papaya", "Apple"}; // cách thứ 3
 String[] ar4 = {"Henry :Engineer", "Pano: Engineer", "Brandone:CEO"};
   
                System.out.println(ar1[0]); // cách thứ 1
              
                    for(int i=0; i < ar2.length; i++){ // phải xử ḍung chữ .length
                System.out.println(ar2[i]);}  // cách thứ 2
              
                    for(String value: ar3){
                System.out.println(value);}  // cách thứ 3
                  
                   for( int i =0;i<ar4.length;i++){  // cách thứ 4
                System.out.println(ar4[i]);}

}}
SUCCESS.
Compilation time: 0.83 sec, absolute running time: 0.13 sec,
cpu time: 0.09 sec, memory peak: 18 Mb, absolute service time: 0.96 sec
Output.
Baxao
Henry
Pano
Juan
Mango
Papaya
Apple
Henry :Engineer
Pano: Engineer
Brandone:CEO

GHI CHÚ.
* Nếu muốn dùng chữ new thì phải viết theo một trong 2 cách sau:
 String[]  ar1 = new String[]{"String1", "String2", "String3"} ;
 String[]  ar2 = new String[3]; 
                ar2[0] = "Henry";
                ar2[1] = "Pano";
                ar2[2] = "Juan";
* Nếu không muốn dùng chữ new thì viết:
 String[]  ar3 = {"Mango", "Papaya", "Apple"};
* Nhớ cần phải xử dụng chữ .length để tiếp cận với parameter của array dầu cho array có cở rất lớn hay nhó.
* Xử dụng tất cả free online compilers có tên sau đây đều cho output giống nhau.

Jdoodle.com, Rextester.com, Ideone.com