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

Lab 8 - (Java) Display student records, sorted by age, use insertion sort

public class Student {

  private String name;
  private int weight;
  private int high;
  private int age;
  private int ID;

  public  Student(String name , int weight , int high , int age , int ID ) {
   
   this.name = name ;
   this.weight = weight;
   this.high = high;
   this.age = age;
   this.ID = ID;
   }

  public String get_name(){
    return this.name;
  }
  public int get_weight(){
    return this.weight;
  }
  public int get_high(){
    return this.high;
  }
  public int get_age(){
    return this.age;
  }
  public int get_ID(){
    return this.ID;
  }
  public void set_name(String value){
    this.name = value;
  }
  public void set_weight(int  value){
    this.weight =  value;
  }
  public void set_high(int  value){
    this.high = value;
  }
  public void set_age(int  value){
    this.age =  value;
  }
  public void set_ID(int  value){
    this.ID = value;
  }

  public void display() {

    System.out.println( "NAME: "+this.name);
    System.out.println( "WEIGHT:"+this.weight+"  kg.");
    System.out.println( "HIGH:"+this.high+"  cm.");
    System.out.println( "AGE :"+this.age+"  years old");
    System.out.println( "ID :"+this.ID);

 }
  public static void main(String[] args) {
    Student[] std = {new Student("Park",40,160,18,1),
                    new Student("Nani",50,180,19,2),
                     new Student("Pepe",80,185,20,3),
                     new Student("Giggs",70,160,18,4),
                     new Student("Laws",85,155,18,5)};
    sort_age(std);
    int index = 0;
 
    while(index<std.length){
      std[index].display();
      index = index + 1;
    }
 
  }
   public static void sort_age(Student[] std){
    int index  = 1 ;
    while(index<std.length){
      String value_name = std[index].get_name();
      int value_weight = std[index].get_weight();
      int value_high = std[index].get_high();
      int value_age = std[index].get_age();
      int value_ID = std[index].get_ID();
      int pos = index ;
      while(pos>0 &&std[pos - 1].get_age()>value_age){
         std[pos].set_name(std[pos - 1].get_name());
         std[pos].set_weight(std[pos - 1].get_weight());
         std[pos].set_high(std[pos - 1].get_high());
         std[pos].set_age(std[pos - 1].get_age());
         std[pos].set_ID(std[pos - 1].get_ID());
         pos = pos - 1;
      }
      if(pos != index){
         std[pos].set_name(value_name);
         std[pos].set_weight(value_weight);
         std[pos].set_high(value_high);
         std[pos].set_age(value_age);
         std[pos].set_ID(value_ID);
      }
     index = index + 1;
    }
   
   }
}

Lab 8 - (Java) Find minimum weight of students

public class Student {
  private String name;
  private int weight;
  private int high;
  private int age;
  private int ID;
  public  Student(String name ,int weight , int high , int age , int ID) {
    this.name = name ;
    this.weight = weight;
    this.high = high;
    this.age = age;
    this.ID = ID;
   }
   public int get_weight(){
    return this.weight;
  }
  public void display() {
    System.out.println( "NAME: "+this.name);
    System.out.println( "WEIGHT:"+this.weight+"  kg.");
    System.out.println( "HIGH:"+this.high+"  cm.");
    System.out.println( "AGE :"+this.age+"  years old");
    System.out.println( "ID :"+this.ID);
}
public static void main(String[] args) {
    Student[] std = {new Student("Park",40,160,18,1),
                    new Student("Nani",50,180,19,2),
                     new Student("Pepe",80,185,20,3),
                     new Student("Giggs",70,160,18,4),
                     new Student("Laws",85,155,18,5)
                    };
    int index = 0;
     while(index<std.length){
      std[index].display();
      index = index + 1;

    }
 System.out.println("The number of students that have weight less than 50 kg. is "+count_weight(std));
   System.out.println( "The minimum weight is  " +find_minimum_weight( std)+"  kg. ");
    }
   public static int count_weight(Student[] std){
    int index = 0 ;
    int count = 0 ;
      while(index<std.length){
      if(std[index].get_weight()<50){
          count +=1 ;
      }
          index = index + 1 ;
      }
        return count    ;
      }
  public static int find_minimum_weight(Student[] std){
    int  index = 0;
    int weight = 100;
    while(index<std.length){
      if(std[index].get_weight()<weight){
        weight = std[index].get_weight();
      }
      index = index + 1;
    }
    return weight;
  }
    }

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

Lab Class - Display Complex

class Complex:
   def __init__(self,real,imagine=0):
      self.real = real
      self.imagine = imagine
   
   def get_real(self):
      return self.real
 
   def get_imagine(self):
      return self.imagine
 
   def display_result(self):
      i = self.imagine
      if i>0 :
         print(self.real,end="+ ")
      else :
         print(self.real,end=" ")
      print(self.imagine,end="i")
      print()
 
def setup():
   a = Complex(2,-1)
   b = Complex(2,-4)
   a.display_result()
   b.display_result()
 
setup()
 

Lab Class - Plus Complex

class Complex:
   def __init__(self,real,imagine=0):
      self.real = real
      self.imagine = imagine
   
   def get_real(self):
      return self.real
 
   def get_imagine(self):
      return self.imagine
 
   def display_result(self):
      i = self.imagine
      if i>0 :
         print(self.real,end="+ ")
      else :
         print(self.real,end=" ")
      print(self.imagine,end="i")
      print()
   
   def add_result(self,areal,aimagine):
      self.real = self.real + areal
      self.imagine = self.imagine + aimagine
      i = self.imagine
      if i>0 :
         print(self.real,end="+ ")
      else :
         print(self.real,end=" ")
      print(self.imagine,end="i")
      print()
 
def setup():
   a = Complex(2,-1)
   b = [2,-4]
   a.add_result(b[0],b[1])
setup()
 

วันพุธที่ 4 พฤศจิกายน พ.ศ. 2558

Lab Raspberry pi - Report, Error

Program very slowly progress

Lab Raspberry pi - Python Grade

class Student:
    def __init__(self,name,idnum,score):
        self.name = name
        self.id = idnum
        self.score = score

# get method
    def get_id(self):
        return self.idnum
    def get_score(self):
        return self.score

#set method
    def set_score(self,value):
        self.score = value
       
#display mathod
    def display_info(self):
        print("Name :", self.name)
        print("ID :", self.id)
        print("Score :", self.score)

def show_all(student):
    i = 0
    while i<len(student):
        print("Grade",find_grade(student[i]))
        student[i].display_info()
        print()
        i = i + 1
           

def show_grade(student):
    i = 0
    while i<len(student) :
        print("Grade = ",find_grade(student[i]))
        i = i+1

def count_grade(student):
 
    i = 0
    a = 0
    b = 0
    c = 0
    d = 0
    f = 0
 
    while i<len(student) :
        if find_grade(student[i]) == "A":
            a = a + 1
        elif find_grade(student[i]) == "B":
            b = b + 1
        elif find_grade(student[i]) == "C":
            c = c + 1
        elif find_grade(student[i]) == "D":
            d = d + 1
        elif find_grade(student[i]) == "F":
            f = f + 1
        i = i + 1
    print("There is", a, "people have grade A")
    print("There is", b, "people have grade B")
    print("There is", c, "people have grade C")
    print("There is", d, "people have grade D")
    print("There is", f, "people have grade E")
         

def find_grade(student):
 
    score = student.get_score()
    if(score<50):
        return "F"
    elif(score<60):
        return "D"
    elif(score<70):
        return "C"
    elif(score<80):
        return "B"
    else :
        return "A"

def setup():
    student = [ Student("Nut",50001, 99),
                Student("Fok",50002, 49),
                Student("Gase",50003, 75),
                Student("Pee", 50004, 68),
                Student("Mind", 50005, 55)]
    show_grade(student)
    print()
    count_grade(student)
    print()
    show_all(student)
         
setup()

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

Lab 8 - (Java) Find/count number of students with age < 30


public class Student {

    private String name;
    private int id;
    private int age;
    private int weight;
    private int height;

    public Student(String name, int id, int age, int weight, int height) {

        this.name = name;
        this.id = id;
        this.age = age;
        this.weight = weight;
        this.height = height;

    }

    public void display() {
        System.out.println(this.name);
        System.out.println(this.id);
        System.out.println(this.age);
        System.out.println(this.weight);
        System.out.println(this.height);

    }

    public int get_age() {
        return this.age;

    }

    public static void main(String[] args) {

        Student[] info = {new Student("Bun Tun", 00001, 18, 68, 149),
            new Student("FlukeKnub", 00002, 17, 50, 179),
            new Student("BasBomba", 00003, 20, 44, 188),
            new Student("GaseKiki", 00004, 21, 67, 180),
            new Student("PeeJa", 00005, 15, 88, 155)};

        age_average(info);

    }

    public static void age_average(Student[] e) {

        int count = 0;
        int i = 0;

        while (i < e.length) {

            if (e[i].get_age() < 30) {
                e[i].display();
                System.out.println();
                count = count + 1;
            }
            i = i + 1;
        }

        System.out.println(count);
    }

}

Lab 8 - (Java) Find average age of students


public class Student {

    private String name;
    private int id;
    private int age;
    private int weight;
    private int height;

    public Student(String name, int id, int age, int weight, int height) {

        this.name = name;
        this.id = id;
        this.age = age;
        this.weight = weight;
        this.height = height;

    }

    public void display() {
        System.out.println(this.name);
        System.out.println(this.id);
        System.out.println(this.age);
        System.out.println(this.weight);
        System.out.println(this.height);

    }

    public int get_age() {
        return this.age;

    }

    public static void main(String[] args) {

        Student[] info = {new Student("Bun Tun", 00001, 18, 68, 149),
            new Student("FlukeKnub", 00002, 17, 50, 179),
            new Student("BasBomba", 00003, 20, 44, 188),
            new Student("GaseKiki", 00004, 21, 67, 180),
            new Student("PeeJa", 00005, 15, 88, 155)};

        age_average(info);

    }

    public static void age_average(Student[] e) {

        int sum = 0;
        int i = 0;
        int average = 0;

        while (i < e.length) {

            sum = sum + e[i].get_age();
            i = i + 1;

        }
        average = sum / e.length;
        System.out.println(average);
    }

}

Lab 8 - (Java) Find/count number of students with BMI > 25


public class Student {

    private String name;
    private int id;
    private int age;
    private int weight;
    private int height;

    public Student(String name, int id, int age, int weight, int height) {

        this.name = name;
        this.id = id;
        this.age = age;
        this.weight = weight;
        this.height = height;

    }

    public void display() {
        System.out.println(this.name);
        System.out.println(this.id);
        System.out.println(this.age);
        System.out.println(this.weight);
        System.out.println(this.height);

    }

    public float get_weight() {
        return this.weight;

    }

    public float get_height() {
        return this.height;

    }

    public static void main(String[] args) {

        Student[] info = {new Student("Bun Tun", 00001, 18, 68, 149),
            new Student("FlukeKnub", 00002, 17, 50, 179),
            new Student("BasBomba", 00003, 20, 44, 188),
            new Student("GaseKiki", 00004, 21, 67, 180),
            new Student("PeeJa", 00005, 15, 88, 155)};

        bmi(info);

    }

    public static void bmi(Student[] e) {

        int count = 0;
        int i = 0;
        float bmi = 0;
        while (i < e.length) {
            bmi = e[i].get_weight() / ((e[i].get_height() / 100) * (e[i].get_height() / 100));

            if (bmi > 25) {
                e[i].display();
                System.out.println();
                count = count + 1;
            }

            i = i + 1;

        }
        System.out.println(count);
    }

}

Lab 8 - (Java) Find student BMI

public class Student {

    private String name;
    private int id;
    private int age;
    private int weight;
    private int height;

    public Student(String name, int id, int age, int weight, int height) {

        this.name = name;
        this.id = id;
        this.age = age;
        this.weight = weight;
        this.height = height;

    }

    public float get_weight () {
       return this.weight;

    }
 
    public float get_height () {
        return this.height;

    }

    public static void main(String[] args) {

        Student[] info = {new Student("Bun Tun", 00001, 18, 68, 149),
            new Student("FlukeKnub", 00002, 17, 50, 179),
            new Student("BasBomba", 00003, 20, 44, 188),
            new Student("GaseKiki", 00004, 21, 67, 180),
            new Student("PeeJa", 00005, 15, 88, 155)};

        bmi(info);

    }

    public static void bmi(Student[] e) {

        int i = 0;
        float bmi = 0;
        while (i < e.length) {
            bmi = e[i].get_weight()/((e[i].get_height ()/100)*(e[i].get_height ()/100));
            System.out.println(bmi);

            i = i + 1;

        }
     
    }

}

Lab 8 - (Java) Display each student record

public class student {

  private String name;
private int id;
private int age;
private int weight;
private int height;

public student(String name, int id, int age, int weight, int height) {

this.name = name;
this.id = id;
this.age = age;
this.weight = weight;
this.height = height;

}

public void display() {
   System.out.println( this.name );
   System.out.println( this.id );
   System.out.println( this.age );
   System.out.println( this.weight );
   System.out.println( this.height );

}

public static void main(String[] args) {

  student[] info = {  new student("Bun Tun", 00001, 18, 68, 149),
                              new student("FlukeKnub", 00002, 17, 50, 179),
                              new student("BasBomba", 00003, 20, 44, 188),
                              new student("GaseKiki", 00004, 21, 67, 180),
                              new student("PeeJa",00005, 15, 88, 155)};

display_info(info);

}

public static void display_info(student[] e) {

   int i = 0;
 
   while ( i < e.length) {
      e[i].display();
      System.out.println();

      i = i + 1;

      }

   }

}

วันเสาร์ที่ 31 ตุลาคม พ.ศ. 2558

Lab 7 - (Class) Insertion Sort Planning

def sort_insertion(my_list):
    i = 1
    while i < len(my_list):

        value_current = my_list[i]
        pos = i

        while((pos > 0) and (my_list[pos-1] > value_current)):
            my_list[pos] = my_list[pos-1]
            pos = pos-1
           
        if (pos != i):
            my_list[pos] = value_current
        i = i + 1
       
    return my_list
   

def setup():
   
    my_list = [15,26,28,17,77,31,44,55,20]
    print(my_list)
    print(sort_insertion(my_list))
   
setup()

Lab 7 - (Class) Display student records, sorted by age, use insertion sort

class student:

    def __init__(self,order,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
    def get_name(self):
        return self.name
    def get_weight(self):
        return self.weight
    def get_height(self):
        return self.height
       
   
    def set_age(self,value):
        self.age = value
        return self.age
    def set_name(self,value):
        self.name = value
        return self.name
    def set_weight(self,value):
        self.weight = value
        return self.weight  
    def set_height(self,value):
        self.height = value
        return self.height  
       
   
def setup():
    i = 0
    info = [student(1,"Buntun",18,54,160),
            student(2,"FlukeKnub",19,75,199),
            student(3,"PeeJa",32,60,150),
            student(4,"Nutdech",18,51,185),
            student(5,"BasBomba",20,65,180)]
    sort_insertion(info)
   
    while (i<len(info)):
        info[i].display()
        i = i+1
   
       
def sort_insertion(info):
    i = 1
    while i < len(info):

        value_current_age = info[i].get_age()
        value_current_name = info[i].get_name()
        value_current_weight = info[i].get_weight()
        value_current_height = info[i].get_height()
        pos = i

        while((pos > 0) and (info[pos-1].get_age() > value_current_age)):
            info[pos].set_age(info[pos-1].get_age())
            info[pos].set_name(info[pos-1].get_name())
            info[pos].set_weight(info[pos-1].get_weight())
            info[pos].set_height(info[pos-1].get_height())
            pos = pos-1
           
        if (pos != i):
            info[pos].set_age(value_current_age)
            info[pos].set_name(value_current_name)
            info[pos].set_weight(value_current_weight)
            info[pos].set_height(value_current_height)
           
        i = i + 1
       
    return info
   
setup()

Lab 7 - (Class) Find minimum weight of students

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,54,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,51,185),
            student("BasBomba",20,65,180)]
    find_weight(info)
       
def find_weight(info):
    min = 0
    i = 1
    count = 0
    while (i<len(info)):
        weight_min = info[min].get_weight()
        weight = info[i].get_weight()
        if (weight_min > weight):
           weight_min = weight
           count = i
        i = i+1
    info[count].display()
   
setup()

Lab 7 - (Class) Display student records with weight < 50

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,43,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_weight(info)
       
def find_weight(info):
    i = 0
    count = 0
    while (i<len(info)):
        weight = info[i].get_weight()
        if (weight < 50):
           info[i].display()
        i = i+1
    print()
   
setup()

Lab 7 - (Class) Find/count number of students with weight < 50

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,43,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_weight(info)
       
def find_weight(info):
    i = 0
    count = 0
    while (i<len(info)):
        weight = info[i].get_weight()
        if (weight < 50):
           info[i].display()
           count = count+1
        i = i+1
    print()
    print ("Number member has weight less than 50 =",count)
   
setup()

Lab 7 - (Class) Find/count number of students with age < 30

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_age(info)
       
def find_age(info):
    i = 0
    count = 0
    while (i<len(info)):
        age = info[i].get_age()
        if (age < 30):
           info[i].display()
           count = count+1
        i = i+1
    print()
    print ("Number member has age less than 30 =",count)
   
setup()

Lab 7 - (Class) Find average age of students

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    average_age(info)
    print ("Summary age =", average_age(info))
   
def average_age(info):
    i = 0
    sum_age = 0
    while(i<len(info)):
       sum_age = sum_age+info[i].get_age()
       i = i+1
    average = sum_age/len(info)
    return average
   
setup()

Lab 7 - (Class) Display student records with BMI > 25

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
    i = 0
    sum = 0
    while(i<len(info)):
        bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
        if ( bmi > 25):
            info[i].display()
            print(bmi)
            print()
        i = i+1
   
setup()

Lab 7 - (Class) Find/count number of students with BMI > 25

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
    i = 0
    sum = 0
    while(i<len(info)):
        bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
        if ( bmi > 25):
            info[i].display()
            print(bmi)
            print()
            sum = sum+1
        i = i+1
    print ("Number member who has bmi more than 25 is",sum )

setup()

Lab 7 - (Class) Find student BMI while version

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,160),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
   i=0
   while(i<len(info)):
      bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
      info[i].display()
      print("BMI is", bmi)
      print()
      i=i+1

setup()

วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2558

Lab 7 - (Class) Find student BMI

class student:
 
    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height
 
    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
     
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
     
def setup():
    a = student("Buntun",18,50,160)
    b = student("FlukeKnub",19,75,199)
    c = student("PeeJa",19,60,160)
    d = student("Nutdech",18,58,185)
    e = student("BasBomba",20,60,180)
 
    a.display(),print(cal_bmi(a))
    print()
    b.display(),print(cal_bmi(b))
    print()
    c.display(),print(cal_bmi(c))
    print()
    d.display(),print(cal_bmi(d))
    print()
    e.display(),print(cal_bmi(e))
    print()
 
   
def cal_bmi(student):
    bmi = student.get_weight()/((student.get_height()/100)*(student.get_height()/100))
    return bmi
 
setup()
 

Lab 7 - (Class) Display each student record

class student:
 
    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height
 
    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
     
def setup():
    a = student("Buntun",18,50,160)
    b = student("FlukeKnub",19,75,199)
    c = student("PeeJa",19,60,160)
    d = student("Nutdech",18,58,185)
    e = student("BasBomba",20,60,180)
 
    a.display()
    b.display()
    c.display()
    d.display()
 
setup()
 

Lab 6 - Multiply two matrices

def setup():
   row_11=[1,2]
   row_12=[3,4]
   matrix_1=[row_11,row_12]
   row_21=[5,6]
   row_22=[7,8]
   matrix_2=[row_21,row_22]
   add_matrix(matrix_1,matrix_2)
 
def add_matrix(matrix_1,matrix_2):
 
   i=0
   j=0
   k=0
 
   add1=0
   add2=0
   add=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         add1 = matrix_1[i][k]*matrix_2[k][j]
         add2 = matrix_1[i][k+1]*matrix_2[k+1][j]
         add = add1+add2
         print(add,end=" ")
         j = j+1
      j = 0
      print("|")
      i = i+1
     
setup()

Lab 6 - Write a function that takes a string as an argument and prints out the string in large letters

def setup():
   word=input()
   display(word)

def display(word):
   j1="#######"
   j2="   #   "
   j3="#  #   "
   j4="  #    "
   j=[j1,j2,j3,j4]
   a1="  # #  "
   a2=" #   # "
   a3="#######"
   a4="#     #"
   a=[a1,a2,a3,a4]
   v1="#     #"
   v2=" #   # "
   v3="  # #  "
   v4="   #   "
   v=[v1,v2,v3,v4]
   i=0
   j=0
   while (i < len(n)): // Check by side
      while (j < len(word)): //  Check by column
         if (word[j] == 'j'):
            print(n[i] , end=" ")
         if(word[j]=='a'):
            print(u[i] , end=" ")
         if (word[j]=='v'):
            print(t[i] , end=" ")
         j=j+1
      j=0
      print()
      i=i+1

setup()

Lab 6 - Transpose a square matrix in place without creating a new matrix.

def setup():
   row_11 = [44,10]
   row_12 = [32,21]
   matrix = [row_11,row_12]
   transpose_matrix(matrix)
 
def transpose_matrix(matrix):
   
   i=0
   j=0
   while(i<len(matrix)):
      print("|",end=" ")
      while(j<len(matrix[i])):
         print(matrix[j][i],end=" ")
         j = j+1
      j = 0
      print("|")
      i = i+1
     
setup()

Lab 6 - Subtract two matrices

def setup():
   row_11=[44,10]
   row_12=[32,21]
   matrix_1=[row_11,row_12]
   row_21=[0,0]
   row_22=[3,33]
   matrix_2=[row_21,row_22]
   new_matrix(matrix_1,matrix_2)
 
def new_matrix(matrix_1,matrix_2):
   i=0
   j=0
   result=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         result = matrix_1[i][j]-matrix_2[i][j]
         print(result ,end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
     
setup()

Lab 6 - Add two matrices

def setup():
   row_11=[44,10]
   row_12=[32,21]
   matrix_1=[row_11,row_12]
   row_21=[0,0]
   row_22=[3,33]
   matrix_2=[row_21,row_22]
   new_matrix(matrix_1,matrix_2)
 
def new_matrix(matrix_1,matrix_2):
   i=0
   j=0
   result=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         result = matrix_1[i][j]+matrix_2[i][j]
         print(result ,end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
     
setup()

Lab 6 - Display the matrix

def setup():
   row_11=[1,5]
   row_12=[3,0]
   matrix_1=[row_11,row_12]
   display_matrix(matrix_1)
 
def display_matrix(matrix):
   i=0
   j=0
   while(i<len(matrix)):
      print("|",end=" ")
      while(j<len(matrix[i])):
         print(matrix[i][j],end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
setup()

Lab 6 - Find index of the floor(s) with maximum number of chairs (in the building)

def setup():
   floor_1=[30,31,33,20,34]
   floor_2=[50,33,23,45,53]
   floor_3=[65,34,21,34,14]
   building=[floor_1,floor_2,floor_3]
   find_max_chairs(building)
   print("Floor have the most of chairs = ",find_max_chairs(building))
 
def find_max_chairs(building):
   i=0
   j=0
   maximum_chair=0
   total=0
   floor=0
   while(i<len(building)):
      while(j<len(building[i])):
         total=total+building[i][j]
         j=j+1
      j=0
      if(total>maximum_chair):
         maximum_chair=total
         floor=i+1
      total=0
      i=i+1
   return floor
 
setup()

Lab 6 - Find total number of chairs (in the building)

def setup():
   floor_1=[30,31,33,20,34]
   floor_2=[50,33,23,45,53]
   floor_3=[65,34,21,34,14]
   building=[floor_1,floor_2,floor_3]
   find_total(building)
   print("total of chairs = ",find_total(building))
 
def find_total(building):
   i=0
   j=0
   total=0
   while(i<len(building)):
      while(j<len(building[i])):
         total=total+building[i][j]
         j=j+1
      j=0
      i=i+1
   return total
 
setup()

Lab 6 - Find minimum weight, weight < 50, Display records

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,23,26,21]
   weight=[43,55,60,54,63]
   height=[175,171,187,181,183]
   find_weight(student_id,names,age,weight,height)
 
def find_weight(student_id,names,age,weight,height):
   i=0
   minimun_weight=weight[1]
   count=0
   while(i<len(weight)):
      if(weight[i]<minimun_weight):
         minimun_weight=weight[i]
      if(weight[i]<50):
         count=count+1
         print(student_id[i],"  ",names[i],"       ",weight[i],"  kg")
      i=i+1
   print("student have weight less than 50 = ",count)
 
setup()

Lab 6 - Display student records, sorted by age, use insertion sort

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,23,26,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   sort_age(student_id,names,age,weight,height)
   display(student_id,names,age,weight,height)

def sort_age(student_id,names,age,weight,height):
    i=1
    while(i<len(student_id)):
      new_s=student_id[i]
      new_n=names[i]
      new_a=age[i]
      new_w=weight[i]
      new_h=height[i]
      j=i
      while(j>0 and age[j-1]>new_a):
         student_id[j]=student_id[j-1]
         names[j]=names[j-1]
         age[j]=age[j-1]
         weight[j]=weight[j-1]
         height[j]=height[j-1]
         j=j-1
      student_id[j]=new_s
      names[j]=new_n
      age[j]=new_a
      weight[j]=new_w
      height[j]=new_h
      i=i+1
     
def display(student_id,names,age,weight,height):
    i=0
    print("ID  ","names     ","age    ","weight  ","height")
    while(i<len(student_id)):
        print(student_id[i],"  ",names[i],"    ",age[i],"       ",weight[i],"       ",height[i])
        print()
        i=i+1

setup()

Lab 6 - Find average age of students

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_average_age(student_id,names,age,weight,height)

def find_average_age(student_id,names,age,weight,height):
    i=0
    summation=0
   
    while(i<len(student_id)):
      summation=summation+age[i]
      i=i+1
    print("Average Age = ",summation/len(student_id) )
    return

setup()

Lab 6 - Find/count number of students with age < 30

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_age(student_id,names,age,weight,height)

def find_age(student_id,names,age,weight,height):
    i=0
    count=0
    while(i<len(student_id)):
      if(age[i]<30):
         print(student_id[i],"   ",names[i],"     ",age[i],"   year old")
         count=count+1
      i=i+1
    print("number student have age less than 30 = ",count)  

setup()

Lab 6 - Find student BMI

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   cal_bmi(student_id,names,age,weight,height)

def cal_bmi(student_id,names,age,weight,height):
    i=0
    bmi=0
    count=0
    while(i<len(student_id)):
      bmi=weight[i]/((height[i]/100)*2)
      print(student_id[i],"  ",names[i],"   ",bmi)
      i=i+1
setup()

Lab 6 - Find/count number of students with BMI > 25

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_bmi(student_id,names,age,weight,height)

def find_bmi(student_id,names,age,weight,height):
    i=0
    bmi=0
    count=0
    while(i<len(student_id)):
      bmi=weight[i]/((height[i]/100)*2)
      if(bmi>25):
         print(student_id[i],"  ",names[i],"   ",bmi)
         count=count+1
      i=i+1
    print("number student have BMI more than 25 = ",count)
setup()

Lab 6 - Display student records (data)

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   display(student_id,names,age,weight,height)
 
def display(student_id,names,age,weight,height):
   i=0  
   print("ID     ","names     ","age     ","weight     ","height")
   while(i<len(student_id)):
      print(student_id[i],"        ",names[i],"        ",age[i],"        ",weight[i],"          ",height[i])
      print()
      i=i+1
setup()

วันอาทิตย์ที่ 4 ตุลาคม พ.ศ. 2558

Lab 5 - (Function) my_endswith()

def my_endswith(word,check):
   i = len(word)-len(check)
   a = 0
   if(len(word)>=len(check)):
      while(i<len(word)):
         if(word[i] == check[a]):
            i = i + 1
            a = a + 1
         else:
            return False
      return True

my_endswith("thailand","land")
assert my_endswith("thailand","land")
assert my_endswith("thailand","thai") == False



Lab 5 - (Function) my_startswith ()

def my_startswith(word,check):
   i = 0
   while(i<len(check)):
       if(word[i] == check[i]):
           i = i + 1
       else:
           return False
   return True

my_startswith("Thailand","That")
assert my_startswith("Thailand","That")
assert my_startswith("Thailand","That") == False


Lab 5 - (Function) my_strip()

def my_replace(word):
   result=""
   i = 0
   while(i<len(word)):
        if(word[i] != " "):
           result = result + word[i]
        i = i+1
   return result
     
my_replace("Thailand    ")
assert (my_replace("Thailand    ") == "Thailand" )
print (my_replace("Thailand    "))


Lab 5 - (Function) my_find()

def my_find(word,charac):
   i = 0
   x = 0

   while(i<len(word)):
        if(charac == word[i]):
           x = i
           print(x)
           break
        i = i+1
       
my_find("Thailandi","i")


Lab 5 - (Function) my_count()

def my_count(word,check):
    i = 0
    count = 0
    while (i<len(word)):
        if (word[i] == check):
            count = count + 1
        i = i + 1
    print (count)
 
my_count("thailand","a")  


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

Lab 5 - Convert a number from base 10 to base 2

def convert_10_to_2():
    num = 29
    keep = ""
    r = 0
    while (num>0):
        r = num % 2
        num = num // 2
        keep = str(r) + keep
    print (keep)

convert_10_to_2()    
    

วันพุธที่ 23 กันยายน พ.ศ. 2558

Lab 5 - Decrease values in array (by percentage)

def decrease_percent(a):
    n = a
    i = 0
    dec_per = 0.5 # 50/100
    print("Number increase",dec_per*100,"%")
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] - n[i]*dec_per
        print("Decrease Array n[",i,"] =",n[i])
        print()
        i = i + 1

decrease_percent([5, 6, 7, 8])




Lab 5 - Increase values in array (by percentage)

def increase_percent(a):
    n = a
    i = 0
    inc_per = 0.5
    print("Number increase",inc_per*100,"%")
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] + n[i]*inc_per
        print("Increase Array n[",i,"] =",n[i])
        print()
        i = i + 1

increase_percent([5, 6, 7, 8])


Lab 5 - Decrease values in array (by fixed value)

def decrease_array(a):
    n = a
    i = 0
    dec = 5
    print("Decrease Number is",dec)
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] - dec
        print("Decrease Array n[",i,"] =",n[i])
        print()
        i = i + 1

decrease_array([5, 6, 7, 8])



Lab 5 - Increase values in array (by fixed value)

def increase_array(a):
    n = a
    i = 0
    inc = 5
    print("Increase Number is",inc)
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] + inc
        print("Increase Array n[",i,"] =",n[i])
        print()
        i = i + 1

increase_array([5, 6, 7, 8])


Lab 5 - Find/count number of positive values in array

def find_count_pos(a):
    n = a
    i = 0
    suma = 0
    x = 0     #amount of positve numer
    while(i < len(n)):
        if(n[i] > 0):
            suma = suma + n[ i ]
            print("Index of Positive number = n[", i ,"]")
            x = x + 1
        i = i + 1        
    print("Summary of Positive number is =", suma)
    print("Amount of Positive number is =", x)
   
find_count_pos([-5, 4, 3, 2, 1, -6, -3])


Lab 5 - Find sum of positive values in array

def pos_sum(a):
    n = a
    i = 0
    suma = 0
    while(i < len(n)):
        if(n[i] > 0):
            suma = suma + n[ i ]
        i = i + 1        
    print("Summary of Positive number is =", suma)
   
pos_sum([-5, 4, 3, 2, 1, -6, -3])


Lab 5 - Find average of values in array

def average_sum(a):
    n = a
    i = 0
    suma = 0
    while(i < len(n)):
         suma = suma + n[ i ]
         i = i + 1
    print("Summary =", suma)
    print("Amount of array =", len(n))
    suma = suma/len(n)
    print("Average of summary =", suma)

average_sum([5, 4, 3, 2, 1, 0, 6, 3])


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

Lab 5 - Find index of (the last) minimum value in array

def lastmin_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,21,900,21]
   i = 0
   maxnum = A[0]

   while(i<len(A)):
        if(maxnum >= A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")

lastmin_array()



Lab 5 - Find index of (the first) minimum value in array

def firstmin_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900,900,899]
   i = 0
   maxnum = A[0]
 
   while(i<len(A)):
        if(maxnum > A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
firstmin_array()


Lab 5 - Find index of (the last) maximum value in array

def lastmax_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900,900,899]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum <= A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
lastmax_array()


Lab 5 - Find index of (the first) maximum value in array

def firstmax_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum < A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
firstmax_array()

Lab 5 - The minimum value in array

def min_array():
   A = [23,22,53,24,45,26,72,81,89,55]
   i = 0
   minnum = A[0]
 
   while(i<len(A)):
        if(minnum > A[i]):
           minnum = A[i]
        i = i+1
   print(minnum)
   
         
min_array()



Lab 5 - The maximum value in array

def max_array():
   A = [1,2,3,4,5,6,7,8,9,10]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum < A[i]):
           maxnum = A[i]
        i = i+1
   print(maxnum)
         
max_array()


Lab 5 - Display elements of array and its index

def run_array():
   A = [1,2,3,4,5,6,7,8,9,10]
   i = 0
 
   while(i<len(A)):
      print("A[",i,"] is",A[i])
      i = i + 1
   print ("Amount of Index's Array is ",len(A))
   
run_array()


Lab 5 - Find sum of values in array

def setup():
    n = [ 5, 4, 3, 2, 1, 0, 6 ]
    i = 0
    sum = 0
    while(i < len(n)):
         sum = sum + n[ i ]
         i = i + 1
         print("sum =", sum)

setup()

Lab 4x - (Python) Summary of Integers

>>> def setup():
count = 1
n = 10
nsum = 0
while (count <= n):
nsum = nsum + count
count = count + 1
print("summation=",nsum)


>>> setup()
summation= 1
summation= 3
summation= 6
summation= 10
summation= 15
summation= 21
summation= 28
summation= 36
summation= 45
summation= 55
>>>

Lab 4x - (Python) Power of Ten

>>> def power_ten(num):
      if(num==6):
         print ("10 ^",num,"is Million")
      if(num==9):
         print ("10 ^",num,"is Billion")
      if(num==12):
         print ("10 ^",num,"is Trillion")
      if(num==15):
         print ("10 ^",num,"is Quadrillion")
      if(num==18):
         print ("10 ^",num,"is Quintillion")
      if(num==21):
         print ("10 ^",num,"is Sextillion")
      if(num==30):
         print ("10 ^",num,"is Nonillion")
      if(num==100):
         print ("10 ^",num,"is Googol")

       
>>> power_ten (9)
10 ^ 9 is Billion
>>>


Lab 4x - (Python) BMI Calculator

>>> def cal_bmi(weight,height):
   bmi = weight/((height/100)**2)
   print("Youe BMI is ",bmi)

 
>>> cal_bmi(62,173)
Youe BMI is  20.715693808680545


Lab 4x - (Python) Grade

>>> def setup():
  score = 78
  print("Your score was =",score)
  if score<101and score>=80:
    print("A")
  if score<80and score>=70:
    print("B")
  if score<70and score>=60:
    print("C")
  if score<60and score>=50:
    print("D")
  if score<50:
    print("F")

 
>>> setup()
Your score was = 78
B
>>>


วันอังคารที่ 15 กันยายน พ.ศ. 2558

Lab 4x - (Python) Mutiple Table

def setup():
    print (multi(7, 12))


def multi(n , row):
    i=0
    while (i <= row):
        result =  n * i
        print( n,"*",i,"=",result)
    i = i + 1

setup()

Lab 4x - (Python) Summary of Integers

def setup():
print (cal_sum(10))
print (cal_sum(15))


def cal_sum (i):
a = 0
x = 1
while (x<=i):
a = a+x
x = x+1
print("Sum 1 to",i,"=",a)


setup()


Lab 4x - (Python) Test

def setup():
  num = 0
  numl = 8
while (num<=numl):
print(num)
num = num + 1

setup()


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

Lab 4 - Prime

void setup() {
  size(250, 150);
  background(50);
  int minNumber = 0;
  int maxNumber = 20;
  int sum = 0;

  int space = 30;
  textAlign(CENTER);
  text("Sum of Prime Number", width/2, height/2-space);
  text(minNumber+" - "+maxNumber, width/2, height/2);
  while (minNumber <= maxNumber) {
    if (prime(minNumber)) {
      sum += minNumber;
    }
    minNumber++;
  }
  text("Sum = "+sum, width/2, height/2+space);
}
boolean prime(int value) {
  int num = 2;
  while (num <= value) {
    if (value%num == 0) {
      if (value == num) {
        return true;
      } else {
        return false;
      }
    }
    num++;
  }
  return false;
}