Skip to content

Commit 8a1de8b

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

1 file changed

+6
-11
lines changed

Section11ObjectOrientedProgramming/OOPs 2.O/src/ClassBluePrint.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
public class ClassBluePrint
2-
{
1+
public class ClassBluePrint {
32
String Color;
43
String brand;
54
String Model;
@@ -9,24 +8,20 @@ public class ClassBluePrint
98
int speed;
109

1110
//Method hai. Speed ko increment kar raha hai.
12-
public void accelerate(int increment)
13-
{
11+
public void accelerate(int increment) {
1412
speed += increment;
1513
}
1614

1715
//Behavior hai toh method mai likh te hai
18-
public void brak(int decrement)
19-
{
16+
public void brak(int decrement) {
2017
speed -= decrement;
21-
if(speed<0)
22-
{
18+
if(speed<0) {
2319
speed=0;
2420
}
2521
}
2622

2723
@Override
28-
public String toString()
29-
{
24+
public String toString() {
3025
return
3126
"Car Properties:\n" +
3227
"Brand: " + brand + "\n" +
@@ -49,4 +44,4 @@ public static void main(String[] args) {
4944
System.out.println(car);
5045
System.out.println("Maximum Speed Limit");
5146
}
52-
}
47+
}

0 commit comments

Comments
 (0)