본문 바로가기
✨ Java/인터페이스(Interface)

자바(JAVA) - 인터페이스 (Interface) 학생 점수

by 환풍 2023. 2. 15.
728x90
인터페이스는 혼자 작업하는 것이라면 굳이 쓸 필요가 없다는 것을 알 수 있다.
결국 클래스를 이용하여 Main메소드에서 호출하는 것은 클래스와 별반 다를게 없지만,
마지막으로 한번만 더 연습해보고자한다.

 

StudentUtil.Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package student;
 
// 1. 아래 요구사항을 충족하는 메소드를 선언.
// 2. 완성한 인터페이스를 구현하는 클래스 StudentTest를 Student 패키지에 만들고
// StudentUtil 인터페이스에서 선언한 메소드를 구현.
 
 
public interface StudentUtil {
    //==첫번째 기능==
    //첫번째 매개변수로 받은 다수의 학생 중
    //두번째 매개변수로 받은 이름을 가진 학생의 점수등급을 리턴
    //단, 전달받은 이름을 가진 학생이 없는 경우, 등급은 "등급없음"이 된다.
    //점수 등급 기준
    // 90 <= 점수 <= 100 -> A
    // 80 <= 점수 <= 89 -> B
    // 70 <= 점수 <= 79 -> C
    // 70 > 점수 -> D
    //메소드명 : getGradeByStudentName
    String getGradeByStudentName(Student[] students, String name);
 
 
 
 
    //==두번째 기능==  예 학생이 매개변수 5개 들어오면 총점이 5개가 있다. 이전부를 리턴해라.
    //매개변수로 받은 다수의 학생들의 총점을 배열로 리턴
    //메소드명 : getTotalScoresToArray
    int[] getTotalScoresToArray(Student[] students);
 
    //==세번째 기능==
    //매개변수로 두 명의 학생이 전달된다.
    //전달된 두 학생 중 총점이 높은 학생 객체를 리턴하는 매소드를 만들어보자.
    // 단, 두 학생의 총점이 같은 경우는 없다고 가정한다.
    // 메소드명 : getHighScoreStudent
    Student getHighScoreStudent(Student stu1, Student stu2);
}
 
cs

요구사항이 담긴 인터페이스 파일을 받아왔다.

이제 이 요구사항을 토대로 클래스의 이름과 매개변수를 지정해주자.

 

Student.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package student;
 
public class Student{
    //이름
    private String name;
 
    //국어점수
    private int korScore;
 
    //수학점수
    private int mathScore;
 
    //영어점수
    private int engScore;
 
    public Student(String name, int korScore, int mathScore, int engScore) {
        this.name = name;
        this.korScore = korScore;
        this.mathScore = mathScore;
        this.engScore = engScore;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getKorScore() {
        return korScore;
    }
 
    public void setKorScore(int korScore) {
        this.korScore = korScore;
    }
 
    public int getMathScore() {
        return mathScore;
    }
 
    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }
 
    public int getEngScore() {
        return engScore;
    }
 
    public void setEngScore(int engScore) {
        this.engScore = engScore;
    }
 
 
}
 
cs

학생 정보를 담을 클래스를 선언해주고 세팅했다.

 

StudentTest.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package student;
 
import java.util.Scanner;
 
//==첫번째 기능==
//첫번째 매개변수로 받은 다수의 학생 중
//두번째 매개변수로 받은 이름을 가진 학생의 점수등급을 리턴
//단, 전달받은 이름을 가진 학생이 없는 경우, 등급은 "등급없음"이 된다.
//점수 등급 기준
// 90 <= 점수 <= 100 -> A
// 80 <= 점수 <= 89 -> B
// 70 <= 점수 <= 79 -> C
// 70 > 점수 -> D
//메소드명 : getGradeByStudentName
public class StudentTest implements StudentUtil {
 
    @Override
    public String getGradeByStudentName(Student[] students, String name) {
        String grade = " ";
        for(int i =0; i<students.length; i++){
            if(students[i].getName().equals(name)){
                int totalScore = students[i].getKorScore() + students[i].getMathScore() + students[i].getEngScore();
                double avg = totalScore / 3.0;
 
 
                if(avg >= 90 && avg <=100)
                {
                    grade = "A";
                }
                else if(avg >= 80 )
                {
                    grade = "B";
                }
                else if(avg >= 70)
                {
                    grade = "C";
                }
                else
                {
                    grade = "D";
                }
 
            }
            if(grade.equals(" "))
            {
                grade = "등급 없음";
            }
 
        }
        return grade;
    }
 
    @Override
    public int[] getTotalScoresToArray(Student[] students) {
        int[] resultArr = new int[students.length];
        for(int i = 0; i< students.length; i++){
            int totalScore = students[i].getKorScore() + students[i].getMathScore() + students[i].getEngScore();
            resultArr[i] = totalScore;
        }
 
        return resultArr;
    }
 
    @Override
    public Student getHighScoreStudent(Student stu1, Student stu2) {
        int totalScore1 = stu1.getKorScore() + stu1.getMathScore() + stu1.getEngScore();
        int totalScore2 = stu2.getKorScore() + stu2.getMathScore() + stu2.getEngScore();
 
        if(totalScore1 > totalScore2)
        {
            return stu1;
        }
        else
        {
            return stu2;
        }
 
    }
}
 
cs

요구사항을 처리할 인터페이스에서,  받아올 Override 기능들을 만들어 주었다. 

 

RunStudent (Main 메소드)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package student;
 
public class RunStudent {
    public static void main(String[] args) {
        StudentTest s = new StudentTest();
 
        Student[] studnets = new Student[3];
        studnets[0= new Student("김"908070);
        studnets[1= new Student("이"402010);
        studnets[2= new Student("박"928378);
 
        String result1 = s.getGradeByStudentName(studnets,"최");
        System.out.println(result1);
 
 
        int[] result2 = s.getTotalScoresToArray(studnets);
        for(int i=0; i<result2.length; i++)
        {
            System.out.println(result2[i] + " ");
        }
 
        System.out.println();
 
        Student s1 = new Student("김"808080);
        Student s2 = new Student("이"707070);
        Student result3 =  s.getHighScoreStudent(s1,s2);
        System.out.println(result3.getName());
        System.out.println(result3.getKorScore());
        System.out.println(result3.getEngScore());
        System.out.println(result3.getMathScore());
    }
}
 
cs

성공적으로 출력되는 것을 확인할 수 있다.

반응형

댓글