Skip to content

Commit 99f8e86

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

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
public class MethodsCC2
2-
{
3-
public static void squareAndCube(double x)
4-
{
1+
public class MethodsCC2 {
2+
public static void squareAndCube(double x) {
53
System.out.println(x * x);
64
System.out.println(x * x * x);
75
}
8-
9-
public static void main(String[] args)
10-
{
6+
public static void main(String[] args) {
117
double a = 2.999;
128
double b = 3.333;
139
double c = 4;
@@ -16,4 +12,4 @@ public static void main(String[] args)
1612
squareAndCube(b);
1713
squareAndCube(c);
1814
}
19-
}
15+
}

0 commit comments

Comments
 (0)