다음을 참고하여 출력결과를 보고 출력을 하세요
Main.java
package pack_Interface; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Step1. 아래 연산중에서 1개를 선택하세요."); System.out.print("1. 덧셈 \t2. 뺄셈 \t3. 곱셈 \t4. 나눗셈 \n"); Scanner scanner = new Scanner(System.in); System.out.print("선택한 연산 : "); int num = scanner.nextInt(); System.out.println("Step2. 계산에 사용할 숫자 2개를 입력하세요."); double num1 = scanner.nextDouble(); double num2 = scanner.nextDouble(); scanner.close(); Distribute add = new Add(num1, num2); Distribute sub = new Sub(num1, num2); Distribute times = new Times(num1, num2); Distribute div = new Div(num1, num2); System.out.println("------------------------"); if (num==1) { add.calc(); } else if (num ==2) { sub.calc(); } else if (num ==3) { times.calc(); } else { div.calc(); } } }
Distribute.java
// interface package pack_Interface; public interface Distribute { public abstract void calc(); }
Add.java
// 덧셈 package pack_Interface; public class Add implements Distribute { private double num1; private double num2; public Add(double num1, double num2) { this.num1 = num1; this.num2 = num2; } @Override public void calc() { double res = num1 + num2; System.out.println("[처리결과]"); System.out.println("Add 객체가 생성되었습니다."); System.out.println("calc 메서드가 실행되었습니다."); System.out.println("계산결과는 " + res + "입니다."); } }
Sub.java
// 뺄셈 package pack_Interface; public class Sub implements Distribute { private double num1; private double num2; public Sub(double num1, double num2) { this.num1 = num1; this.num2 = num2; } @Override public void calc() { double res = num1 - num2; System.out.println("[처리결과]"); System.out.println("Sub 객체가 생성되었습니다."); System.out.println("calc 메서드가 실행되었습니다."); System.out.println("계산결과는 " + res + "입니다."); } }
Times.java
// 곱셈 package pack_Interface; public class Times implements Distribute { private double num1; private double num2; public Times(double num1, double num2) { this.num1 = num1; this.num2 = num2; } @Override public void calc() { double res = num1 * num2; System.out.println("[처리결과]"); System.out.println("Times 객체가 생성되었습니다."); System.out.println("calc 메서드가 실행되었습니다."); System.out.println("계산결과는 " + res + "입니다."); } }
Div.java
// 나눗셈 package pack_Interface; public class Div implements Distribute { private double num1; private double num2; public Div(double num1, double num2) { this.num1 = num1; this.num2 = num2; } @Override public void calc() { double res = num1 / num2; System.out.println("[처리결과]"); System.out.println("Div 객체가 생성되었습니다."); System.out.println("calc 메서드가 실행되었습니다."); System.out.println("계산결과는 " + res + "입니다."); } }
결과
Add (덧셈)
Sub (뺄셈)
Times (곱셈)
Div (나눗셈)