Skip to content

Commit 0ad057f

Browse files
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

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
//Storing the returned value in a variable.
2-
public class FunctionsCC4
3-
{
4-
public static int calculateSum(int num1, int num2)
5-
{
1+
public class FunctionsCC4 {
2+
public static int calculateSum(int num1, int num2) {
63
return num1 + num2;
74
}
8-
public static void main(String[] args)
9-
{
5+
public static void main(String[] args) {
6+
//Storing the returned value in a variable.
107
int sum = 0;
118
sum = calculateSum(3, 4);
9+
//calling a calculate sum method,
1210
int avg = sum / 2;
1311
System.out.println(avg);
1412
}
15-
}
13+
}

0 commit comments

Comments
 (0)