본문 바로가기
Project/Study | etc

JAVA의 정석 3판 - 객체지향 프로그래밍 연습문제(3)

by 코젼 2022. 6. 24.
728x90
반응형

🚩문제 1

🚩문제 2

💾소스코드

public class Exercise6_4 {
	public static void main(String[] args) {
		Student s = new Student();
		s.name = "홍길동";
		s.ban = 1;
		s.no = 1;
		s.kor = 100;
		s.eng = 60;
		s.math = 76;
		
		System.out.println("이름 : " + s.name);
		System.out.println("총점 : " + s.getTotal());
		System.out.println("평균 : " + s.getAverage());
	}
}

class Student {
	String name;
	int ban;
	int no;
	int kor;
	int eng;
	int math;

	int getTotal() {
		return this.kor + this.eng + this.math;
	}
	
	float getAverage() {
		float avg = 0.0f;
		avg = getTotal() / (float)(3);
		avg = Math.round(avg*10) / (float)10;
		return avg;
	}
}

💾소스코드

public class Exercise6_5 {
	public static void main(String[] args) {
		Student1 s = new Student1("홍길동", 1, 1, 100, 60, 76);
		System.out.println(s.info());
	}

}

class Student1 {
	String name;
	int ban;
	int no;
	int kor;
	int eng;
	int math;
	
	Student1(String name, int ban, int no, int kor, int eng, int math) {
		this.name = name;
		this.ban = ban;
		this.no = no;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	
	int getTotal() {
		return this.kor + this.eng + this.math;
	}
	
	float getAverage() {
		float avg = 0.0f;
		avg = getTotal() / (float)(3);
		avg = Math.round(avg*10) / (float)10;
		return avg;
	}
	
	String info() {
		return (this.name + ", " + this.ban + ", " + this.no + ", "
				+ this.kor + ", " + this.eng + ", " + this.math + ", " + getTotal() + ", " + getAverage());
	}
}
728x90
반응형

댓글