관리 메뉴

nalaolla

오브젝트 예제 1 (객체 생성 및 출력) 본문

JAVA/7. Object3

오브젝트 예제 1 (객체 생성 및 출력)

날아올라↗↗ 2016. 6. 22. 17:15
728x90
  1. package test.com;
  2.  
  3. public class Test01Score {
  4.     int     num;
  5.     String  name;
  6.     int  kor;
  7.     int     eng;
  8.     int  math;
  9.     int     total;
  10.     double  avg;
  11.     String  grade;
  12.    
  13.     public Test01Score() {
  14.         num++;
  15.         name = "KIM";
  16.         kor = 77;
  17.         eng = 88;
  18.         math = 99;
  19.         total = kor + eng + math;
  20.         avg = total / 3.0;
  21.        
  22.         if (avg >= 90) {
  23.             grade = "A";
  24.         } else if(avg >= 80) {
  25.             grade = "B";
  26.         } else if(avg >= 70) {
  27.             grade = "C";
  28.         } else if(avg >= 60) {
  29.             grade = "D";
  30.         } else {
  31.             grade = "Other";
  32.         }
  33.     }
  34.    
  35.     public Test01Score(String name, int kor, int eng, int math) {
  36.         num++;
  37.         this.name = name;
  38.         this.kor = kor;
  39.         this.eng = eng;
  40.         this.math = math;
  41.         total = kor + eng + math;
  42.         avg = total / 3.0;
  43.        
  44.         if (avg >= 90) {
  45.             grade = "A";
  46.         } else if(avg >= 80) {
  47.             grade = "B";
  48.         } else if(avg >= 70) {
  49.             grade = "C";
  50.         } else if(avg >= 60) {
  51.             grade = "D";
  52.         } else {
  53.             grade = "Other";
  54.         }
  55.     }
  56. }





  1. package test.com;
  2.  
  3. public class Test01ScoreMain {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.println("score...");
  7.        
  8.         //Tess01Score객체 생성 및 출력
  9.        
  10.         System.out.println("============ 빈객체 호출 ============");
  11.         Test01Score s01 = new Test01Score();
  12.         System.out.println(s01.num);
  13.         System.out.println(s01.name);
  14.         System.out.println(s01.kor);
  15.         System.out.println(s01.eng);
  16.         System.out.println(s01.math);
  17.         System.out.println(s01.total);
  18.         System.out.println(s01.avg);
  19.         System.out.println(s01.grade);
  20.        
  21.         System.out.println("============ 인자값 4개 객체 호출 ============");
  22.         Test01Score s02 = new Test01Score("DANIEL"909589);
  23.         System.out.println(s02.num);
  24.         System.out.println(s02.name);
  25.         System.out.println(s02.kor);
  26.         System.out.println(s02.eng);
  27.         System.out.println(s02.math);
  28.         System.out.println(s02.total);
  29.         System.out.println(s02.avg);
  30.         System.out.println(s02.grade);
  31.        
  32.     }
  33.  
  34. }


728x90