Skip to content

Commit 98eb17b

Browse files
committed
feat: Add ReturningValues class with add and product instance methods
WHAT the code does: - Defines a `ReturningValues` class with attributes `number1` and `number2`. - Constructor initializes both attributes. - `addToNumbers(int a)` increases both numbers by a given value. - `getProduct()` computes and returns the product of the two numbers. - `main()` demonstrates: - Creating an object with values (1, 2). - Modifying them with `addToNumbers(3)` → numbers become (4, 5). - Printing updated values. - Calculating product (4 × 5 = 20) and printing result. WHY this matters: - Demonstrates **instance methods vs constructor initialization**. - Shows **state mutation** of object attributes through methods. - Introduces returning values from methods instead of only printing. HOW it works: 1. Constructor assigns `1` and `2` to `number1` and `number2`. 2. `addToNumbers(3)` → adds 3 to each, making them `4` and `5`. 3. `getProduct()` → multiplies updated values → `20`. 4. Results are printed step by step. Tips & gotchas: - Fields are package-private (default) → better to make them `private` and use getters for encapsulation. - Multiple calls to `addToNumbers()` keep incrementing values. - Using `int` can overflow for very large numbers; `long` or `BigInteger` may be safer. - `getProduct()` creates a local variable unnecessarily → can directly `return number1 * number2`. Use-cases: - Educational example for **methods returning values**. - Foundation for designing classes that encapsulate arithmetic logic. - Practice with constructors, instance fields, and mutator methods. - Can be extended to more complex math operations. Short key: class-returning values-add-product. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ac2fd3b commit 98eb17b

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
1-
public class ReturningValues
2-
{
3-
//Instance attributes//
1+
public class ReturningValues {
2+
//Instance attributes
43
int number1;
54
int number2;
65

7-
//Constructor//
8-
public ReturningValues (int value1, int value2)
9-
{
6+
//Constructor
7+
public ReturningValues (int value1, int value2) {
108
number1 = value1;
119
number2 = value2;
1210
}
1311

14-
//Instance Methods//
15-
public void addToNumbers(int a)
16-
{
12+
//Instance Methods
13+
public void addToNumbers(int a) {
1714
number1 += a;
1815
number2 += a;
1916
}
2017

21-
public int getProduct()
22-
{
18+
public int getProduct() {
2319
int product = number1 * number2;
2420
return product;
2521
}
2622

27-
public static void main(String[] args)
28-
{
29-
//create the object with initial values
23+
public static void main(String[] args) {
24+
//create the object with initial values.
3025
ReturningValues exampleObject = new ReturningValues(1, 2);
31-
//modify the values through a method
26+
27+
//modify the values through a method.
3228
exampleObject.addToNumbers(3);
3329
System.out.println(exampleObject.number1);
3430
System.out.println(exampleObject.number2);
35-
//calculate some value through another method
31+
32+
//calculate some value through another method.
3633
int number3 = exampleObject.getProduct();
3734
System.out.println(number3);
3835
}
39-
}
36+
}

0 commit comments

Comments
 (0)