점수(Score) 클래스와 학생 정보(Student) 클래스를 구현하고 Main에서 리스트를 활용하여 학생 성적 정보를 나타내어라.
조건
점수(Score) 클래스
멤버변수 영어, 수학 점수
기본 생성자, 매개변수가 있는 생성자
getter 와 setter 메소드
학생 정보(Student) 클래스
멤버변수 이름, 점수(타입 Score)
기본 생성자, 매개변수가 있는 생성자
getter 와 setter 메소드
Main
1. 학생 정보(이름, 점수) 3개 입력받아 list에 저장
2. 전체 데이터 검색(출력)
3 데이터 수정
3.1 1번 인덱스의 학생 정보를 변경
// 예시) 이름: 물리
// 수학 :100
// 영어 : 50
3.2 0번 인덱스 학생의 영어 점수만 변경
4. 데이터 삭제 : 1 번 인덱스 학생의 모든 정보 삭제
Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Student> list = new ArrayList<>();
// 1. 학생 정보(이름, 점수) 3개 입력받아 list에 저장
for (int i = 0; i < 3; i++) {
System.out.println("수학 점수 입력:");
int math = sc.nextInt();
System.out.println("영어 점수 입력:");
int eng = sc.nextInt();
System.out.println("이름 입력:");
String name = sc.next();
list.add(new Student(name, new Score(math, eng)));
}
// 2. 전체 데이터 검색(출력)
for (Student x : list) {
System.out.println(x);
}
// 3. 데이터 수정
// 3.1) 1번 인덱스의 학생 정보를 변경
// 예시) 이름: 물리
// 수학 :100
// 영어 : 50
list.set(1, new Student("물리", new Score(100, 50)));
// 3.2) 0번 인덱스 학생의 영어 점수만 변경
list.get(0).getScore().setEng(770); //
System.out.println();
// 변경된 리스트의 모든 데이터 출력
for (Student x : list) {
System.out.println(x);
}
System.out.println();
// 데이터 삭제 : 1 번 인덱스 학생의 모든 정보 삭제
list.remove(1);
for (Student x : list) {
System.out.println(x);
}
} // end main()
} // end CollectionMain04
점수(Score) 클래스
public class Score {
//멤버 변수 영어, 수학 점수
private int math;
private int eng;
public Score() {
}
// 기본 생성자, 매개변수가 있는 생성자
public Score(int math, int eng) {
this.eng = eng;
this.math = math;
}
// getter 와 setter 메소드
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
@Override
public String toString() {
return "Score [math=" + math + ", eng=" + eng + "]";
}
}
학생 정보(Student) 클래스
public class Student {
// 멤버 변수 이름, 점수(타입 Score)
private String name;
private Score score;
// 기본 생성자, 매개변수가 있는 생성자
public Student() {
}
public Student(String name,Score score) {
this.name=name;
this.score=score;
}
//getter 와 setter 메소드
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
}