12
21

* ๋‹ค์Œ์˜ ์ƒ์† ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š” ์ธํ„ฐํŽ˜์ด์Šค์™€ ํด๋ž˜์Šค๋“ค์„ ์ž‘์„ฑํ•˜์„ธ์š”.

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();	//๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก
	}

}
728x90
๋ฐ˜์‘ํ˜•
COMMENT