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 | |
parent | a914804458e9697f1a9e0430c72ffe941582bbf5 (diff) | |
download | zadania-5ea0ab6272277357c847ce324582e08cff58d134.tar.gz |
Incorporate changes to Java assignment #5
Updating assignment as directed
-rw-r--r-- | java/code/AddTwoNumbers.java | 53 | ||||
-rw-r--r-- | java/output/AddTwoNumbers.typ | 25 | ||||
-rw-r--r-- | java/state.sql | 2 | ||||
-rw-r--r-- | java/text/AddTwoNumbers.typ | 15 |
4 files changed, 26 insertions, 69 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"); } } } - diff --git a/java/output/AddTwoNumbers.typ b/java/output/AddTwoNumbers.typ index 0d3da48..c7102f4 100644 --- a/java/output/AddTwoNumbers.typ +++ b/java/output/AddTwoNumbers.typ @@ -1,35 +1,14 @@ #import "/template.typ": highlight-output -==== Taking input from command line arguments #highlight-output[``` -$ java AddTwoNumbersCLI -Usage: AddTwoNumbersCLI first-number second-number -$ java AddTwoNumbersCLI 5 7 +$ java AddTwoNumbersCLI 5 7 Taking input from CLI arguments: 5.0 + 7.0 = 12.0 -```] -==== Taking input using `java.util.Scanner` -#highlight-output[``` -$ java AddTwoNumbersScan Taking input using java.util.Scanner: -Enter first number: 12 +Enter first number: 12 Enter second number: 7.3 12.0 + 7.3 = 19.3 -$ java AddTwoNumbersScan -Taking input using java.util.Scanner: -Enter first number: 5.1 -Enter second number: 2c -Invalid numbers -```] -==== Taking input using `java.io.BufferedReader` -#highlight-output[``` -$ java AddTwoNumbersBuf Taking input using java.io.BufferedReader: Enter first number: 11.1 Enter second number: 9.6 11.1 + 9.6 = 20.7 -$ java AddTwoNumbersBuf -Taking input using java.io.BufferedReader: -Enter first number: 78 -Enter second number: ab -Invalid numbers ```] diff --git a/java/state.sql b/java/state.sql index 0fffc5b..462d787 100644 --- a/java/state.sql +++ b/java/state.sql @@ -9,7 +9,7 @@ INSERT INTO `assignments` VALUES ('ArraySort', 1, 1, 1, 1), ('StringOperations', 1, 1, 1, 1), ('MatrixOperations', 1, 1, 1, 1), - ('AddTwoNumbers', 0, 0, 0, 1), + ('AddTwoNumbers', 1, 1, 1, 1), ('CylinderCalculations', 1, 1, 1, 1), ('ComplexCalculations', 1, 0, 1, 1), ('FindNumberInArray', 1, 0, 0, 1), diff --git a/java/text/AddTwoNumbers.typ b/java/text/AddTwoNumbers.typ index 0ef86fa..b0d6128 100644 --- a/java/text/AddTwoNumbers.typ +++ b/java/text/AddTwoNumbers.typ @@ -1,7 +1,6 @@ #import "/template.typ": * #show: A => apply(A) #set raw(lang: "java-new") -#set par(leading: 0.575em) #assignment(5)[ Write a program to add two numbers by taking input from command line arguments, the `Scanner` class and the `BufferedReader` class. @@ -17,12 +16,12 @@ - `static double parseDouble(String)`: Try to parse the provided string as a `double` value. - `java.util.Scanner` class: - `double nextDouble()`: Scan a `double` value from the the input stream. -- `java.io.IOException` class represents the exception to be thrown when an error occurs while performing an I/O operation. - `java.io.BufferedReader` class provides for he efficient reading of characters, arrays, and lines from a character-input stream, buffering characters. - Constructor `BufferedReader(Reader)`: Creates a buffering character-input stream for the provided reader stream. - `String readLine()`: Returns a line of text from the input stream. - `java.io.InputStreamReader` class adapts a byte stream into character stream using a specific character set encoding. - Constructor `InputStreamReader(InputStream)`: Creates a reader stream for characters from the provided input stream with the default character set encoding. +- `java.io.IOException` class represents the exception to be thrown when an error occurs while performing an I/O operation. - `java.util.InputMismatchException` is thrown by the methods of `Scanner` class when encountering invalid input. - `java.lang.NumberFormatExeption` is thrown by `parseDouble` method when the provided string does not constitute a valid `double` value. @@ -33,12 +32,10 @@ - `double valueOf()`: Returns the underlying number. - The `ArithmeticOperations` class has methods implementing operations on `Number`s. - `static Number add(Number, Number)`: Adds two `Number`s provided and returns the sum as a `Number`. -- `AddTwoNumbers` class contains the operation of adding when the two numbers have been provided. +- `AddTwoNumbers` class contains the operation of adding when the two numbers have been provided and orchestrates the main program with the `main()` method: - `static void process(double, double)`: Performs addition by creating `Number` objects from the two provided `double` values. -- The `AddTwoNumbersCLI` orchestrates a main program with: - - `public static void main(String[])`: Implements addition of two numbers by taking input from command line arguments. -- The `AddTwoNumbersScan` orchestrates a main program with: - - `public static void main(String[])`: Implements addition of two numbers by taking input utilising `java.util.Scanner`. -- The `AddTwoNumbersBuf` orchestrates a main program with: - - `public static void main(String[])`: Implements addition of two numbers by taking input using `java.io.BufferedReader`. + - `public static void main(String[])`: Implements addition of two numbers by taking input: + - From command line arguments, + - Utilising `java.util.Scanner`, and + - Using `java.io.BufferedReader`. #signature() |