-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava
More file actions
37 lines (31 loc) · 1.01 KB
/
Copy pathJava
File metadata and controls
37 lines (31 loc) · 1.01 KB
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
// Main class name must match the filename (SampleProgram.java)
public class SampleProgram {
// Main method: The entry point of any Java program
public static void main(String[] args) {
// Create an object of the Car class
Car myCar = new Car("Toyota", 2023);
// Call methods on the object
myCar.displayDetails();
myCar.startEngine();
}
}
// A secondary class representing a real-world object
class Car {
// Attributes (Variables)
private String brand;
private int year;
// Constructor to initialize attributes
public Car(String carBrand, int carYear) {
this.brand = carBrand;
this.year = carYear;
}
// Method to display car information
public void displayDetails() {
System.out.println("Car Brand: " + brand);
System.out.println("Model Year: " + year);
}
// Method simulating an action
public void startEngine() {
System.out.println("The " + brand + "'s engine is now running.");
}
}