Line data Source code
1 0 : /**
2 :
3 : @file Calculator.java
4 : @brief This file serves as a demonstration file for the Calculator class.
5 : @details This file contains the implementation of the Calculator class, which provides various mathematical operations.
6 : */
7 :
8 : /**
9 :
10 1 : @package com.ucoruh.calculator
11 : @brief The com.ucoruh.calculator package contains all the classes and files related to the Calculator App.
12 : */
13 : package com.ucoruh.calculator;
14 :
15 : import org.slf4j.LoggerFactory;
16 :
17 : import ch.qos.logback.classic.Logger;
18 : /**
19 :
20 : @class Calculator
21 : @brief This class represents a Calculator that performs mathematical operations.
22 : @details The Calculator class provides methods to perform mathematical operations such as addition, subtraction, multiplication, and division. It also supports logging functionality using the logger object.
23 : @author ugur.coruh
24 : */
25 1 : public class Calculator {
26 :
27 : /**
28 : * @brief Logger for the Calculator class.
29 : */
30 1 : private static final Logger logger = (Logger) LoggerFactory.getLogger(Calculator.class);
31 :
32 : /**
33 : * @brief Calculates the sum of two integers.
34 : *
35 : * @details This function takes two integer values, `a` and `b`, and returns their sum. It also logs a message using the logger object.
36 : *
37 : * @param a The first integer value.
38 : * @param b The second integer value.
39 : * @return The sum of `a` and `b`.
40 : */
41 1 : public int add(int a, int b) {
42 : logger.info("add({}, {})", a, b);
43 : return a + b;
44 : }
45 :
46 : /**
47 : * @brief Calculates the difference between two integers.
48 : *
49 : * @param a The value to subtract from.
50 : * @param b The value to subtract.
51 : * @return The result of `a` minus `b`.
52 : */
53 1 : public int subtract(int a, int b) {
54 : logger.info("subtract({}, {})", a, b);
55 : return a - b;
56 : }
57 :
58 : /**
59 : * @brief Calculates the product of two integers.
60 : *
61 : * @param a The first integer value.
62 : * @param b The second integer value.
63 : * @return The product of `a` and `b`.
64 : */
65 1 : public int multiply(int a, int b) {
66 : logger.info("multiply({}, {})", a, b);
67 : return a * b;
68 : }
69 :
70 : /**
71 : * @brief Divides one integer by another.
72 : *
73 : * @details Division is performed as floating point division so that, for
74 : * example, dividing 1 by 3 does not silently truncate to 0.
75 : *
76 : * @param a The dividend.
77 : * @param b The divisor.
78 : * @return The result of `a` divided by `b`.
79 : * @throws ArithmeticException if `b` is zero.
80 : */
81 1 : public double divide(int a, int b) {
82 : logger.info("divide({}, {})", a, b);
83 :
84 : if (b == 0) {
85 : logger.error("divide by zero requested for dividend {}", a);
86 : throw new ArithmeticException("Division by zero");
87 : }
88 :
89 : return (double) a / (double) b;
90 : }
91 : }
|