Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling multiple constructors - Only goes to the same one?

I'm new to Java and I can't figure this out. I've tried passing values in the arguments of my objects, but it only wants to go to the constructor with no parameters. The other 2 have multiple parameters but it doesn't notice it? Thanks in advance!

Here's the code:

    Car vehicle1 = new Car();
    Car vehicle2 = new Car();
    Car vehicle3 = new Car();

public Car() {

    private int numberOfCars;

    private String company;
    private String modelName;
    private String modelYear;
    private double hp;
    private int doors;
    private String gears;
    private String color;
    private boolean racingCar;
    private double price;

        numberOfCars = -1;
        company = "***EMPTY***";
        modelName = "***EMPTY***";
        modelYear = "***EMPTY***";
        hp = -1;
        doors = -1;
        gears = "***EMPTY***";
        color = "***EMPTY***";
        racingCar = false;
        price = -1;
    }
    public Car(String companyName, String modelNa, String modelYe){

        numberOfCars = -1;
        company = companyName;
        modelName = modelNa;
        modelYear = modelYe;
        hp = -1;
        doors = -1;
        gears = "***EMPTY***";
        color = "***EMPTY***";
        racingCar = false;
        price = -1;
    }
    public Car(int carAmount, String companyTitle, String modelTitle, String modelBirth, double horsePower, int door, String gearType, String colors, boolean raceCar, double prices){

        numberOfCars = carAmount;
        company = companyTitle;
        modelName = modelTitle;
        modelYear = modelBirth;
        hp = horsePower;
        doors = door;
        gears = gearType;
        color = colors;
        racingCar = raceCar;
        price = prices;
    }
like image 393
ClaytonR Avatar asked Jun 17 '26 09:06

ClaytonR


2 Answers

In order to call the other constructors, you need to specify parameters when you build your object:

Car vehicle1 = new Car();
Car vehicle2 = new Car("companyName", "modelNa", "modelYe");
Car vehicle3 = new Car(2, "companyTitle", "modelTitle", "modelBirth", 5.2, 4, "gearType", "colors", false, 1600);
like image 188
grebesche Avatar answered Jun 19 '26 21:06

grebesche


This is complete coding for the problem you are facing


class Car
{
  String company;
  String modelName;
  String modelYear;
  double hp;
  int doors;
  String gears;
  String color;
  boolean racingCar;
  double price;
  int numberOfCars;



  public Car() {

    numberOfCars = 1;
    company = "BMW";
    modelName = "BMW M3 DTM";
    modelYear = "2012";
    hp = 500;
    doors = 2;
    gears = "Clutch";
    color = "Black";
    racingCar =true;
    price = -1;
   }

 public Car(String companyName, String modelNa, String modelYe){

    numberOfCars = 1;
    company = companyName;
    modelName = modelNa;
    modelYear = modelYe;
    hp = 500;
    doors = 2;
    gears = "Clutch";
    color = "White";
    racingCar = true;
    price = 100000;

}
public Car(int carAmount, String companyTitle, String modelTitle, String modelBirth, double horsePower, int door, String gearType, String colors, boolean raceCar, double prices){

    numberOfCars = carAmount;
    company = companyTitle;
    modelName = modelTitle;
    modelYear = modelBirth;
    hp = horsePower;
    doors = door;
    gears = gearType;
    color = colors;
    racingCar = raceCar;
    price = prices;

}

public static void main(String args[]) 
 {
Car vehicle1 = new Car();
Car vehicle2 = new Car("BMW", "BMW M3 DTM", "2012");
Car vehicle3 = new Car(1, "BMW", "BMW M3 DTM", "2012", 500, 2, "Clutch", "White", true, 5000000);
}
}

There are some basic facts you should understand before you work with constructors

  • No Parameter- Compiler will search for Method/Constructor with no parameter
  • With Parameter- Compiler will search for Method/Constructor with parameter you specify while calling.
  • Also Type & Number of parameters matters

Google it, you will find the theories!

like image 21
Palak Avatar answered Jun 19 '26 23:06

Palak