public class Student {
 private String name;
 private String telNo;
 private int    age;
 private double weight;
 
 public Student(String name,String telNo,int age,double weight){
  this.name   = name;
  this.telNo  = telNo;
  this.age    = age;
  this.weight = weight;
 }
 
 public String toString() { 
  String  str;
  
  str = "Name    : " + name     + "\n"         +
        "TelNo   : " + telNo    + " \n"        +
        "age     : " + age      + " year old\n"+
        "Aeight  : " + weight   + " Kgs.\n";
  return(str);
 }
 public String getName(){
  return name;
 }
 public String getTelNo(){
  return telNo;
 }
 public int getAge(){
  return age;
 }
 public double getWeight(){
  return weight;
 }
 public void setName(String name){
  this.name=name;
 }
 public void setTelNo(String telNo){
   this.telNo=telNo;
 }
 public void setAge(int age){
  this.age=age;
 }
 public void setWeight(double weight){
  this.weight=weight;
 }
 public static void main(String[] args) {
  Student s1 = new Student("B","0812345678",20,89.1);
  Student s2 = new Student("V","08123456789",19,91.2);
  System.out.println("Detail B\n"  +s1);
  System.out.println("Detail V\n"  +s2);
  
  s1.setName("สมปอง");
  s1.setTelNo("0869876543");
  s2.setAge(21);
  s2.setWeight(70);
  System.out.println("name   s1 :"  +s1.getName());
  System.out.println("TelNo  s1 :"  +s1.getTelNo());
  System.out.println("Age    s2 :"  +s2.getAge());
  System.out.println("Weight s2 :"  +s2.getWeight());
  
  // try: remove method toString() and see what happends
 }
}
 
No comments:
Post a Comment