풀스택/java

자바 메소드, 함수

lsme 2022. 4. 6. 21:40

8일차(4월 6일) 

 

 

1. 메소드를 이해하기 위해서 차의 속성을 가지고 코드 생성. 

첫번째 코드는 두번째 코드박스에서 불러와서 사용된다. 

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
public class Car {
    //속성
    String carName;
    String CarColor;
    int carYear;
    int carSpeed;
    int displacement;
    
    //메소드 
    //리턴타입 메소드명(데이터타입 파라미터명){
    //실행문;
    //}
    
    //리턴타입: 메소드 기능을 수행한 후 메소드를 호출한 곳으로 반환
    //데이터타입
    //반환하는 데이터가 있으면 리턴타입으로 viod로 지정한다. 
    
    //overloading 오버로딩
    //클래스 내에서 동일한 이림의 메소드를 여러개 정의 할 수 있도록 해주는 기능.
    void run() {
        System.out.println("달린다.");
    }
    void stop() {
        System.out.println("멈춘다.");
    }
    void speedup() {
        carSpeed++;
    }
    void speedup(int velocity) {
        carSpeed+= velocity;
    }
    void speeddown() {
        carSpeed--;
    }
    void speeddown(int velocity) {
        carSpeed-= velocity;
    }
    void start() {
        System.out.println("시동을 건다.");
    }
}
 
cs

 

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
public class CarMain {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car myCar = new Car(); //생성자. //객체를 만들땐 new를 쓴다.
        System.out.println("myCar.carSpeed = " + myCar.carSpeed);
        System.out.println("myCar.carName = " + myCar.carName);
 
        myCar.carSpeed = 2;
        myCar.carName = "포르쉐";
        
        System.out.println("myCar.carSpeed = " + myCar.carSpeed);
        System.out.println("myCar.carName = " + myCar.carName);
        
        System.out.println("===메소드 호출===");
        myCar.run();
        myCar.speedup();
        System.out.println("myCar.speedup()");
        System.out.println("myCar.carSpeed = " + myCar.carSpeed);
        
        myCar.speeddown(100);
        System.out.println("speeddown(100)");
        System.out.println("myCar.carSpeed = " + myCar.carSpeed);
        
        
        Car yourCar = new Car(); //생성자, 객체2
        
        yourCar = myCar; //같은 객체를 가르킨다.
        System.out.println("yourCar = myCar");
        System.out.println("speeddown(100)");
        System.out.println("myCar.carSpeed = " + myCar.carSpeed);
    }
 
}
 
cs

 

 

 

 

 

2. 응용한다. 은행 서비스 만든다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Account {
 
    int balance =0//잔액을 저장하는 속성
    
    void deposit(int account) { //입금처리
        balance += account;
        System.out.println(account + "원이 입금 되었입니다.");
    }
    void withdraw(int account) { //출금처리
        if(balance - account < 0) {
            System.out.println("잔액이 부족합니다.");
        }
        else {
            balance -= account;
            System.out.println(account + "원이 출금되었입니다.");
        }
    }
    void showBalance() { //현재의 잔액을 출력
        //현재 계좌의 잔액은 ()월 입니다.
        System.out.println("현재 계좌의 잔액은 " + balance + "입니다.");
    }
}
cs
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
import java.util.Scanner;
 
public class Banking {
 
    public static void main(String[] args) {
        
        boolean stop = false;
        Account account = new Account();
        Scanner scanner = new Scanner(System.in);
        // TODO Auto-generated method stub
        //프로그램이 실행되면 아래와 같이 메뉴를 출력한다. 
        // 1. 입금
        // 2. 출금
        // 3. 잔액조회
        // 4. 프로그램 종료
        
        //1번이나 2번 메뉴를 선택하면 금액을 보인다. 
        //1번 선택시, 입금액 :
        //2번 선택시, 출금액 :
        //그리고 입출금 처리한다. 
        
        //3번메뉴를 선택하면 현재 잔액을 출력
        //4번메뉴를 선택하면 프로그램 종료.
        
        while(!stop) {
            System.out.println();
            System.out.println("다음을 선택하세요. ");
            System.out.println("1. 입금");
            System.out.println("2. 출금");
            System.out.println("3. 잔액조회");
            System.out.println("4. 프로그램 종료");
            
            int num = scanner.nextInt();
            int money = 0;
            
            switch(num) {
                case 1 : 
                    System.out.print("입금액 : ");
                    money = scanner.nextInt();
                    account.deposit(money);
                    break;
                case 2 :
                    System.out.println("출금액");
                    money = scanner.nextInt();
                    account.withdraw(money);
                    break;
                case 3:
                    account.showBalance();
                    break;
                case 4:
                    System.out.println("종료되었습니다.");
                    stop = true;
            }
        }
    }
}
 
cs