Skip to content

Commit 4e9b93c

Browse files
committed
feat: Add MethodsCC class with squareAndCube method
WHAT the code does: - Defines a `MethodsCC` class with a static method `squareAndCube(int x)`. - `squareAndCube()` prints the square and cube of the input integer. - `main()` calls this method for values `2`, `3`, and `4`. WHY this matters: - Demonstrates **methods with parameters** for reusable logic. - Reinforces arithmetic operations and function abstraction. - Shows how the same method can be reused for multiple inputs. HOW it works: 1. `squareAndCube(2)` → prints `4` and `8`. 2. `squareAndCube(3)` → prints `9` and `27`. 3. `squareAndCube(4)` → prints `16` and `64`. Tips & gotchas: - Current implementation prints directly — could be enhanced to return results for reuse. - Works fine for small integers; for large values, consider `long` or `BigInteger` to avoid overflow. - Method naming (`squareAndCube`) is descriptive and follows good practice. Use-cases: - Educational demo for **methods with arguments**. - Useful in teaching function reusability in math-related problems. - Can be extended into a math utility library. - Good exercise for beginners in Java. Short key: class-methodscc-square-cube. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent c4c50b5 commit 4e9b93c

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
class MethodsCC
2-
{
3-
public static void squareAndCube(int x)
4-
{
1+
class MethodsCC {
2+
public static void squareAndCube(int x) {
53
System.out.println(x * x);
64
System.out.println(x * x * x);
75
}
8-
public static void main (String[] args)
9-
{
6+
public static void main (String[] args) {
107
int a = 2;
118
int b = 3;
129
int c = 4;
@@ -15,32 +12,40 @@ public static void main (String[] args)
1512
squareAndCube(c);
1613
}
1714
}
15+
1816
/*
1917
why we use x in System.out.println.
2018
21-
In the method squareAndCube(int x), the x is a parameter. It acts as a placeholder for the value that you pass to the method when you call it.
19+
In the method squareAndCube(int x), the x is a parameter.
20+
It acts as a placeholder for the value that you pass to the method when you call it.
2221
2322
How it works:
2423
When you call squareAndCube(a), the value of a (which is 2) is passed to the method.
2524
Inside the method, x takes the value of a, so x = 2.
2625
Now, when you write System.out.println(x * x), it calculates 2 * 2 and prints 4.
2726
Similarly, System.out.println(x * x * x) calculates 2 * 2 * 2 and prints 8.
27+
2828
Why use x?
29-
x is a variable that holds the value passed to the method. It allows the method to work with any value you pass, making the method reusable.
29+
x is a variable that holds the value passed to the method.
30+
It allows the method to work with any value you pass, making the method reusable.
31+
3032
For example, when you call squareAndCube(b), x becomes 3, and when you call squareAndCube(c), x becomes 4.
31-
Key Takeaway:
32-
x is just a placeholder for the value you pass to the method. It makes the method flexible and reusable for different inputs.
33-
*/
3433
34+
Key Takeaway:
35+
x is just a placeholder for the value you pass to the method.
36+
It makes the method flexible and reusable for different inputs.
3537
36-
/*
3738
Methods improve code in several ways:
3839
39-
Reusability: You can write a method once and call it multiple times, avoiding code duplication. For example, in the given problem, squareAndCube is called three times instead of writing the same logic three times.
40+
Reusability: You can write a method once and call it multiple times, avoiding code duplication.
41+
For example, in the given problem, squareAndCube is called three times instead of writing the same logic three times.
42+
Modularity: Methods break down complex tasks into smaller, manageable parts.
43+
This makes the code easier to understand, debug, and maintain.
4044
41-
Modularity: Methods break down complex tasks into smaller, manageable parts. This makes the code easier to understand, debug, and maintain.
45+
Readability: By giving meaningful names to methods, your code becomes more readable.
4246
43-
Readability: By giving meaningful names to methods, your code becomes more readable. For example, squareAndCube clearly indicates what the method does.
47+
For example, squareAndCube clearly indicates what the method does.
4448
45-
Maintainability: If you need to change the logic, you only need to update it in one place (the method), rather than in multiple places.
46-
*/
49+
Maintainability: If you need to change the logic, you only need to update it in one place (the method),
50+
rather than in multiple places.
51+
*/

0 commit comments

Comments
 (0)