JAVA_Learn/Do it! _ Algorithm Study

#4. 1주차 과제 4 [Chap02_2C-1, 14]

CEJ_0929 2021. 12. 30. 18:12

2C-1 (ArraySumForIn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package practice;
 
public class ArraySumForIn_2C_1 {
 
    public static void main(String[] args) {
        
        double [] a = {1.0,2.0,3.0,4.0,5.0};
        
        for (int i = 0; i < a.length; i++
            System.out.println("a["+i+"]= "+a[i]);
        
        
        double sum = 0;
        for (double i : a) sum+=i;
        
        /*
         * 일반 for문에서 i = index 또는 int형의 별도의 변수.
         * 
         * 확장 for문에서 i =
         * 배열a를 순차적으로 탐색하며 해당 순번의 '값'으로 작용
         *     int, double, String, char, 클래스 등 무엇이든 대입된다.
         */
        
        System.out.println("모든 요소의 합은: "+sum);
 
    }
}
cs

 


2-14 (PhysicalExamination)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package practice;
 
import java.util.Scanner;
 
public class PhysicalExamination_2_14 {
    
    //PhysicalExamination클래스 내의 모든 메서드, 클래스에서 공유하는 static 멤버
    static final int VMAX = 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++) {
            // 시력이 0.0보다 같거나 높고, 2.1보다 낮거나 같으면
            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 stdIn = new Scanner(System.in);
 
        PhyscData[]x = {
 
                new PhyscData("박현규"1620.3),
                new PhyscData("함진아"1730.7),
                new PhyscData("최윤미"1752.0),
                new PhyscData("홍연의"1711.5),
                new PhyscData("이수진"1680.4),
                new PhyscData("김영준"1741.2),
                new PhyscData("박용규"1690.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~ : %2d명\n", i/10.0,vdist[i]);
 
    }
}
 
cs

 


이번주 스터디 과제 끝 ^^ !!