* ๋ค์์ ์์ ๊ด๊ณ๋ฅผ ๊ฐ๋ ์ธํฐํ์ด์ค์ ํด๋์ค๋ค์ ์์ฑํ์ธ์.
Student <-- Calc, Print
<-- Person
<์ธํฐํ์ด์ค>
1) Calc
-์ถ์๋ฉ์๋: int calcTot(int[] a);
float calcAvg(int a, int b);
2) Print
-์ถ์๋ฉ์๋: void printInfo(); // num, name, c, java, net, tot, avg ์ถ๋ ฅ
<์ถ์ํด๋์ค>
1) Person
-๋ณ์: int num, String name
-์ถ์ ๋ฉ์๋ : void inputData(); //num, name, c, java, net ์ ๋ ฅ
<ํด๋์ค>
1)Student
- ์ธํฐํ์ด์ค Calc, Print์ ํด๋์ค Person์ ๋ค์ค ์์
- main() ๋ฉ์๋ ํฌํจ
- ์ด์ ๊ณผ ํ๊ท ์ ๊ฐ๊ฐ calcTot์ calcAvg ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ณ์ฐํด์ผ ํจ
- ์๋์ ๊ฐ์ด ์ถ๋ ฅ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก ์์ฑํ ๊ฒ
(์ถ๋ ฅ๊ฒฐ๊ณผ)
import java.util.Scanner;
interface Calc{
int calcTot(int[] a);
float calcAvg(int a, int b);
}
interface Print{
void printInfo();
}
abstract class Person{
int num;
String name;
Person(int num, String name){ //๋ณ์ ์ ์ธํ๊ธฐ์ ์ด๊ธฐํ
this.num = num;
this.name = name;
}
abstract void inputData();
}
public class StudentEx extends Person implements Calc, Print {
Scanner sc = new Scanner(System.in); //์
๋ ฅ๋ฐ์ Scanner ์ ์ธ, ์ปจํธ๋กค์ฌํํธ์ค๋ก ์ฝ์
int[] score = new int [3]; //์ ์ 3๊ฐ ์
๋ ฅ ๋ฐ์
StudentEx(int num, String name) { //๋ค์ฌ์จ Person์์๋ ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ ธ์์ ์ด๊ธฐํ
super(num, name);
}
@Override //์ค๋ฒ๋ผ์ด๋ฉ์ผ๋ก ๊ฐ์ ธ์ 5๊ฐ์ง ์
๋ ฅ ๋ฐ์
void inputData() {
System.out.print("num: ");
this.num = sc.nextInt();
System.out.print("name: ");
this.name = sc.next();
System.out.print("c: ");
this.score[0] = sc.nextInt();
System.out.print("java: ");
this.score[1] = sc.nextInt();
System.out.print("net: ");
this.score[2] = sc.nextInt();
}
@Override //tot ๋ณ์ ์ ์ธํ๊ณ , ๋ฐฐ์ด 3๊ฐ ์์๋๋ก ๋ํ๋ ๋ฐฉ์์ผ๋ก ๊ณ์ฐ
public int calcTot(int[] a) {
int tot = 0;
for(int i = 0; i<a.length; i++) {
tot += a[i];
}
return tot;
}
@Override //a์ ์ ์ฒด ํฉ์ฐ์ ๊ฐ์ ธ์ค๊ณ b์ ๋๋ ์ซ์๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ์
public float calcAvg(int a, int b) {
return (float) a / b;
}
@Override //๋ชจ๋ ์ถ๋ ฅ, ๊ณ์ฐ๋ ์ฌ๊ธฐ์ ์ง์ . ๋ง์ง๋ง์๋ ์ค๋ฐ๊ฟํ๊ธฐ ์ํด ln
public void printInfo() {
System.out.print("num: "+this.num);
System.out.print(", name: "+this.name);
System.out.print(", c: "+this.score[0]);
System.out.print(", java: "+this.score[1]);
System.out.print(", net: "+this.score[2]);
System.out.print(", tot: "+this.calcTot(this.score));
System.out.println(", avg: "+this.calcAvg(calcTot(this.score),3));
}
public static void main(String[] args) {
StudentEx s1 = new StudentEx(1, "hong"); //๋ฉ์ธ์์๋ StudentEx๋ฅผ ๋ถ๋ฌ ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ (์ฐ๋ ๊ธฐ๊ฐ)
s1.inputData(); //๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๊ธฐ๋ง 3๋ฒ ๋ฐ๋ณตํ๋ค.
StudentEx s2 = new StudentEx(2, "kim");
s2.inputData();
StudentEx s3 = new StudentEx(3, "lee");
s3.inputData();
s1.printInfo(); s2.printInfo(); s3.printInfo(); //๊ฐ์ ์ถ๋ ฅํ๋๋ก
}
}