Commit 8a1de8b
committed
feat: Add ClassBluePrint to model a car with fields, behaviors, and toString override
WHAT the code does:
Defines a ClassBluePrint class representing a car with attributes:
- brand, model, color, year, country of origin, and speed.
Implements two behavior methods:
- accelerate(int increment): increases speed.
- brak(int decrement): decreases speed, ensuring it does not go below zero.
Overrides toString() to return a formatted string of all car properties.
main() demonstrates usage by creating a car, assigning attributes, accelerating, and printing details.
WHY this matters:
Demonstrates the idea of a "class as a blueprint" for objects in Java.
Shows encapsulation of both data (fields) and behavior (methods) within a class.
Introduces method overriding via toString() for customized object printing.
Illustrates how objects can model real-world entities with both properties and actions.
HOW it works:
A ClassBluePrint object is created in main().
Its fields are initialized directly (brand = "TATA", model = "EV", etc.).
accelerate(100) increases the car’s speed.
System.out.println(car) automatically calls toString(), printing the formatted details.
Tips and gotchas:
Field names should typically follow Java naming conventions (e.g., color instead of Color).
Methods brak and accelerate manipulate speed directly; in real-world design, validation may be added (e.g., maximum speed limits).
Direct field access is used here, but encapsulation with getters/setters is preferred for larger systems.
The brak() method name looks like a typo—conventionally it would be brake().
Use-cases:
Educational demo of classes, fields, methods, and object instantiation in Java.
Foundation for object-oriented modeling of real-world entities.
Could be extended to simulate car behavior (e.g., fuel consumption, gear system, or safety checks).
Short key: class-classblueprint car-model fields-methods toString-override.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 92521b0 commit 8a1de8b
File tree
1 file changed
+6
-11
lines changed- Section11ObjectOrientedProgramming/OOPs 2.O/src
1 file changed
+6
-11
lines changedLines changed: 6 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | | - | |
| 11 | + | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | | - | |
19 | | - | |
| 16 | + | |
20 | 17 | | |
21 | | - | |
22 | | - | |
| 18 | + | |
23 | 19 | | |
24 | 20 | | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | | - | |
29 | | - | |
| 24 | + | |
30 | 25 | | |
31 | 26 | | |
32 | 27 | | |
| |||
49 | 44 | | |
50 | 45 | | |
51 | 46 | | |
52 | | - | |
| 47 | + | |
0 commit comments