Java/객체지향
[Java] 객체 지향 연습 정답
ycraah
2024. 8. 13. 22:46
1. 다음과 같은 실행 결과를 얻도록 Student 클래스에 생성자와 info()를 추가하시오.
public class Ex5_1 {
public static void main(String[] args) {
Student s = new Student ("홍길동", 1, 1, 100, 60 , 76);
String str = s.info();
System.out.println(str);
}
}
class Student{
String name;
int ban, no, kor, eng, math;
int sum;
float aver;
public Student(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;
this.sum = kor + eng + math;
this.aver = (Math.round(sum*10/3f))/10f;
}
String info(){
return name + "," + ban + "," + no + "," + kor + "," + eng + "," + math + "," + sum + "," + aver;
}
}
2. getTotal()과 getAverage()를 추가하시오.
public class Ex5_2 {
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, no, kor, eng, math;
int sum;
float aver;
public Student(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;
this.sum = kor + eng + math;
this.aver = (Math.round(sum*10/3f))/10f;
}
public Student(){
}
String info(){
return name + "," + ban + "," + no + "," + kor + "," + eng + "," + math + "," + sum + "," + aver;
}
int getTotal(){
return kor + eng + math;
}
float getAverage(){
return (Math.round((kor + eng + math)*10/3f))/10f;
}
}