-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
27 lines (23 loc) · 700 Bytes
/
Car.java
File metadata and controls
27 lines (23 loc) · 700 Bytes
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
// Define the Car class
public class Car {
String color;
int year;
// Constructor to initialize the object
public Car(String color, int year) {
this.color = color;
this.year = year;
}
// Method
public void displayInfo() {
System.out.println("This is a " + color + " car from " + year + ".");
}
// main method to create and use Car objects
public static void main(String[] args) {
// Create an instance (object) of the Car class
Car myCar = new Car("Red", 2023);
Car anotherCar = new Car("Blue", 2021);
// Call the object's method
myCar.displayInfo();
anotherCar.displayInfo();
}
}