본문 바로가기

JAVA/JAVA_1

Chap09_03.예시

Student : 이름, 국어, 영어, 수학, 총점, 평균 / 성적 출력메서드

문과Student : + 사회, 성적 출력메서드 override

이과Student : + 과학, 성적 출력메서드 override

여러명의 학생(문과, 이과) 학생에 대한 성적을 산출

 

자식 객체(L_Student, S_Student)를 부모데이터타입(Student)의 배열에 넣어서 

1. 문과학생만 성적목록 출력 (instanceof 사용)

2. 이과학생만 성적목록 출력 (instanceof 사용)

3. 전체학생 성적목록 출력

-------------------------------------------------------------------------------------------

Main

package kosta.mission.mission08;

import java.util.Scanner;

public class StuMain2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
		int count=0; //학생 수 count
		Student[] students=new Student[100];
		Scanner scan=new Scanner(System.in);
		LoopWhile:
			while(true) {
				System.out.println("학생들의 성적을 입력합니다");
				System.out.println("======================");
				System.out.println("1.문과 2.이과 3.성적입력종료");
				String key=scan.nextLine();
				
				switch (key) {
				case "1":
					System.out.println("학생이름 : ");
					String stuName=scan.nextLine();
					System.out.print("국어 : ");
					int kor=Integer.parseInt(scan.nextLine());
					System.out.print("영어 : ");
					int eng=Integer.parseInt(scan.nextLine());
					System.out.print("수학 : ");
					int math=Integer.parseInt(scan.nextLine());
					System.out.print("사회 : ");
					int society=Integer.parseInt(scan.nextLine());
					L_Student l=new L_Student(stuName, kor, eng, math, society);
					l.calSum();
					l.calAvg();
					students[count++]=l;
					break;
				case "2":
					System.out.println("학생이름 : ");
					stuName=scan.nextLine();
					System.out.print("국어 : ");
					kor=Integer.parseInt(scan.nextLine());
					System.out.print("영어 : ");
					eng=Integer.parseInt(scan.nextLine());
					System.out.print("수학 : ");
					math=Integer.parseInt(scan.nextLine());
					System.out.print("과학 : ");
					int science=Integer.parseInt(scan.nextLine());
					S_Student s=new S_Student(stuName, kor, eng, math, science);
					s.calSum();
					s.calAvg();
					students[count++]=s;
					break;
				case "3":
					break LoopWhile;

				}
			}
		System.out.println("성적 입력한 학생 수:"+count);
		System.out.println("전체학생 점수 출력");
		System.out.println("====================");
		for (int i = 0; i < count; i++) {
			students[i].printScore();
		}
		System.out.println();
		System.out.println("문과학생만 출력");
		System.out.println("====================");
		for (int i = 0; i < students.length; i++) {
			if(students[i] instanceof L_Student) {
				students[i].printScore();
			}
		}
		System.out.println();
		System.out.println("이과학생만 출력");
		System.out.println("====================");
		for (int i = 0; i < students.length; i++) {
			if(students[i] instanceof S_Student) {
				students[i].printScore();
			}
		}
	}

}

  Student 클래스 (부모)

package kosta.mission.mission08;

public class Student {
	private String stuName;
	private int kor;
	private int eng;
	private int math;
	private int total;
	private int avg;
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(String stuName, int kor, int eng, int math) {
		super();
		this.stuName = stuName;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	public int calSum() {
		this.total=this.kor+this.eng+this.math;
		return getTotal();		
	}
	public int calAvg() {
		this.avg=getTotal()/3;
		return getAvg();
	}
	public void printScore() {
		System.out.println("학생이름: "+getStuName());
		System.out.println("국      어: "+getKor());
		System.out.println("수      학: "+getMath());
		System.out.println("영      어: "+getEng());
		System.out.println("총      점: "+getTotal());
		System.out.println("평      균: "+getAvg());
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getAvg() {
		return avg;
	}
	public void setAvg(int avg) {
		this.avg = avg;
	}
	
}

L_Student (자식1 클래스)

package kosta.mission.mission08;

public class L_Student extends Student {
	private int society;

	public L_Student() {
		// TODO Auto-generated constructor stub
	}

	public L_Student(String stuName, int kor, int eng, int math, int society) {
		super(stuName, kor, eng, math);
		this.society = society;
	}

	@Override
	public int calSum() {
		setTotal(super.calSum()+getSociety());
		return getTotal();
	}

	@Override
	public int calAvg() {
		setAvg(getTotal()/4);
		return getAvg();
	}

	@Override
	public void printScore() {
		System.out.println("학생이름: "+getStuName());
		System.out.println("국      어: "+getKor());
		System.out.println("수      학: "+getMath());
		System.out.println("영      어: "+getEng());
		System.out.println("사      회: "+getSociety());
		System.out.println("총      점: "+getTotal());
		System.out.println("평      균: "+getAvg());
	}

	public int getSociety() {
		return society;
	}

	public void setSociety(int society) {
		this.society = society;
	}
	
	
}

S_Student (자식2 클래스)

package kosta.mission.mission08;

public class S_Student extends Student {
	private int science;

	public S_Student() {
		// TODO Auto-generated constructor stub
	}

	public S_Student(String stuName, int kor, int eng, int math, int science) {
		super(stuName, kor, eng, math);
		this.science = science;
	}
	@Override
	public int calSum() {
		setTotal(super.calSum()+getScience());
		return getTotal();
	}

	@Override
	public int calAvg() {
		setAvg(getTotal()/4);
		return getAvg();
	}

	@Override
	public void printScore() {
		System.out.println("학생이름: "+getStuName());
		System.out.println("국      어: "+getKor());
		System.out.println("수      학: "+getMath());
		System.out.println("영      어: "+getEng());
		System.out.println("과      학: "+getScience());
		System.out.println("총      점: "+getTotal());
		System.out.println("평      균: "+getAvg());
	}

	public int getScience() {
		return science;
	}

	public void setScience(int science) {
		this.science = science;
	}
	
}

'JAVA > JAVA_1' 카테고리의 다른 글

Chap10_01.추상메서드, 추상클래스 (abstract)  (0) 2021.01.29
상속 Review  (0) 2021.01.29
Chap09_02.instanceof 연산자 이용  (0) 2021.01.28
Chap09_01.상속(Inheritance)  (0) 2021.01.28
Chap08_예외처리(Exception)_예시  (0) 2021.01.27