Skip to content

Commit 6e0ad9b

Browse files
committed
feat: Perform addition using encapsulated methods in Add class [oop-addition-basic]
🔑 Key: oop-addition-basic 🧠 Logic: - Designed a class `Add` that encapsulates input, processing, and output: • `getData()` → reads two integers `a` and `b` from user input. • `getTotal()` → calculates sum of `a + b` and stores it in `total`. • `showData()` → prints the result to console. - Class `ClAdd` serves as the **driver** which creates an object of `Add` and sequentially calls its methods. 📌 Concepts Demonstrated: - ✅ **Object-Oriented Programming** principles: • **Encapsulation**: data and logic grouped in methods inside a class. • **Modularity**: each method performs a distinct task (input, process, output). - ✅ **Scanner class usage** for runtime input. 🧪 Flow of Execution: 1. `Add a = new Add();` → Object creation. 2. `getData()` → Prompts user and accepts input. 3. `getTotal()` → Computes `a + b`. 4. `showData()` → Displays the result. 📎 Use Cases: - This is a basic template for understanding class-based design and separating logic for clarity and reusability. Signed-off-by: Somesh diwan <[email protected]>
1 parent 2c3343d commit 6e0ad9b

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
import java.util.Scanner;
22

3-
class ClAdd
4-
{
5-
public static void main(String[] args)
6-
{
3+
class ClAdd {
4+
public static void main(String[] args) {
75
Add a = new Add();
86
a.getData();
97
a.getTotal();
108
a.showData();
119
}
1210
}
1311

14-
class Add
15-
{
12+
class Add {
1613
int a, b, total;
1714

18-
void getData()
19-
{
15+
void getData() {
2016
Scanner sc = new Scanner(System.in);
2117
System.out.println("Enter values of a & b to perform addition: ");
2218
a = sc.nextInt();
2319
b = sc.nextInt();
2420
}
2521

26-
void getTotal()
27-
{
22+
void getTotal() {
2823
total = a+b;
2924
}
3025

31-
void showData()
32-
{
26+
void showData() {
3327
System.out.println("Addition of a & b is: "+total);
3428
}
3529
}

0 commit comments

Comments
 (0)