import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.InputMismatchException; class Number { private double value; Number(double n) { value = n; } double valueOf() { return value; } } class ArithmeticOperations { static Number add(Number a, Number b) { return new Number(a.valueOf() + b.valueOf()); } } class AddTwoNumbers { static void process(double a, double b) { Number n1 = new Number(a), n2 = new Number(b); System.out.println(n1.valueOf() + " + " + n2.valueOf() + " = " + ArithmeticOperations.add(n1, n2).valueOf()); } } class AddTwoNumbersCLI extends AddTwoNumbers { public static void main(String args[]) { if (args.length != 2) { System.err.println("Usage: AddTwoNumbersCLI first-number second-number"); System.exit(1); } try { System.out.println("Taking input from CLI arguments:"); var v1 = Double.parseDouble(args[0]); var v2 = Double.parseDouble(args[1]); process(v1, v2); } catch (NumberFormatException e) { System.err.println("Invalid numbers"); } } } class AddTwoNumbersScan extends AddTwoNumbers { public static void main(String args[]) { try { System.out.println("Taking input using java.util.Scanner:"); var sc = new Scanner(System.in); System.out.print("Enter first number: "); var v1 = sc.nextDouble(); System.out.print("Enter second number: "); var v2 = sc.nextDouble(); process(v1, v2); } catch (InputMismatchException e) { System.err.println("Invalid numbers"); } } } class AddTwoNumbersBuf extends AddTwoNumbers { public static void main(String args[]) { try { System.out.println("Taking input using java.io.BufferedReader:"); var r = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter first number: "); var v1 = Double.parseDouble(r.readLine()); System.out.print("Enter second number: "); var v2 = Double.parseDouble(r.readLine()); process(v1, v2); } catch (NumberFormatException e) { System.err.println("Invalid numbers"); } catch (IOException e) { System.err.println("I/O error occured while reading input."); } } }