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;
}
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);
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
Google it, you will find the theories!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With