220810
한 번에 끝내는 Java/Spring 웹 개발 마스터
package ch14;
public interface Calc {
double PI = 3.14;
int ERROR = -999999999;
int add(int num1, int num2);
int substract(int num1, int num2);
int times(int num1, int num2);
int divide(int num1, int num2);
default void description() {
System.out.println("정수의 사칙연산을 제공합니다.");
}
static int total(int array[]) {
int total = 0;
for(int num: array) {
total += num;
}
return total;
}
}
package ch14;
public abstract class Calculator implements Calc {
@Override
public int add(int num1, int num2) {
return num1 + num2;
}
@Override
public int substract(int num1, int num2) {
return num1 - num2;
}
}
package ch14;
public class CompleteCalc extends Calculator {
@Override
public int times(int num1, int num2) {
return num1 * num2;
}
@Override
public int divide(int num1, int num2) {
if(num2 == 0)
return ERROR;
return num1/num2;
}
public void showInfo() {
System.out.println("모두 구현했습니다.");
}
}
package ch14;
public class CalcTest {
public static void main(String[] args) {
Calc calc = new CompleteCalc();
System.out.println(calc.add(10, 10));
System.out.println(calc.substract(10, 5));
System.out.println(calc.times(10, 10));
System.out.println(calc.divide(10, 10));
calc.description();
int[] arr = {1,2,3,4,5};
System.out.print(Calc.total(arr));
}
}