9월 7일 개인공부

안효빈·2022년 9월 6일
0

개인 공부

목록 보기
8/36

백준코딩 15552번 빠른 A+B

임의의 난수 A+B를 T개만큼 출력하는 반복문

풀기에 앞서 조건으로 BufferedReader와 BufferedWriter을 사용하여

시간을 최대한 단축시키라고 함.

BufferedReader와 BufferedWriter

이거 내일


arraylist패키지공부

package arraylist;

import java.util.ArrayList;

public class Student {
	
	
		int studentID; //학번
		String studentName; //이름
		
//			Subject클래스 생성
		ArrayList<Subject> subjectList = new ArrayList<Subject>(); //과목 리스트
		
//		생성자
		
		public Student(int studentID, String studentName) {
			
			this.studentID = studentID;
			this.studentName = studentName;
//		********과목 리스트
//			this.subjectList = new ArrayList<Subject>(); 
//			subjectList = new ArrayList<Subject>(); 
			//굳이 따로 분리해서 쓴거고 위에 걍 ArrayList<Subject> subjectList 옆에 = new ArrayList<Subject>(); 해두 똑같음

//			이게 없으니까 StudentTest의 Student생성자들이 제 역할을 못함. 그 생성자로 만든 addSubject메소드가 안돌아감
			
		}
		
//		과목별 과목 점수 메소드
		public void addSubject(String name, int point) {
			
			Subject subject = new Subject();
			
//			******Student()생성자의 과목리스트로감
			subjectList.add(subject); //밑에 향상for문을 돌리려면 드가야되는듯
			
			//subject클래스가서 해당 메소드들 확인
			subject.setName(name);
			subject.setScorePoint(point);
		}
		
		public void showStudentInfo() {
			int total = 0;
//			*******반복문을 이용
			for(Subject s : subjectList) {
				
				total += s.getScorePoint();
				System.out.println(studentName + "의 " + s.getName() + "성적은 " + s.getScorePoint() + "점입니다.");
			}
			
			System.out.println("학생 " + studentName + "의 총점은 " + total + "점 입니다." + "\n" + "평균 점수는 " + total / (subjectList.size()) + "점 입니다");
		}
		
}
package arraylist;

public class Subject {
	
	
	private String name; //과목이름
	private int scorePoint; //과목점수
	
	
//	public Subject() {}
	
	
	public String getName() {
		
		return name;
	}
	
	public void setName(String name) {
		
		this.name = name;	//과목이름
	}
	
	public int getScorePoint() {
		
		return scorePoint;
	}
	
	public void setScorePoint(int scorePoint) {
		
		this.scorePoint = scorePoint;	//과목점수
	}	
	
	
}
package arraylist;

public class StudentTest {
	
	public static void main(String args[]) {
		
//		학생별 과목 점수와 총점을 출력
		Student studentLee = new Student(1001, "이순신");
		Student studentKim = new Student(1002, "김유신");
		
//		과목별 과목 점수 ***Student.java***
		studentLee.addSubject("국어", 100);
		studentLee.addSubject("수학", 50);

		studentKim.addSubject("국어", 70);
		studentKim.addSubject("수학", 85);
		studentKim.addSubject("영어", 100);
		
//		학생이름, 과목명, 과목별성적 출력 메소드
		studentLee.showStudentInfo();
		System.out.println("=========================");
		studentKim.showStudentInfo();
	}
	
}

studentLee ==> Student와 같은 구조를 가진 집


Subject subject = new Subject();

		subjectList.add(subject);
		
		//subject클래스가서 해당 메소드들 확인
		subject.setName(name);

===> subject의 필드에 초기화를 하고

===> 그걸 포장해서 자기 집의 subjectList를 장식함


studentLee.addSubject("국어", 100); ==> 집 안의 subjectList를 장식함

위에서 한 행동이랑 같음


studentLee.showStudentInfo(); ===> 자기 집을 찍어서 보여주는 카메라를 작동함.

==> 꾸며진건 Student와 비슷하게 생긴 구조의 자기(studentLee) 집이므로

==> 자기가 꾸민 자기 필드의 변수들이 카메라에 찍혀서 나옴

profile
다들 화이팅

0개의 댓글