วันจันทร์ที่ 2 พฤศจิกายน พ.ศ. 2558

LAB 8 Java Generic 1-12

Lab 8 Java Generic 1 ( Display each student record )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void diaplay_student_record(Student[] studentRec){
   int index = 0;
        System.out.println("--Students Record--");
        while(index < studentRec.length){
          
          System.out.println("---"+(index+1)+"---");
          System.out.println("Name : "+studentRec[index].get_name());
          System.out.println("ID : "+studentRec[index].get_idNumber());
          System.out.println("Age : "+studentRec[index].get_age()+" years");
          System.out.println("Weight : "+studentRec[index].get_weight()+" Kg");
          System.out.println("Height : "+studentRec[index].get_height()+" cm");
          System.out.println(" ");
          
          index += 1;
          
          }
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 55 , 168 ),
                                    new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                    new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          
          diaplay_student_record(studentRec);
        }
    }

Lab 8 Java Generic 2 ( Find student BMI )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void diaplay_bmi(Student[] studentRec){
   int index = 0;
        System.out.println("--Students Record--");
        float bmi;
        float height;
        float weight; 
        while(index < studentRec.length){      
          height = studentRec[index].get_height();
          weight = studentRec[index].get_weight();
          bmi = weight/(height*height/10000);
          System.out.println("---"+(index+1)+"---");
          System.out.println("Name : "+studentRec[index].get_name());
          System.out.println("ID : "+studentRec[index].get_idNumber());
          System.out.println("BMI  : "+bmi);
          System.out.println(" ");
          
          index += 1;
          
          }
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                    new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                    new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          
          diaplay_bmi(studentRec);
        }
    }

Lab 8 Java Generic 3 ( Find/count number of students with BMI > 25 )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void count_bmi_above_25(Student[] studentRec){
        int index = 0;
        int count = 0;
        float weight;
        float height;
        float bmi ;
        while(index < studentRec.length){
          weight = studentRec[index].get_weight();
          height = studentRec[index].get_height();
          bmi =  weight*10000/(height*height);
          if(bmi > 25)
            count += 1;
          index += 1; 
        }
        System.out.println("Number of students with BMI > 25 is "+ count);             
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          count_bmi_above_25(studentRec);
        }
    }

Lab 8 Java Generic 4 ( Display student records with BMI > 25 )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
     
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
     
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
     
      public static void Display_bmi_above_25(Student[] studentRec){
        int index = 0;
        int count = 0;
        float weight;
        float height;
        float bmi ;
        while(index < studentRec.length){
          weight = studentRec[index].get_weight();
          height = studentRec[index].get_height();
          bmi =  weight*10000/(height*height);
          if(bmi > 25){
            count += 1;
  System.out.println("----"+count+"----");
            System.out.println("Student name : "+studentRec[index].get_name());
            System.out.println("ID : "+studentRec[index].get_idNumber());
            System.out.print("weight : "+studentRec[index].get_weight()+" Kg ");
            System.out.println("Height : "+studentRec[index].get_height()+" cm");
            System.out.println("BMI : "+bmi);
            System.out.println("");
          }
          index += 1;
        }      
      }
     
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          Display_bmi_above_25(studentRec);
        }
    }

Lab 8 Java Generic 5 ( Find average age of students )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void find_avg_age(Student[] studentRec){
        int index = 0;
        float avg = 0;
        while(index < studentRec.length){
          avg += studentRec[index].get_age();
          index += 1; 
        }        
       avg /= studentRec.length; 
       System.out.println("Average age of students : " + avg + " years");
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          find_avg_age(studentRec);
        }
    }

Lab 8 Java Generic 6 ( Find/count number of students with age < 30 )
     public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void count_below_30_year(Student[] studentRec){
        int index = 0;
        int count = 0;
        while(index < studentRec.length){
          if(studentRec[index].get_age() < 30)
            count += 1;
          index +=1;
        }        
       System.out.println("Number of students with age < 30 is "+count);
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          count_below_30_year(studentRec);
        }
    }
        
Lab 8 Java Generic 7 ( Display student records, sorted by age, use insertion sort )
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void count_below_30_year(Student[] studentRec){
        int index1 = 0;
        int temp2;
    int[] temp = new int[studentRec.length];
        while(index1 < studentRec.length ){
     temp[index1] = studentRec[index1].get_age();
         index1 += 1;
        }
    index1 = 0;
        while(index1 < temp.length){
          int index2 = index1 + 1;
          while(index2 < temp.length){
            if(temp[index1] > temp[index2]){
                temp2 = temp[index1];
                temp[index1] = temp[index2];
                temp[index2] = temp2;
            }
            index2 += 1;
          }
          index1 += 1;
        }
        index1 = 0;
        System.out.println("---Sorted by age---");
        int count = 0;
        while(index1 < studentRec.length){
          int index2 = 0;
          boolean stop = false;
          while((index2 < temp.length) && ! stop){
            if(temp[index1] == studentRec[index2].get_age()){
              System.out.println("---"+(count+1)+"---");
              System.out.println("Student name : "+studentRec[index2].get_name() );
              System.out.println("ID : "+studentRec[index2].get_idNumber() );
              System.out.println("Age : "+studentRec[index2].get_age()+" years");
              System.out.println("weight : "+studentRec[index2].get_weight()+" Kg");
              System.out.println("Height : "+studentRec[index2].get_height()+" cm");
              System.out.println("");
              stop = true;
              count += 1;
            }
          index2 += 1;
          }
        index1 += 1;
      }
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          count_below_30_year(studentRec);
        }

    }

Lab 8 Java Generic 8 ( Find minimum weight of students )      
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void find_minimum_weight(Student[] studentRec){
        int index = 0;
    int ref_value = studentRec[index].get_weight();
        while(index < studentRec.length){
          if(ref_value > studentRec[index].get_weight())
            ref_value = studentRec[index].get_weight();
          index += 1;
        }
      System.out.println("Minimum weight of students is "+ref_value);
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Andy" , 44592 , 19 , 59 , 168 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          find_minimum_weight(studentRec);
        }
    }

Lab 8 Java Generic 9 ( Find/count number of students with weight < 50 )      
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void count_below_weight_50(Student[] studentRec){
        int index = 0;
    int count = 0;
        while(index < studentRec.length){
          if(50 > studentRec[index].get_weight())
            count += 1;
          index += 1;
        }
        System.out.println("Number of students with weight < 50 is "+count);
      }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Mabel" , 44592 , 19 , 49 , 155 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          count_below_weight_50(studentRec);
        }
    }

Lab 8 Java Generic 10 ( Display student records with weight < 50 )     
    public class Student{
      private String name;
      private int idNumber;
      private int age;
      private int weight;
      private int height;
      
      public Student(String name,int idNumber,int age,int weight,int height){
        this.name = name;
        this.idNumber = idNumber;
        this.age = age;
        this.weight = weight;
        this.height = height;
      }
      
      public String get_name(){
        return this.name;
      }
      public int get_idNumber(){
        return this.idNumber;
      }
      public int get_age(){
        return this.age;
      }
      public int get_weight(){
        return this.weight;
      }
      public int get_height(){
        return this.height;
      }
      
      public static void display_weight_below_50_data(Student[] studentRec){
        int index = 0;
    System.out.println("--Student records with weight < 50--");
        while(index < studentRec.length){
        if(50 > studentRec[index].get_weight()){
            System.out.println("---"+(index+1)+"---");
            System.out.println("Student name : "+studentRec[index].get_name());
            System.out.println("ID : "+studentRec[index].get_idNumber());
            System.out.println("Age : "+studentRec[index].get_age()+" years");
            System.out.println("weight : "+studentRec[index].get_weight()+" Kg");
            System.out.println("Height : "+studentRec[index].get_height()+" cm");
            System.out.println("");
            }
        index += 1;
         }
       }
      
        public static void main(String[] args){
          Student[] studentRec = { new Student( "Mabel" , 44592 , 19 , 49 , 155 ),
                                        new Student( "Mike" , 44593 , 32 , 65 , 173 ),
                                               new Student( "Nick" , 44594 , 27 , 61 , 165 ),
                                               new Student( "Jack" , 44595 , 35 , 68 , 158 ),
                                               new Student( "Lucy" , 44596 , 55 , 75 , 175),  
                                               new Student( "Max" , 44597 , 18 , 56 , 163 ),
                                        new Student( "Luck" , 44598 , 20 , 58 , 169 )  };
          display_weight_below_50_data(studentRec);
        }
    }

Lab 8 Java Generic 11 ( Create class Banner for printing a banner 1 )     
    public class Alphabet{
      private String Line1;
      private String Line2;
      private String Line3;
      private String Line4;
      private String Line5;
      
      public Alphabet(String Line1,String Line2,String Line3,String Line4,String Line5){
        this.Line1 = Line1;
        this.Line2 = Line2;
        this.Line3 = Line3;
        this.Line4 = Line4;
        this.Line5 = Line5;
      }
      
      public void display_alphabet_L1(){
        System.out.print(this.Line1);
      }
      public void display_alphabet_L2(){
        System.out.print(this.Line2);
      }
      public void display_alphabet_L3(){
        System.out.print(this.Line3);
      }
      public void display_alphabet_L4(){
        System.out.print(this.Line4);
      }
      public void display_alphabet_L5(){
        System.out.print(this.Line5);
      }
      
      public static void draw_alphabet(Alphabet[] alphabet_group,String word){
        int index_print = 0;
        while(index_print < 5){
          int index_word = 0;
          while(index_word < word.length()){
              if(word.charAt(index_word) == 'N'){
                //System.out.print("1");
                print_func(alphabet_group,index_print,0);
              }
              else if(word.charAt(index_word) == 'O'){
                //System.out.print("2");
                print_func(alphabet_group,index_print,1);
              }
              else if(word.charAt(index_word) == 'B'){
                //System.out.print("3");
                print_func(alphabet_group,index_print,2);
              }
            index_word += 1;
          }
        System.out.println(""); 
        index_print += 1 ;
       }
      }
      
       public static void print_func(Alphabet[] alphabet_group,int index_print,int alphabet_index){
          if(index_print == 0){
            alphabet_group[alphabet_index].display_alphabet_L1();
          }
          else if(index_print == 1){
            alphabet_group[alphabet_index].display_alphabet_L2();
          }
          else if(index_print == 2){   
            alphabet_group[alphabet_index].display_alphabet_L3();
          }
          else if(index_print == 3){   
            alphabet_group[alphabet_index].display_alphabet_L4();
          }
          else if(index_print == 4){   
            alphabet_group[alphabet_index].display_alphabet_L5();  
          }
        }
        
        public static void main(String[] args){
          Alphabet[] alphabet_group  = { new Alphabet(
             " #     # ",
                                                      " ##   # ",
                                                      " # #  # ",
                                                      " #  # # ",
                 " #   ## " ),
                                        new Alphabet(
                                                      "  ####  ",
                                                      " #       # ",
                                                      " #       # ",
                                                      " #       # ",
                                                      "  ####  " ),
                                                        new Alphabet(
                                                          " #####  ",
                                                          " #       #  ",
                                                          " #####  ",
                                                          " #        # ",
                                                          " #####  " ) };
          draw_alphabet(alphabet_group,"NOOB");
        }
                                        
      }

Lab 8 Java Generic 12 ( Create class Banner for printing a banner 2 )   
public class Alphabet{ private String Line1; private String Line2; private String Line3; private String Line4; private String Line5; public Alphabet(String Line1,String Line2,String Line3,String Line4,String Line5){ this.Line1 = Line1; this.Line2 = Line2; this.Line3 = Line3; this.Line4 = Line4; this.Line5 = Line5; } public void display_alphabet_L1(){ System.out.print(this.Line1); } public void display_alphabet_L2(){ System.out.print(this.Line2); } public void display_alphabet_L3(){ System.out.print(this.Line3); } public void display_alphabet_L4(){ System.out.print(this.Line4); } public void display_alphabet_L5(){ System.out.print(this.Line5); } public void display_star_alphabet_L1(){ System.out.print(this.Line1); } public void display_star_alphabet_L2(){ System.out.print(this.Line2); } public void display_star_alphabet_L3(){ System.out.print(this.Line3); } public void display_star_alphabet_L4(){ System.out.print(this.Line4); } public void display_star_alphabet_L5(){ System.out.print(this.Line5); } public String get_alphabet_L1(){ return this.Line1; } public String get_alphabet_L2(){ return this.Line2; } public String get_alphabet_L3(){ return this.Line3; } public String get_alphabet_L4(){ return this.Line4; } public String get_alphabet_L5(){ return this.Line5; } public static void draw_alphabet_star(Alphabet[] alphabet_group,String print){ int index = 0; while( index < print.length() ){ if (print.charAt(index) == '#'){ System.out.print("*"); } else{ System.out.print(" "); } index += 1; } } public static void draw_alphabet(Alphabet[] alphabet_group,String word){ int index_print = 0; while(index_print < 5){ int index_word = 0; while(index_word < word.length()){ if(word.charAt(index_word) == 'N'){ //System.out.print("1"); print_func(alphabet_group,index_print,0); } else if(word.charAt(index_word) == 'O'){ //System.out.print("2"); print_func(alphabet_group,index_print,1); } else if(word.charAt(index_word) == 'B'){ //System.out.print("3"); print_func(alphabet_group,index_print,2); } index_word += 1; } System.out.println(""); index_print += 1 ; } } public static void print_func(Alphabet[] alphabet_group,int index_print,int alphabet_index){ if(index_print == 0){ draw_alphabet_star(alphabet_group,alphabet_group[alphabet_index].get_alphabet_L1()); } else if(index_print == 1){ draw_alphabet_star(alphabet_group,alphabet_group[alphabet_index].get_alphabet_L2()); } else if(index_print == 2){ draw_alphabet_star(alphabet_group,alphabet_group[alphabet_index].get_alphabet_L3()); } else if(index_print == 3){ draw_alphabet_star(alphabet_group,alphabet_group[alphabet_index].get_alphabet_L4()); } else if(index_print == 4){ draw_alphabet_star(alphabet_group,alphabet_group[alphabet_index].get_alphabet_L5()); } } public static void main(String[] args){ Alphabet[] alphabet_group = { new Alphabet( " # # ", " ## # ", " # # # ", " # # # ", " # ## " ), new Alphabet( " #### ", " # # ", " # # ", " # # ", " #### " ), new Alphabet( " ##### ", " # # ", " ##### ", " # # ", " ##### " ) }; draw_alphabet(alphabet_group,"NOOB"); } }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น