-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCar.java
64 lines (52 loc) · 1.55 KB
/
Car.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Day_09_ClassObjects.Example1;
// p4n.in
// codeswithpankaj.com
public class Car{
// Instance variables
private String make;
private String model;
private int year;
// Constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Getter methods
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
// Setter methods
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
// Main method to demonstrate the class
public static void main(String[] args) {
// Create a Car object
Car myCar = new Car("Toyota", "Camry", 2021);
// Use the getter methods to retrieve the object's properties
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());
// Use the setter methods to modify the object's properties
myCar.setMake("Honda");
myCar.setModel("Accord");
myCar.setYear(2022);
// Display the modified properties
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());
}
}