25 พฤศจิกายน 2558

Lab Raspberry Pi

class Student:
    def __init__(self,name,id,score):
        self.st_name=name
        self.st_id=id
        self.st_score=score

    def display(self):
        print("Name :",self.st_name)
        print("ID :",self.st_id)
        print("Score :",self.st_score)
        print("-----------------------")

    def getScore(self):
        return self.st_score

    def setScore(self,sc):
        self.st_score = sc

def findGrade(std):
    score = std.getScore()
    if (score >= 80) :
        return "A"
    elif (score >= 70) :
        return "B"
    elif (score >= 60) :
        return "C"
    elif (score >= 50) :
        return "D"
    else : return "F"

def countGrade(std,grade):
    i = 0
    count = 0
    while(i<len(std)):
        if(findGrade(std[i])==grade):
            count+=1
        i+=1
    return count

def showAllGrade(std):
    i = 0
    while(i<len(std)):
        print("Grade :",findGrade(std[i]))
        std[i].display()
        i+=1

def setup():
    std_data = [Student("bas",10075,65),
           Student("nop",20046,85),
           Student("toffi",30092,78),
           Student("Aun",20143,59)]

    print("Grade :",findGrade(std_data[1]))
    print(countGrade(std_data,"C"))
    showAllGrade(std_data)

setup()









2 พฤศจิกายน 2558

Lab8 JAVA

public class Student {
  private String st_name;
  private int st_id;
  private int st_age;
  private float st_weight;
  private float st_height;
  public Student(String name ,int id ,int age ,int weight ,int height){
    this.st_name = name;
    this.st_id = id;
    this.st_age = age ;
    this.st_weight = weight;
    this.st_height = height;
  }
  public void display(){
    System.out.println("Name : "+this.st_name);
    System.out.println("ID : "+this.st_id);
    System.out.println("Age : "+this.st_age);
    System.out.println("Weight : "+this.st_weight);
    System.out.println("Height : "+this.st_height);
  }
  public float getWeight(){
    return this.st_weight;
  }
  public float getHeight(){
    return this.st_height;
  }
  public int getAge(){
    return this.st_age;
  }
  public int getID(){
    return this.st_id;
  }
  public static void main (String[] agrs) {
    Student[] st_record  = {new Student("Tar",10016,21,69,179),
                            new Student("Tape",10032,21,90,174),
                            new Student("Bas",10032,19,69,175),
                            new Student("Top",10091,21,92,178),
                            new Student("Karn",10130,18,65,184)};
    displayStData(st_record);
    showBmi(st_record);
    showAvgAge(st_record);
    sortRecordByAge(st_record);
    findMinWeight(st_record);
   
  }
  public static void displayStData(Student[] std){
    int i = 0;
    while (i<std.length) {
      std[i].display();
      System.out.println("----------------------------------");
      i+=1;
    }
  }
  public static void showBmi(Student[] std){
    int i = 0;
    int count = 1;
    while (i<std.length) {
      float bmi = std[i].getWeight() / ((std[i].getHeight()/100)*(std[i].getHeight()/100));
      String bmi_f = String.format("%.2f",bmi);
        if (bmi > 25){
          System.out.println("No : "+count);
          std[i].display();
          System.out.printf("BMI : "+bmi_f);
          System.out.println();
          System.out.println("----------------------------------");
          count+=1;
      }
      i+=1;
    }
  }
  public static void showAvgAge(Student[] std){
    int i = 0;
    int count = 0;
    int sum_age = 0;
    while (i < std.length) {
      sum_age += std[i].getAge();
      if (std[i].getAge() < 30){
        count+=1;
      }
      i+=1;
    }
    float avg_age = sum_age/std.length;
    System.out.println("Average age of students : "+avg_age);
    System.out.println("----------------------------------");
  }
  public static void sortRecordByAge(Student[] std){
    int i = 0;
    while (i < std.length) {
      if (i!=std.length-1 && std[i].getAge() > std[i+1].getAge()) {
        int j  = i;
        while (j >= 0) {
          if (std[j].getAge() > std[j+1].getAge()) {
            Student data_copy = std[j];
            std[j] = std[j+1];
            std[j+1] = data_copy;
            j-=1;
          }
          else {
            break;
          }
        }
      }
      i+=1;
    }
    System.out.println("Sort record by age");
    System.out.println("////////////////////////////////");
    displayStData(std);
  }
  public static void findMinWeight(Student[] std){
    int i = 0;
    int count = 1;
    float min_w = std[0].getWeight();
    System.out.println("Student weight < 70");
    System.out.println("////////////////////////////////");
    while (i<std.length){
      if (std[i].getWeight() < min_w){
        min_w = std[i].getWeight();
      }
      if (std[i].getWeight() < 70){
        System.out.println("No : "+count);
        std[i].display();
        System.out.println("----------------------------------");
        count+=1;
      }
      i+=1;
    }
    System.out.println("Minimum weight of students : "+min_w);
    System.out.println("----------------------------------");
  }
}

Lab7 Class and method

class student:
    def __init__(self, st_name, st_id, st_age, st_weight, st_height):
        self.name = st_name
        self.id = st_id
        self.age = st_age
        self.weight = st_weight
        self.height = st_height
    def stData(self):
        print("Name :",self.name)
        print("ID :",self.id)
        print("Age :",self.age)
        print("Weight :",self.weight)
        print("Height :",self.height)
       
    def getWeight(self):
        return self.weight
    def getHeight(self):
        return self.height
    def getAge(self):
        return self.age
    def getID(self):
        return self.id
       
 

def setup():
   
    tar = student("tar",10012,21,69,179)
    tape = student("tape",10032,21,90,174)
    bas = student("bas",10075,19,69,175)
    top = student("top",10091,21,92,178)
    karn = student("karn",10130,18,60,184)
    st_record = [top,tape,tar,karn,bas]
 
    displayStData(st_record)
    showBmi(st_record)
    showAge(st_record)
    sortRecord(st_record)
    stWeight(st_record)
             
def displayStData(student):
    i = 0
    while i < len(student):
        student[i].stData()
        print("------------------------")
        i+=1

def showBmi(student):
    i = 0
    count = 1
    while i < len(student):
        #bmi=float("{0:.2f}".format(student[i].getWeight()/((student[i].getHeight()/100)**2)))
        bmi=float(format(student[i].getHeight()/((student[i].getHeight()/100)**2),'.2f'))
        #bmi = student[i].getWeight()/(student[i].getHeight()/100)**2
        if bmi > 25:
            print("No :",count)
            student[i].stData()
            print("Bmi :",bmi)
            print("------------------------")
            count+=1
        i+=1

def showAge(student):
    i = 0
    count = 0
    sum_age = 0
    while i < len(student):
        sum_age += student[i].getAge()
        if student[i].getAge() < 30:
            count+=1
        i+=1
    avg_age = sum_age/len(student)
    print("Average age of students :",avg_age)
    print("------------------------")

def sortRecord(student):  
    i = 0
    while i < len(student) :
        if i!=len(student)-1 and student[i].getAge() > student[i+1].getAge():
            j = i
            while j >= 0:
                if student[j].getAge() > student[j+1].getAge():
                    dataCopy = student[j]
                    student[j] = student[j+1]
                    student[j+1] = dataCopy
                    j-=1
                else :
                    break
        i+=1
    print("Sort record by age")
    print("------------------------")
    displayStData(student)

def stWeight(student):
    i = 0
    count = 1
    min_weight = student[0].getWeight()
    while i < len(student):
        if student[i].getWeight() < min_weight:
            min_weight = student[i].getWeight()
        if student[i].getWeight() < 70:
            print("No :",count)
            student[i].stData()
            print("------------------------")
            count+=1
        i+=1
    print("Minimum weight of students :",min_weight)
    print("------------------------")
       
setup()