관리 메뉴

nalaolla

오브젝트 예제 4 (접근제한자) 본문

JAVA/5. Object1

오브젝트 예제 4 (접근제한자)

날아올라↗↗ 2016. 6. 22. 17:09
728x90
  1. package test.com;
  2.  
  3. public class Test03score {
  4.    
  5.     //접근제한자 선언이 없다 >> default
  6.     //접근범위 public > protected > default : 같은 패키지 안에서만 > private : 동일클래스
  7.     public String name;
  8.     int kor;
  9.     int eng;
  10.     int math;
  11.     int total;
  12.     protected double avg;
  13.     /*private*/ String grade;
  14.    
  15.     public Test03score() {
  16.         //매개변수 없는 생성자 : 생략가능
  17. //    System.out.println(name);
  18. //    System.out.println(kor);
  19. //    System.out.println(eng);
  20. //    System.out.println(math);
  21. //    System.out.println(total);
  22. //    System.out.println(avg);
  23. //    System.out.println(grade);
  24.        
  25.     }
  26.    
  27.     public Test03score(String name, int kor, int eng, int math) {
  28.         //this >> class내부의 전역변수를 가리킴
  29.         this.name = name;
  30.         this.kor = kor;
  31.         this.eng = eng;
  32.         this.math = math;
  33.         total = kor + eng + math;
  34.         avg = (int)((total / 3.0d * 10))/10d;
  35.        
  36.         if(avg >=90) {
  37.             grade = "A";
  38.         } else if(avg >=80) {
  39.             grade = "B";
  40.         } else if(avg >=70) {
  41.             grade = "C";
  42.         } else if(avg >=60) {
  43.             grade = "D";
  44.         } else {
  45.             grade = "Other";
  46.         }
  47.     }
  48.    
  49.    
  50.  
  51. }   //end class




  1. package test.com;
  2.  
  3. public class Test03scoreMain {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         System.out.println("score...");
  8.         Test03score score = new Test03score();
  9.         // System.out.println(score.name);
  10.         // System.out.println(score.kor);
  11.         // System.out.println(score.eng);
  12.         // System.out.println(score.math);
  13.         // System.out.println(score.total);
  14.         // System.out.println(score.avg);
  15.         // System.out.println(score.grade);
  16.         System.out.println("=====================");
  17.  
  18.         // 매개변수가 있는 생성자를 만들고
  19.         // 매개벼수가 있는 생성자로 인자(값)를 전달 후
  20.         // 출력하시오
  21.         // 전달할 인자값(이름:홍길동, 국어:100, 영어:99, 수학:88)
  22.  
  23.         Test03score score2 = new Test03score("홍길동"1009988);
  24.         System.out.println("이름 : " + score2.name);
  25.         System.out.println("국어점수 : " + score2.kor);
  26.         System.out.println("영어점수 : " + score2.eng);
  27.         System.out.println("수학점수 : " + score2.math);
  28.         System.out.println("총점 : " + score2.total);
  29.         System.out.println("평균 : " + score2.avg);
  30.         System.out.println("등급 : " + score2.grade);
  31.  
  32.         System.out.println("=====================");
  33.     }
  34. }


728x90