20/01/02 3일차 CLASS - METHOD exercises

Java 교육 3일차

CLASS - METHOD exercises

배열 데이터 활용한 연습문제

⇒ Class 활용
  - Main Class
  - Data Class

⇒ for-if 구조



Data 저장을 위한 일반 class


1
2
3
4
5
6
7
8
9
10
public class EMPClass {
    int[] age={27,34,28,26,41,28,42,29,29,32};
    char[] gender={'F','M','F','M','M','F','M','M','F','M'};
    int[] pay={460,200,250,300,300,200,350,200,400,440};
    
    public EMPClass(){        
        System.out.println("EMP 생성자 내부");                
    }    
}
cs

실행을 위한 Main Class

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
public class Main {
    public static void main(String[] args) {
        
     EMPClass emp = new EMPClass();    // EMPClass 불러오기
        
     // 문제1 월급이 400 이상인 사람의 수
     
     int count = 0;
     
     for(short i=0; i<emp.pay.length; i++){
         if(emp.pay[i]>=400){     
             count ++;
         }
      }     
     System.out.println("The number of people who earn more than 400 is :\n" + count);
     
     
     //    문제2 성별이 여자인 사람들의 월급 합
     int sum = 0;
     
     for(short j=0; j<emp.gender.length ; j++){
         if (emp.gender[j] == 'F'){
             sum = sum + emp.pay[j];
         }         
     }
     
     System.out.println("The sum of women's pay is :\n" + sum);
     
     // 문제3 성별이 남자인 사람들의 월급 합과 평균연령
        
    int wsum = 0, agesum = 0, s = 0;
        
    for(short k=0; k<emp.gender.length ; k++){
        if(emp.gender[k] == 'M'){
            wsum = wsum + emp.pay[k];
            agesum = agesum + emp.age[k];
            s++;
        }
    }
    double ageaver = (agesum / (s*1.0));
    
    System.out.println("The sum of men's pay is :\n" + wsum);
    System.out.println("The average of the men is :\n" + ageaver);     
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 문제4 독일에 근무하는 사원들의 평균 급여
int gesum = 0, gecount = 0 ;
    
for(short m = 0; m<emp.code.length; m++){
    if (emp.code[m].equals("DE")){
        gesum = gesum + emp.pay[m];
        gecount++;
    }        
}
double geaverpay = gesum / (gecount*1.0);
System.out.println("The averge pay of GE is :\n" + geaverpay);
cs


METHOD

4 types


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
// Method type 1 : no input data, no return
    
    public void sum1(){
        int n = 10 + 20 ;
        System.out.println("Sum = " + n);
    }
    
    
// Method type 2 : no input data, return is
    
    public int sum2(){
        int n = 10 + 20 ;
        return n ;
    }
    
    
// Method type 3 : input data is, no return
    
    public void sum3(int a, int b){
        int n = a+b;
        System.out.println("Sum of inputs is : " + n);
    }
    
    
// Method type 4 : input data is, return is
    
    public double div(float f1, int a){
        double d = f1/a;
        return d;
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Method1 
     emp.sum1();  // method declaration  
     
// Method2
     System.out.println(emp.sum2()); // no need to declaration, 
     int r = emp.sum2();  // input sum2 method to variable r 
     System.out.println(r);
     
// Method3
     emp.sum3(111,3333);  // method declaration and input data
     
     
// Method4
     double r2 = emp.div(12.4f, 55);
     System.out.println(r2);
     System.out.println(r+r2);
cs


둘 이상의 data types 를 return 하는 방법 : CLASS

1. 둘 이상의 data types 를 저장할 class 생성
1
2
3
4
5
6
7
8
public class ResultData {
    int r1;
    float r2;
    String r3;
    
    public ResultData() {
    }
}
cs

2. method class 에서 변수 선언 - class object 생성 - return
1
2
3
4
5
6
public ResultData test2(){
    int a =10float b = 20String s = "ABC";  // Variable declaration
    ResultData rd = new ResultData();  // class object 생성
    rd.r1 = a; rd.r2 = b; rd.r3 = s;   // variables input to class
    return rd;        
}
cs

3. main class 에서 호출
1
2
3
4
 ResultData result = emp.test2();
     System.out.println(result.r1);
     System.out.println(result.r2);
     System.out.println(result.r3);
cs


<Class 연습>

MainClass


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
public class MainClass {
    public static void main(String[] args) {
        
        DataClass dc = new DataClass();
        
        int sum = 0;
        for (int i = 0; i<dc.ic.length ; i++){
            sum = sum + dc.ic[i].price;
        }
        
        System.out.println(sum + "\n");
        
        for (int i=0; i<dc.ic.length ; i++){
            if(dc.ic[i].press.equals("길벗")){
                System.out.println(dc.ic[i].title);
            }
        }
        
        System.out.println("\n");
        
        for (int i=0; i<dc.ic.length ; i++){
            if(dc.ic[i].press.contains("길벗")){
                System.out.println(dc.ic[i].title);
            }
        }
    }
}
cs


InfoClass


1
2
3
4
5
6
7
8
9
10
11
12
public class InfoClass {
    String title, author, press;    int price;
    
    public InfoClass() {
    }
    
    public InfoClass(String t, int p, String a, String pr){
        title = t ; price = p ; author = a ; press = pr;
        //System.out.println("InfoClass 생성자 작업 완료");
    }
}
cs


DataClass


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DataClass {
    
    InfoClass ic[] = new InfoClass[6];
        
        public DataClass() {
            
            //System.out.println("DataClass 생성자 작업 완료");
            
            ic[0= new InfoClass("이것이 자바다"30000"신용권""한빛미디어");
            //System.out.println(ic[0].press);
            
            ic[1= new InfoClass("파이썬"25000"홍길동""길벗");
            //System.out.println(ic[1].press);
            
            ic[2= new InfoClass("정보처리기사"35000"기사""길벗");
            ic[3= new InfoClass("HTML"15000"꾸미기""한빛미디어");
            ic[4= new InfoClass("JSP&Servlet"23000"열혈""길벗출판사");
            ic[5= new InfoClass("Oracle"21000"oracle""A길벗출판사");
        }
}
cs

댓글

가장 많이 본 글