Creating a Java Car Class with Parameterized Constructor and Default Values
Constructor with Default Values
Write a Java program to create a class called Car with instance variables make, model, and year. Implement a parameterized constructor that initializes these variables and assigns default values if not provided. Print the values of the variables.
Sample Solution:
Java Code:
Car.java
// Define the Car class
public class Car {
// Private instance variables
private String make;
private String model;
private int year;
// Parameterized constructor with default values
public Car(String make, String model, int year) {
// Initialize make with the provided parameter or a default value
this.make = (make == null || make.isEmpty()) ? "Unknown Make" : make;
// Initialize model with the provided parameter or a default value
this.model = (model == null || model.isEmpty()) ? "Unknown Model" : model;
// Initialize year with the provided parameter or a default value
this.year = (year <= 0) ? 2000 : year;
}
// Main method to test the Car class
public static void main(String[] args) {
// Create a new Car object with valid data
Car car1 = new Car("Toyota", "Corolla", 2021);
// Print the values of the instance variables for car1
System.out.println("Car 1 Make: " + car1.make);
System.out.println("Car 1 Model: " + car1.model);
System.out.println("Car 1 Year: " + car1.year);
// Create a new Car object with some invalid data
Car car2 = new Car("", "", -1);
// Print the values of the instance variables for car2
System.out.println("Car 2 Make: " + car2.make);
System.out.println("Car 2 Model: " + car2.model);
System.out.println("Car 2 Year: " + car2.year);
}
}
Output:
Car 1 Make: Toyota Car 1 Model: Corolla Car 1 Year: 2021 Car 2 Make: Unknown Make Car 2 Model: Unknown Model Car 2 Year: 2000
Explanation:
- Define the Car class:
- The Car class is defined with the keyword class.
- Private instance variables:
- The Car class has three private instance variables: make (a String), model (a String), and year (an int).
- Parameterized constructor with default values:
- The parameterized constructor Car(String make, String model, int year) is defined.
- Inside the constructor:
- Initialize make: If the provided make parameter is null or empty, set this.make to "Unknown Make". Otherwise, set it to the provided make.
- Initialize model: If the provided model parameter is null or empty, set this.model to "Unknown Model". Otherwise, set it to the provided model.
- Initialize year: If the provided year parameter is less than or equal to 0, set this.year to 2000. Otherwise, set it to the provided year.
- Main method:
- The main method is defined to test the Car class.
- Valid data: A Car object (car1) is created with valid data. The values of the instance variables are printed to the console.
- Invalid data: A Car object (car2) is created with some invalid data (empty strings and a negative year). The values of the instance variables are printed to the console, demonstrating the use of default values.
Note on Constructors:
In the above exercise, the constructors for the Car class work by:
- Parameterized Constructor with Default Values: Initializes the make, model, and year instance variables with specific values provided as arguments, or assigns default values if the provided arguments are invalid (null, empty, or negative).
- Encapsulation: By keeping the instance variables private and initializing them through constructors with validation and default values, the class ensures controlled initialization, data integrity, and appropriate handling of invalid input, encapsulating the logic for setting values and enforcing constraints within the class itself.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Constructor Previous: Creating a Java Account Class with Validated Parameterized Constructor.
Java Constructor Next: Creating a Java Point Class with Overloaded Constructors.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics