[Java] tip: 성적 프로그램 만들기 (생성자, 객체형 배열)

febCho·2023년 11월 23일
0

Java

목록 보기
34/53

성적 프로그램 만들기 - 문제

  • Score
* [실습]
* 1. 멤버 변수 : 이름(name), 국어(korean), 영어(english), 수학(math)
* 2. 생성자 : 인자가 없는 생성자, 인자가 있는 생성자
* 3. 메서드 : 총점(makeSum), 평균(makeAvg), 등급(makeGrade)
* 4. set/get 메서드 생성
  • ScoreMain
	 * [실습]
	 * 1. Scanner 객체 생성
	 * 2. 배열의 길이가 4인 scoreArray 배열 생성
	 * 3. 변수 전체 과목 총점(total), 전체 과목 평균(avg)
	 * 4. Score 객체를 4개 생성해서 배열에 저장
	      이름, 국어, 영어, 수학 점수를 입력받아서 객체에 저장하시오.
	 * 5. 반복문을 이용한 객체의 멤버 변수 값 출력
	      이름, 국어, 영어, 수학, 총점, 평균, 등급
	 * 6. 전체 과목 총점, 전체 과목 평균

성적 프로그램 만들기 - 풀이

  • Score
package kr.s19.object.array;

public class Score {
	
	private String name;
	private int korean;
	private int english;
	private int math;
	
	public Score() {}
	
	public Score(String name, int korean, int english, int math) {
		this.name = name;
		this.korean = korean;
		this.english = english;
		this.math = math;
	}
	
	public int makeSum() {
		return korean + math + english;
	}
	
	public int makeAvg() {
		return makeSum() / 3;
	}
	
	public String makeGrade() {
		String grade;
		switch(makeAvg()/10){
			case 10:
			case 9: grade = "A"; break;
			case 8: grade = "B"; break;
			case 7: grade = "C"; break;
			case 6: grade = "D"; break;
			default: grade = "F";
		}
		return grade;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getKorean() {
		return korean;
	}

	public void setKorean(int korean) {
		this.korean = korean;
	}

	public int getEnglish() {
		return english;
	}

	public void setEnglish(int english) {
		this.english = english;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}
	
	
}
  • ScoreMain
package kr.s19.object.array;

import java.util.Scanner;

public class ScoreMain {
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		Score[] scoreArray = new Score[4];
		int total = 0;
		int avg = 0;
		
		for(int i=0;i<scoreArray.length;i++) {
			System.out.print("이름> ");
			String name = input.nextLine();
			System.out.print("국어> ");
			int korean = input.nextInt();
			System.out.print("영어> ");
			int english = input.nextInt();
			System.out.print("수학> ");
			int math = input.nextInt();
			
			input.nextLine();//enter 흡수 - 오동작 해결
			//이름에 엔터가 들어가서 바로 국어로 넘어가는 버그
			
			scoreArray[i] = new Score(name, korean, english, math);
			System.out.println("========================");
		}
		
		for(Score score : scoreArray) {
			System.out.printf("%s\t", score.getName());
			System.out.printf("%d\t", score.getKorean());
			System.out.printf("%d\t", score.getEnglish());
			System.out.printf("%d\t", score.getMath());
			System.out.printf("%d\t", score.makeSum());
			System.out.printf("%d\t", score.makeAvg());
			System.out.printf("%s\n", score.makeGrade());
			total += score.makeSum();
		}
		avg = total / (scoreArray.length * 3);
		
		System.out.println("======================");
		System.out.printf("전체 과목 총점: %d, 전체 과목 평균: %d", total, avg);
		
		input.close();
	}
}

Score - 변수와 메서드를 저장해 객체로 활동하는 측면
ScoreMain - 메인이 있어서 객체 만들어 수행하는 측면

1. Score

  1. 총점, 평균, 등급을 구하는 기본적인 풀이 방법은 아래 게시글을 참고한다.
    참고) 생성자를 활용해 성적 처리하기
    참고) switch문을 활용해 등급 구하기

2. ScoreMain

  1. import를 통해 간단하게 Scanner 객체를 생성한다.
    import java.util.Scanner;Scanner input = new Scanner(System.in);

  2. 배열의 길이가 4인 scoreArray 배열을 선언 및 생성한다. (초기화 이후에 진행)
    Score[] scoreArray = new Score[4];

  3. Score 객체를 4개 생성하는 과정은 반복 작업이므로, for문을 이용해 scoreArray 배열의 길이 만큼 루프를 돌며
    1) 이름, 국어 성적, 영어 성적, 수학 성적 데이터를 입력 받고
    ex. System.out.print("이름> ");
    String name = input.nextLine();
    2) Score 생성자에 데이터를 넘겨
    3) Score 객체를 생성해 scoreArray 배열의 인덱스에 저장을 해준다.
    scoreArray[i] = new Score(name, korean, english, math);

  4. 그렇게 값을 저장한 뒤에는 확장 for문을 이용해 데이터를 출력하는데, 이때 for문 안에서 전체 학생의 전체 과목의 총합(total)을 구해주면 편리하다.

for(Score score : scoreArray) {
	System.out.printf("%s\t", score.getName());
	System.out.printf("%d\t", score.getKorean());
	System.out.printf("%d\t", score.getEnglish());
	System.out.printf("%d\t", score.getMath());
	System.out.printf("%d\t", score.makeSum());
	System.out.printf("%d\t", score.makeAvg());
	System.out.printf("%s\n", score.makeGrade());
	total += score.makeSum();
		}
  1. 그렇게 total을 구하고 난 뒤에는, 전체 과목의 평균을 아래와 같이 구한다.
    ex. avg = total / (scoreArray.length * 3);

  2. 구한 총점과 평균까지 따로 출력이 끝나면 input.close();를 한다.

  3. 콘솔 및 출력 결과는 다음과 같다.

출력)
이름> 홍길동
국어> 99
영어> 100
수학> 80
========================
이름> 장영실
국어> 85
영어> 99
수학> 98
========================
이름> 이순신
국어> 87
영어> 78
수학> 99
========================
이름> 을지문덕
국어> 87
영어> 66
수학> 99
========================
홍길동	99	100	80	279	93	A
장영실	85	99	98	282	94	A
이순신	87	78	99	264	88	B
을지문덕	87	66	99	252	84	B
======================
전체 과목 총점: 1077, 전체 과목 평균: 89
profile
Done is better than perfect.

0개의 댓글