วันพฤหัสบดีที่ 5 พฤศจิกายน พ.ศ. 2558

Lab 8 - String(Version 2) [JAVA]

public class Banner {
private String word;
private String symbol;

public Banner(String word, String symbol) {
this.word = word;
this.symbol = symbol;
}

public void printWord() {
String[] A = { "A", "  #  ", " # # ", "#   #", "#####", "#   #" };
String[] C = { "C", "#####", "#    ", "#    ", "#    ", "#####" };
String[] E = { "E", "#####", "#    ", "#### ", "#    ", "#####" };
String[] blank = { " ", "  ", "  ", "  ", "  ", "  " };
String[][] character = { A, C, E, blank };
this.changesymbol(character);
char[] wordArray = this.word.toCharArray();
for (int row = 1; row <= 5; row++) {
for (int i = 0; i < this.word.length(); i++) {
for (int j = 0; j < character.length; j++) {
char checkChar = character[j][0].charAt(0);
if (wordArray[i] == checkChar) {
System.out.print(character[j][row]+" ");
}
}
}
System.out.println();
}
}

public void changesymbol(String[][] character) {
for (int i = 0; i < character.length; i++) {
for (int j = 1; j < character[i].length; j++) {
character[i][j] = character[i][j].replace("#",this.symbol);
}
}
}

public void set_symbol(String symbol) {
this.symbol = symbol;
}

public static void main(String[] args) {
Banner word = new Banner("AEC ACE ECE ACA", "#");
word.printWord();
System.out.println();
word.set_symbol("*");
word.printWord();
}
}

Lab 8 - String(Version 1) [JAVA]

public class Banner {
private String word;
private String symbol;

public Banner(String word, String symbol) {
this.word = word;
this.symbol = symbol;
}

public void printWord() {
String[] A = { "A", "  #  ", " # # ", "#   #", "#####", "#   #" };
String[] C = { "C", "#####", "#    ", "#    ", "#    ", "#####" };
String[] E = { "E", "#####", "#    ", "#### ", "#    ", "#####" };
String[] blank = { " ", "  ", "  ", "  ", "  ", "  " };
String[][] character = { A, C, E, blank };
this.changesymbol(character);
char[] wordArray = this.word.toCharArray();
for (int row = 1; row <= 5; row++) {
for (int i = 0; i < this.word.length(); i++) {
for (int j = 0; j < character.length; j++) {
char checkChar = character[j][0].charAt(0);
if (wordArray[i] == checkChar) {
System.out.print(character[j][row]+" ");
}
}
}
System.out.println();
}
}

public void changesymbol(String[][] character) {
for (int i = 0; i < character.length; i++) {
for (int j = 1; j < character[i].length; j++) {
String newSymbol = "";
char[] wordArray = character[i][j].toCharArray();
char checkSymbol = this.symbol.charAt(0);
for (int k = 0; k < character[i][j].length(); k++) {
if (wordArray[k] != checkSymbol && wordArray[k] != ' ') {
newSymbol += this.symbol;
} else {
newSymbol += wordArray[k];
}
}
character[i][j] = newSymbol;

}
}
}

public void set_symbol(String symbol) {
this.symbol = symbol;
}

public static void main(String[] args) {
Banner word = new Banner("AEC ACE ECE ACA", "#");
word.printWord();
System.out.println();
word.set_symbol("*");
word.printWord();
}
}

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

Lab 8 - Student(Age) [JAVA]

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 int get_age() {
return this.age;
}

public void display() {
System.out.println("Name : " + this.name);
System.out.println("ID : " + this.ID);
System.out.println("Age : " + this.age);
System.out.println("Weight : " + this.weight);
System.out.println("Height : " + this.height + "\n");
}

public static void main(String[] args) {
Student[] std = { new Student("ant", 58001, 39, 55, 165), new Student("bird", 58010, 25, 75, 170),
new Student("cat", 58011, 19, 44, 155), new Student("dog", 58100, 30, 72, 169),
new Student("eagle", 58101, 22, 70, 178), new Student("frog", 58110, 18, 100, 175) };

System.out.println("Number of students with age < 30 =" + find_numAge_lessthan30(std) + "\n \n");
System.out.println("Student Data (Sort by Age)");
insertionSort_byAge(std);
for (int i = 0; i < std.length; i++) {
System.out.println();
std[i].display();
}
}

public static int find_numAge_lessthan30(Student[] student) {
int count = 0;
for (int i = 0; i < student.length; i++) {
if (student[i].get_age() < 30) {
count += 1;
}
}
return count;
}

public static void insertionSort_byAge(Student[] student) {
for (int i = 1; i < student.length; i++) {
Student currentStudent = student[i];
int position = i;
while(position > 0 && student[position - 1].get_age() > currentStudent.get_age()) {
student[position] = student[position - 1];
position--;
}
student[position] = currentStudent;
}
}
}

Lab 8 - Student(Weight) [JAVA]

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 int get_weight(){
 return this.weight;
  }

  public void display() {
    System.out.println("Name : "+this.name);
    System.out.println("ID : "+this.ID);
    System.out.println("Age : "+this.age);
    System.out.println("Weight : "+this.weight);
    System.out.println("Height : "+this.height+"\n");
  }
  public static void main(String[] args){
    Student[] std = {new Student("ant",58001,39,55,165),
           new Student("bird",58010,25,75,170),
           new Student("cat",58011,19,44,155),
           new Student("dog",58100,30,72,169),
           new Student("eagle",58101,22,70,178),
           new Student("frog",58110,18,100,175)};

    System.out.println("Number of students with weight < 50 = "+find_numWeight_lessthan50(std)+"\n");
    for(int i = 0; i < std.length; i++){
        if(std[i].get_weight() < 50){
            std[i].display();
        }
    }
  }
 
  public static int find_numWeight_lessthan50(Student[] student){
    int count = 0;
    for(int i = 0; i < student.length; i++){
        if(student[i].get_weight() < 50){
            count += 1;
        }
    }
    return count;
  }
}

วันอาทิตย์ที่ 1 พฤศจิกายน พ.ศ. 2558

Lab 8 - Student(BMI) [JAVA]

public class Student {
  private String name;
  private int ID;
  private int age;
  private float weight;
  private float 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("Name : "+this.name);
    System.out.println("ID : "+this.ID);
    System.out.println("Age : "+this.age);
    System.out.println("Weight : "+this.weight);
    System.out.println("Height : "+this.height);
  }

  public float get_weight(){
    return this.weight;
  }
   
  public float get_height(){
    return this.height;
  }
  public static void main(String[] args){
    Student[] std = {new Student("ant",58001,39,55,165),
           new Student("bird",58010,25,75,170),
           new Student("cat",58011,19,44,155),
           new Student("dog",58100,30,72,169),
           new Student("eagle",58101,22,70,178),
           new Student("frog",58110,18,100,175)};
System.out.println("Number of students with BMI > 25 ="+find_numBMI_greaterthan25(std));
    int i = 0;
    while(i < std.length){
      if(calBMI(std[i]) > 25){
        System.out.println();
        std[i].display();
        System.out.println("BMI : "+String.format("%.2f",calBMI(std[i])));
      }
      i += 1;
    }
  }
 
  public static float calBMI(Student student){
    float highM = student.get_height()/100;
    float bmi = student.get_weight()/(highM*highM);
    return bmi;
  }
  public static int find_numBMI_greaterthan25(Student[] student){
    int i = 0;
    int count = 0;
    while(i < student.length){
      if(calBMI(student[i]) > 25){
        count += 1;
      }
      i += 1;
    }
    return count;
  }
}

Lab 8 - Student(Display) [JAVA]

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("Name : "+this.name);
    System.out.println("ID : "+this.ID);
    System.out.println("Age : "+this.age);
    System.out.println("Weight : "+this.weight);
    System.out.println("Height : "+this.height+"\n");
  }
  public static void main(String[] args){
    Student[] std = {new Student("ant",58001,39,55,165),
           new Student("bird",58010,25,75,170),
           new Student("cat",58011,19,44,155),
           new Student("dog",58100,30,72,169),
           new Student("eagle",58101,22,70,178),
           new Student("frog",58110,18,100,175)};

    int i = 0;
    while(i < std.length){
        std[i].display();
        i = i + 1;
    }
  }
}

Lab 7 - String (Version 2)

class Banner:
    def __init__(self,word,symbol):
        self.word = word
        self.symbol = symbol
       
    def printWord(self):
        A = ["A","  #  "," # # ","#   #","#####","#   #"]
        C = ["C","#####","#    ","#    ","#    ","#####"]
        E = ["E","#####","#    ","#### ","#    ","#####"]
        blank = [" ","  ","  ","  ","  ","  "]
        char = [A,C,E,blank]
        self.changesymbol(char)
        row = 1
        while(row <= 5):
            i = 0
            while(i < len(self.word)):
                j = 0
                while(j < len(char)):
                    if(self.word[i] == char[j][0]):
                        print(char[j][row],end = " ")
                    j+=1
                i += 1
            print("")
            row += 1
           
    def changesymbol(self,character):
        i = 0
        while(i < len(character)):
            j = 1
            while(j < len(character[i])):
                character[i][j] = character[i][j].replace("#",self.symbol)
                j += 1
            i += 1

    def set_symbol(self,symbol):
        self.symbol = symbol

def setup():
    word = Banner("AEC ACE ECE ACA","#")
    word.printWord()
    print()
    word.set_symbol("*")
    word.printWord()

setup()

Lab 7 - String (Version 1)

class Banner:
    def __init__(self,word,symbol):
        self.word = word
        self.symbol = symbol
       
    def printWord(self):
        A = ["A","  #  "," # # ","#   #","#####","#   #"]
        C = ["C","#####","#    ","#    ","#    ","#####"]
        E = ["E","#####","#    ","#### ","#    ","#####"]
        blank = [" ","  ","  ","  ","  ","  "]
        char = [A,C,E,blank]
        self.changesymbol(char)
        row = 1
        while(row <= 5):
            i = 0
            while(i < len(self.word)):
                j = 0
                while(j < len(char)):
                    if(self.word[i] == char[j][0]):
                        print(char[j][row],end = " ")
                    j+=1
                i += 1
            print("")
            row += 1
           
    def changesymbol(self,character):
        i = 0
        while(i < len(character)):
            j = 1
            while(j < len(character[i])):
                k = 0
                newSymbol = ""
                while(k < len(character[i][j])):
                    if(character[i][j][k] != self.symbol and character[i][j][k] != " "):
                        newSymbol += self.symbol
                    else:
                        newSymbol += character[i][j][k]
                    k += 1
                character[i][j] = newSymbol
                j += 1
            i += 1

    def set_symbol(self,symbol):
        self.symbol = symbol

def setup():
    word = Banner("AEC ACE ECE ACA","#")
    word.printWord()
    print()
    word.set_symbol("*")
    word.printWord()

setup()

Lab 7 - Student (Weight)

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

    def display(self):
        print("Name :",self.name)
        print("ID :",self.ID)
        print("Age :",self.age)
        print("Weight :",self.weight)
        print("Height :",self.height)

    def get_weight(self):
        return self.weight

def setup():
    std = [Student("ant",58001,39,55,165),
           Student("bird",58010,25,75,170),
           Student("cat",58011,19,44,155),
           Student("dog",58100,30,72,169),
           Student("eagle",58101,22,70,178),
           Student("frog",58110,18,100,175)]

    print("Number of students with weight < 50 =",find_numWeight_lessthan50(std),"\n")
    i = 0
    while(i < len(std)):
        if(std[i].get_weight() < 50):
            std[i].display()
        i += 1


def find_numWeight_lessthan50(student):
    i = 0
    count = 0
    while(i < len(student)):
        if(student[i].get_weight() < 50):
            count += 1
        i += 1
    return count

setup()

Lab 7 - Student (Age)

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

    def display(self):
        print("Name :",self.name)
        print("ID :",self.ID)
        print("Age :",self.age)
        print("Weight :",self.weight)
        print("Height :",self.height)

    def get_age(self):
        return self.age
   
def setup():
    std = [Student("ant",58001,39,55,165),
           Student("bird",58010,25,75,170),
           Student("cat",58011,19,44,155),
           Student("dog",58100,30,72,169),
           Student("eagle",58101,22,70,178),
           Student("frog",58110,18,100,175)]

    print("Number of students with age < 30 =",find_numAge_lessthan30(std),"\n \n")
    print("Student Data (Sort by Age)")
    i = 0
    insertionSort_byAge(std)
    while(i < len(std)):
        print()
        std[i].display()
        i += 1

def find_numAge_lessthan30(student):
    i = 0
    count = 0
    while(i < len(student)):
        if(student[i].get_age() < 30):
            count += 1
        i += 1
    return count

def insertionSort_byAge(student):
    for i in range(1,len(student)):
        currentStudent = student[i]
        position = i
        while(position > 0 and student[position-1].get_age() > currentStudent.get_age()):
            student[position] = student[position-1]
            position = position-1
        student[position] = currentStudent
       

setup()

Lab 7 - Student (BMI)

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

    def display(self):
        print("Name :",self.name)
        print("ID :",self.ID)
        print("Age :",self.age)
        print("Weight :",self.weight)
        print("Height :",self.height)

    def get_weight(self):
        return self.weight
   
    def get_height(self):
        return self.height

def setup():
    std = [Student("ant",58001,39,55,165),
           Student("bird",58010,25,75,170),
           Student("cat",58011,19,44,155),
           Student("dog",58100,30,72,169),
           Student("eagle",58101,22,70,178),
           Student("frog",58110,18,100,175)]
    print("Number of students with BMI > 25 =",find_numBMI_greaterthan25(std))
    i = 0
    while(i < len(std)):
        if(calBMI(std[i]) > 25):
            print()
            std[i].display()
            print("BMI : %.2f"%calBMI(std[i]))
        i += 1

def calBMI(student):
    highM = student.get_height()/100
    bmi = student.get_weight()/(highM*highM)
    return bmi

def find_numBMI_greaterthan25(student):
    i = 0
    count = 0
    while(i < len(student)):
        if(calBMI(student[i]) > 25):
            count += 1
        i += 1
    return count
setup()

Lab 7 - Student (Display)

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

    def display(self):
        print("Name :",self.name)
        print("ID :",self.ID)
        print("Age :",self.age)
        print("Weight :",self.weight)
        print("Height :",self.height,"\n")

def setup():
    std = [Student("ant",58001,39,55,165),
           Student("bird",58010,25,75,170),
           Student("cat",58011,19,44,155),
           Student("dog",58100,30,72,169),
           Student("eagle",58101,22,70,178),
           Student("frog",58110,18,100,175)]

    i = 0
    while(i < len(std)):
        std[i].display()
        i += 1
 
setup()