반응형
Subject.java
package subject;
public class Subject {
protected int kor, eng, math, sum;
protected double avg;
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 getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public Subject(int kor, int eng, int math) {
this.kor = kor;
this.eng = eng;
this.math = math;
this.sum = kor + eng + math;
this.avg = Math.round((double) sum / 3);
}
public void sum() {
this.sum = kor + eng + math;
}
public void average() {
this.avg = Math.round((double)sum / 3);
}
public void showSubjectInfo() {
System.out.println("*기본교과* 국어:" + kor + ", 영어:" + eng + ", 수학:" + math + " | [합계:" + sum + ", 평균:" + avg + "]");
}
}
LiterarySubject.java
package subject;
public class LiterarySubject extends Subject {
private int his;
public int getHis() {
return his;
}
public void setHis(int his) {
this.his = his;
}
public LiterarySubject(int kor, int eng, int math, int his) {
super(kor, eng, math);
this.his = his;
this.sum = kor + eng + math + his;
this.avg = Math.round((double) sum / 4);
}
@Override
public void sum() {
this.sum = kor + eng + math + his;
}
@Override
public void average() {
this.avg = Math.round((double)sum / 4);
}
@Override
public void showSubjectInfo() {
System.out.println("*기본교과* 국어:" + kor + ", 영어:" + eng + ", 수학:" + math + " | *문과 교과* 역사:" + his + " | [합계:"
+ sum + ", 평균:" + avg + "]");
}
}
ScienceSubject.java
package subject;
public class ScienceSubject extends Subject {
private int phy, chemi;
public int getPhy() {
return phy;
}
public void setPhy(int phy) {
this.phy = phy;
}
public int getChemi() {
return chemi;
}
public void setChemi(int chemi) {
this.chemi = chemi;
}
public ScienceSubject(int kor, int eng, int math, int phy, int chemi) {
super(kor, eng, math);
this.phy = phy;
this.chemi = chemi;
this.sum = kor + eng + math + phy + chemi;
this.avg = Math.round((double) sum / 5);
}
@Override
public void sum() {
this.sum = kor + eng + math + phy + chemi;
}
@Override
public void average() {
this.avg = Math.round((double)sum / 5);
}
@Override
public void showSubjectInfo() {
System.out.println("*기본교과* 국어:" + kor + ", 영어:" + eng + ", 수학:" + math + " | *이과 교과* 물리:" + phy + ", 화학:"
+ chemi + " | [합계:" + sum + ", 평균:" + avg + "]");
}
}
SubjectTest.java
package subject;
import java.util.ArrayList;
public class SubjectTest {
public static void main(String[] args) {
ArrayList<Subject> list = new ArrayList<Subject>();
list.add(new Subject(90, 80, 100));
list.add(new ScienceSubject(80, 80, 100, 80, 90));
list.add(new LiterarySubject(80, 70, 75, 86));
for (int i = 0; i < list.size(); i++) {
list.get(i).showSubjectInfo();
System.out.println();
}
}
}
<결과값>
Student.java
package subject;
public class Student {
int studentNum;
Subject subject;
int rank = 1;
public Student() {
}
public Student(int studentNum, int kor, int eng, int math) {
this.studentNum = studentNum;
this.subject = new Subject(kor, eng, math);
}
public Student(int studentNum, Subject subject) {
this.studentNum = studentNum;
this.subject = subject;
}
}
CalcRank.java
package subject;
import java.util.ArrayList;
public class CalcRank {
public ArrayList<Student> calcRank(ArrayList<Student> list) {
int[] rank = new int[3];
int[] avg = new int[3];
for (int i = 0; i < 3; i++) {
avg[i] = (int) list.get(i).subject.getAvg();
}
for (int i = 0; i < 3; i++) {
rank[i] = 1;
for (int j = 0; j < avg.length; j++) {
if (avg[i] < avg[j])
++rank[i];
}
}
for (int i = 0; i < 3; i++) {
list.get(i).rank = rank[i];
}
return list;
}
}
Main.java
package subject;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student(1, 90, 90, 90));
list.add(new Student(2, new Subject(88, 97, 90)));
list.add(new Student(3, 70, 70, 70));
CalcRank calc = new CalcRank();
list = calc.calcRank(list);
for (int i = 0; i < list.size(); i++) {
System.out.println((i + 1) + "번 학생 : ");
list.get(i).subject.showSubjectInfo();
System.out.println("석차 : " + list.get(i).rank);
System.out.println();
}
}
}
<결과값>
반응형
'Java' 카테고리의 다른 글
[Java-기초] 실습 (주문번호, 상품이름, 상품가격, 갯수) (0) | 2021.02.24 |
---|---|
[Java-기초] 실습 (멤버 호출) (0) | 2021.02.24 |
[Java-기초] 실습 (메서드로 구구단 호출하기) (0) | 2021.02.24 |
[Java-기초] 실습 (로또 추첨 번호) (0) | 2021.02.24 |
[Java-기초] 알고리즘 실습 (마방진) (0) | 2021.02.24 |
댓글