Tools: VSCode | Source Code: GitHub
The CarDemo
class serves as a straightforward example of defining and utilizing a class in Java. It encapsulates the basic attributes of a car: modelDemo
, horsepDemo
, and yearDemo
, which represent the model name, horsepower, and manufacturing year, respectively. The class includes a constructor that initializes these attributes, allowing for the creation of car objects with specific details. In the main
method, an instance of CarDemo
is created with sample values for model, horsepower, and year. The program then outputs these details to the console, demonstrating the functionality of the class and its ability to manage and display car information. This simple design illustrates key concepts in object-oriented programming, including class construction and attribute management, making it an excellent example for beginners to understand the fundamentals of Java programming.
public class CarDemo {
private String modelDemo;
private int horsepDemo;
private int yearDemo;
public CarDemo(String model, int horse, int year) {
modelDemo = model;
horsepDemo = horse;
yearDemo = year;
}
public static void main(String[] args) {
CarDemo supra = new CarDemo("model", 11, 11);
System.out.println("Model: " + supra.modelDemo + ", Horsepower: " + supra.horsepDemo + ", Year: " + supra.yearDemo);
}
}