Commit 0ad057f
committed
feat: Add FunctionsCC4 class with calculateSum and average calculation
WHAT the code does:
- Defines a `FunctionsCC4` class with a static method `calculateSum(int num1, int num2)`.
- `calculateSum()` returns the sum of two integers.
- In `main()`:
- Calls `calculateSum(3, 4)` and stores result in `sum`.
- Computes average as `sum / 2`.
- Prints the average.
WHY this matters:
- Demonstrates **storing return values** in variables for further use.
- Shows chaining of operations: using method output to calculate another result.
- Reinforces return values vs intermediate computation inside main.
HOW it works:
1. `calculateSum(3, 4)` → returns `7`.
2. `sum = 7`.
3. `avg = 7 / 2` → integer division gives `3`.
4. Prints `3`.
Tips & gotchas:
- Since `avg` is `int`, fractional parts are truncated.
→ Use `double` for more accurate averages (`(double) sum / 2`).
- `int sum = 0;` is redundant before assignment — can be simplified.
- Method name could be generalized to `add()` if reused in other contexts.
Use-cases:
- Educational example for **return values + further calculations**.
- Basis for programs needing averages, totals, or aggregates.
- Useful for explaining integer division vs floating-point division in Java.
- Starter code for building math utility libraries.
Short key: class-functions cc4-sum-average.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent bf0c63a commit 0ad057f
1 file changed
+6
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
6 | 3 | | |
7 | 4 | | |
8 | | - | |
9 | | - | |
| 5 | + | |
| 6 | + | |
10 | 7 | | |
11 | 8 | | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | | - | |
| 13 | + | |
0 commit comments