Jeh Hayer
Jeh Hayer

Reputation: 13

Difficulty with User-defined functions

I'm new to Java and trying to learn some simple Java but am having some difficulty with the constructor. It seems that the constructor and properties I set are not being picked up by the Main Java class from the Cars Class. Even though I have used "Cars" to reference in the Main Class. It also shows "Cannot resolve symbol...." which is how I came to my conclusion. What could be causing this?

Car.java file

import java.awt.*;

public class Car {

    double averageMilesPerGallon;
    String licensePlate;
    Color paintColor;
    boolean areTailingWorking;

    public Car(double inputAverageMPG,
               String inputLicensePlate,
               Color inputPaintColor,
               boolean inputAreTaillightsWorking) {
        this.averageMilesPerGallon = inputAverageMPG;
        this.licensePlate = inputLicensePlate;
        this.paintColor = inputPaintColor;
        this.areTailingWorking = inputAreTaillightsWorking;

    }
}

Main.java file

public class Main {

    public static void main(String[] args) {


        Car myCar = new Car(inputAverageMPG: 25.5,
                inputLicensePlate:"1B32E",
                Color.BLUE,
                inputAreTaillightsWorking: true);

        Car sallyCar = new Car(inputAveraMPG: 13.9,
                inputLicensePlate: "1G42D",
                Color.BLACK.
                        inputAreTaillightsWorking: false);

        System.out.println("My car's license plate: " + myCar.licensePlate);
        System.out.println("Sally's License Plate: " + sallyCar.licensePlate);
    }
}

Upvotes: 0

Views: 53

Answers (1)

Leo Aso
Leo Aso

Reputation: 12513

Java does not have named arguments. Instead of doing this

    Car myCar = new Car(inputAverageMPG: 25.5,
            inputLicensePlate:"1B32E",
            Color.BLUE,
            inputAreTaillightsWorking: true);

    Car sallyCar = new Car(inputAveraMPG: 13.9,
            inputLicensePlate: "1G42D",
            Color.BLACK.
                    inputAreTaillightsWorking: false);

which is not valid Java code, just pass the arguments in, without any prefix or anything like that.

    Car myCar = new Car(25.5, "1B32E", Color.BLUE, true);
    Car sallyCar = new Car(13.9, "1G42D", Color.BLACK, false);

Upvotes: 2

Related Questions