summary refs log tree commit diff stats
path: root/java/text/AddTwoNumbers.typ
blob: 0ef86fa65070aabc3e821064cf2a26ebf7e35d19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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.
]

#scos("AddTwoNumbers")

=== Discussion

#skind[Classes, interfaces and methods used from Java standard library]

- `java.lang.Double` class:
  - `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.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.
  
#skind[Classes and methods implemented in the program]

- The `Number` class objects represents a `double` value.
  - Constructor `Number(double)`: Create a number object for the provided value.
  - `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.
  - `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`.
#signature()