클래스와 연관된 문제 (6-1,6-2)
연습문제01
- 다음과 같은 멤버변수를 갖는 Student 클래스를 정의하시오.
class Student{
String name;
int ban;
int no;
int kor;
int eng;
int math;
}
연습문제01-1
- 다음과 같은 실행결과를 얻도록 Student 클래스에 생성자와 info()를 추가하시오
public class Exercise {
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 {
}
console
홍길동, 1, 1, 100, 60, 76, 236, 78.7
내가 작성한 코드
public class Main {
String name;
int ban;
int no;
int kor;
int eng;
int math;
public Main(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;
}
public String info() {
int sum = kor + eng + math;
return name + ", " + ban +", " + no +
", " + kor +", " + eng + ", " + math + ", " +
(sum + ", " + Math.round(sum*10/3.00) * 0.1);
}
public static void main(String[] args) {
Main m = new Main("홍길동", 1, 1, 100, 60, 76);
String str = m.info();
System.out.println(str);
}
}
References