0% found this document useful (0 votes)
12 views

Constructor Example

Uploaded by

Juan Dela Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Constructor Example

Uploaded by

Juan Dela Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

public class Car6 {

String make;
String model;
int year;
int speed;

//Default constructor
public Car6(){
this.make = "None";
this.model = "None";
this.year = 0;
this.speed = 0;
}

//Overloaded constructor with make and model parameter


public Car6(String make, String model){
this.make = make;
this.model = model;
this.year = 0;
this.speed = 0;
}

//Overloaded constructor with make, model and year parameter


public Car6(String make, String model, int year){
this.make = make;
this.model = model;
this.year = year;
this.speed = 0;
}

//Overloaded constructor with make, model, year and speed parameter


public Car6(String make, String model, int year, int speed){
this.make = make;
this.model = model;
this.year = year;
this.speed = speed;
}

//Getter
public String getMake(){
return make;
}

public String getModel(){


return model;
}
public int getYear(){
return year;
}

public int getSpeed(){


return speed;
}

//setter
public void setMake(String make){
this.make=make;
}

public void setModel(String model){


this.model=model;
}

public void setYear(int year){


this.year=year;
}

public void setSpeed(int speed){


this.speed=speed;
}

public static void main(String[] args) {


// Create a Car object using the default constructor
Car6 myCar1 = new Car6();

// Create a Car object using the constructor with make and model parameters
Car6 myCar2 = new Car6("Honda", "Civic");

// Create a Car object using the constructor with make, model, and year parameters
Car6 myCar3 = new Car6("Toyota", "Camry", 2021);

// Create a Car object using the constructor with make, model, year, and speed parameters
Car6 myCar4 = new Car6("Ford", "Mustang", 2019, 120);

// Print out the details of each car object


System.out.println("Car 1:");
System.out.println("Make: " + myCar1.getMake());
System.out.println("Model: " + myCar1.getModel());
System.out.println("Year: " + myCar1.getYear());
System.out.println("Speed: " + myCar1.getSpeed());
System.out.println("\nCar 2:");
System.out.println("Make: " + myCar2.getMake());
System.out.println("Model: " + myCar2.getModel());
System.out.println("Year: " + myCar2.getYear());
System.out.println("Speed: " + myCar2.getSpeed());

System.out.println("\nCar 3:");
System.out.println("Make: " + myCar3.getMake());
System.out.println("Model: " + myCar3.getModel());
System.out.println("Year: " + myCar3.getYear());
System.out.println("Speed: " + myCar3.getSpeed());

System.out.println("\nCar 4:");
System.out.println("Make: " + myCar4.getMake());
System.out.println("Model: " + myCar4.getModel());
System.out.println("Year: " + myCar4.getYear());
System.out.println("Speed: " + myCar4.getSpeed());

}
}

You might also like