S007_Sungjuk _허용할 범위_public, private, protect 개념

Posted by PeEn
2019. 5. 14. 11:44 Programing/Java
  • // public  = 누구나 사용할 수 있다.
  • // private = 혼자만 사용할 수 있다.
  • // protect =  상속자만 사용할 수 있다.
  • //   = 상관없다.
    1. public class Sungjuk (메인 매소드)
      package kr.ac.jj.java201562054;
      
      import java.util.Scanner;
      
      public class S007_Sungjuk { 
      
      	public static void main(String[] args) {
      		// TODO Auto-generated method stub
      		
      		HighStudent hs = new HighStudent();
      		
      		hs.Input();
      		hs.Print();
      		HighStudent hs2 = new HighStudent(100,99,100);
      		hs2.Print();
      		
      	}
      
      }
      
    2. public class HighStudent (매소드)
      package kr.ac.jj.java201562054;
      
      import java.util.Scanner;
      
      
      // public 	=	누구나 사용할 수 있다.
      // private	=	혼자만 사용할 수 있다.
      // protect	= 	상속자만 사용할 수 있다.
      //			=	상관없다.
      public class HighStudent {
      	private String name = "No Name";	//이름
      	private int kor;	//국어성적
      	private int eng;	//영어성적
      	private int mat;	//수학성적
      	private int tot;	//합계	
      	private double ave;	//평균
      	private String grade;	//등급
      	
      	public HighStudent(){
      		
      	
      	}
      	
      	public HighStudent(int kor, int eng, int mat){
      		this.kor = kor;
      		this.eng = eng;
      		this.mat = mat;
      		Calc();
      	}
      	
      	public void Input(){
      		Scanner scan = new Scanner(System.in);
      		System.out.print("이름:");
      		name = scan.next();
      		System.out.print("국어:");
      		kor = scan.nextInt();
      		System.out.print("영어:");
      		eng = scan.nextInt();
      		System.out.print("수학:");
      		mat = scan.nextInt();
      		Calc();
      	}
      	
      	public void Print(){
      		System.out.println(
      				name
      				+"\t"+kor
      				+"\t"+eng
      				+"\t"+mat
      				+"\t"+tot
      				+"\t"+String.format("%6.2f",ave)//소수점 자르기
      				+"\t"+grade
      				);
      	}
      	
      	private void Calc() {
      		tot = kor + eng + mat;
      		ave = (double)tot / 3;	
      		
      		if(ave >= 90) {
      			grade = "수";
      		}
      		else if(ave >= 80) {
      			grade = "우";
      		}
      		else if(ave >= 70) {
      			grade = "미";
      		}
      		else if(ave >= 60) {
      			grade = "양";
      		}
      		else {
      			grade = "가";
      		}
      		
      		
      	}
      	
      
      }