728x90
320x100
package chap02;
import java.util.Scanner;
// 시력분포를 해당되는 사람만큼 *로 표시하기.
public class Practice07 {
static final int VMAX = 21; //시력분포(0.0~0.1 단위로 21개)
static class PhyscData {
String name; //이름
int height; //키
double vision; //시력
public PhyscData(String name, int height, double vision) {
this.name = name;
this.height = height;
this.vision = vision;
}
}
//키의 평균값을 구함
static double aveHeight(PhyscData[] dat){
double sum = 0;
for (int i = 0; i < dat.length; i++)
sum += dat[i].height;
return sum / dat.length;
}
//시력 분포를 구함
static void distVision(PhyscData[] dat, int[] dist){
int i = 0;
dist[i] = 0;
for (i = 0; i < dat.length; i++)
if (dat[i].vision >= 0.0 && dat[i].vision <= VMAX / 10.0)
dist[(int)(dat[i].vision * 10)]++;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PhyscData[] x = {
new PhyscData("강민하", 162,0.3),
new PhyscData("김찬우", 173,0.7),
new PhyscData("박준서", 175,2.0),
new PhyscData("유서범", 171,1.5),
new PhyscData("이수연", 168,0.4),
new PhyscData("장경오", 174,1.2),
new PhyscData("황지안", 169,0.8),
};
int[] vdist = new int[VMAX]; //시력분포
System.out.println("-- 신체검사 리스트 --");
System.out.println("이름 키 시력");
System.out.println("-----------------");
for(int i = 0; i < x.length; i++)
System.out.printf("%-8s%3d%5.1f\n",
x[i].name, x[i].height, x[i].vision);
System.out.printf("\n평균 키 : %5.1fcm\n", aveHeight(x));
distVision(x, vdist); //시력 분포를 구함
System.out.println("\n시력 분포");
for (int i = 0; i < VMAX; i++) {
System.out.printf("%3.1f~ : ", i / 10.0);
for (int j = 0; j < vdist[i]; j++)
System.out.print('*');
System.out.println();
}
}
}
클래스 본체와 멤버 : 클래스 본체에서는 다음과 같은 내용 선언 가능.
- 멤버(필드, 메서드, 중첩 클래스, 중첩 인터페이스)
- 클래스 초기화, 인스턴스 초기화
- 생성자
규칙
- 필드, 메서드, 생성자를 선언할 때 Public, protected, private을 지정가능.
- 메서드, 생성자는 다중으로 정의(오버로드)할수 있음.
- final로 선언한 필드는 값을 한 번만 대입할 수 있음.
- 생성자는 새로 생성하는 인스턴스를 초기화 하기 위해 사용함.
파이널 클래스
- 클래스 접속 제한자인 final을 붙여 클래스를 선언하면 하위 클래스를 가질 수 없는 (다른 클래스가 상속할 수 없는) 파이널 클래스가 됨.
파생클래스
- 클래스 A를 직접 상위 클래스로 하려면 선언할 때 extends A를 추가해야함. 이 때 선언한 클래스는 클래스 A의 직접 하위 클래스가 됨. 클래스 선언에 extends가 없는 클래스의 경우 직접 상위 클래스는 오브젝트 클래스가 됨. 오브젝트 클래스는 유일하게 상위 클래스를 갖지 않는 클래스임.
인터페이스 구현
- 인터페이스 x를 구현하러면 선언에 implements x를 추가해야 함.
추상클래스
- 클래스 수식자인 abstract를 붙여 클래스를 선언하면 추상 메서드를 가질수 있는 추상클래스가 됨.
- 추상클래스형은 불완전한 클래스임으로 인스터스를 만들수 없음 (추상 메서드란 실체가 정의되지 않은 메서드. 실체는 하위 클래스에서 정의.)
중첩 클래스
- 클래스 또는 인터페이스 안에 선언한 클래스는 중첩 클래스가 됨.
- 멤버 클래스는 그 선언이 다른 클래스 또는 인터페이스 선언에 의해 직접 둘러 싸인 클래스.
- 내부 클래스는 명시적으로도 암묵적으로도 정적이라고 선언하지 않은 중첩 클래스. 정적 초기화나 멤버 인터페이스 선언을 할 수 없음. 그리고 컴파일을 할 때 상수 필드가 아니면 정적 멤버를 선언할 수 없음.
- 지역클래스는 이름이 주어진 중첩 클래스인 내부 클래스. 어떤 클래스의 멤버도 될 수 없음.
728x90
320x100
'💻 하나씩 차곡차곡 > 자료구조 & 알고리즘(JAVA)' 카테고리의 다른 글
선형검색 (chap03/SeqSearch) (0) | 2022.10.27 |
---|---|
n일 전/일의 날짜를 반환하기 (chap02/Practice08) (0) | 2022.10.26 |
향상된 for문 (for-each문) (chap02/ArraySumForIn) (0) | 2022.10.24 |
배열 복사하기 (chap02/CloneArray) (0) | 2022.10.23 |
1000 이하의 소수를 나열하기 (chap02/PrimeNumber3) (0) | 2022.10.22 |