[ 작업 환경 ]

 

Windows10

jdk-15

 

 

 

 

 

1. 계산기 class 만들기

 

public class Calculator {
    private int num1, num2;

    Calculator(int a, int b) {
        num1 = a;
        num2 = b;
    }
    public int sum() {
        return num1 + num2;
    }
    public int minus() {
        return num1 - num2;
    }
}

 

 

 

 

 

2. 계산기 class 활용 예시

 

public class Calculator_user {
    public static void main(String[] args) {
        Calculator mycal1 = new Calculator(3, 5);
        System.out.println("Sum : " + mycal1.sum());
        System.out.println("Difference : " + mycal1.minus());
        Calculator mycal2 = new Calculator(7, 2);
        System.out.println("Sum : " + mycal2.sum());
        System.out.println("Difference : " + mycal2.minus());
    }
}

 

 

 

 

 

 

3. 실행 결과

 

javac Calculator.java
javac Calculator_user.java
java Calculator_user

 

 

 

 

+ Recent posts