diff options
author | Sudipto Mallick <smlckz@bccr> | 2024-01-10 13:53:54 +0530 |
---|---|---|
committer | Sudipto Mallick <smlckz@bccr> | 2024-01-10 13:53:54 +0530 |
commit | 5ea0ab6272277357c847ce324582e08cff58d134 (patch) | |
tree | 870571d13f1fb6f0135463e1d1d71e05dbd0c641 /java/code | |
parent | a914804458e9697f1a9e0430c72ffe941582bbf5 (diff) | |
download | zadania-5ea0ab6272277357c847ce324582e08cff58d134.tar.gz |
Incorporate changes to Java assignment #5
Updating assignment as directed
Diffstat (limited to 'java/code')
-rw-r--r-- | java/code/AddTwoNumbers.java | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/java/code/AddTwoNumbers.java b/java/code/AddTwoNumbers.java index 3c1b3e8..7f10c23 100644 --- a/java/code/AddTwoNumbers.java +++ b/java/code/AddTwoNumbers.java @@ -26,56 +26,37 @@ class AddTwoNumbers { 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 { + if (args.length != 2) { + System.err.println("Usage: AddTwoNumbersCLI first-number second-number"); + } else { + System.out.println("Taking input from CLI arguments:"); + var v1 = Double.parseDouble(args[0]); + var v2 = Double.parseDouble(args[1]); + process(v1, v2); + } + double v1, v2; 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(); + v1 = sc.nextDouble(); System.out.print("Enter second number: "); - var v2 = sc.nextDouble(); + 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()); + v1 = Double.parseDouble(r.readLine()); System.out.print("Enter second number: "); - var v2 = Double.parseDouble(r.readLine()); + 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."); + } catch (InputMismatchException e) { + System.err.println("Invalid numbers"); + } catch (NumberFormatException e) { + System.err.println("Invalid numbers"); } } } - |