-2

I am fairly new at this and have been looking for a solution on Internet for two days, yet, I could not find any.

This is the class that I identified and initialized variables.

 package HelloWorld;

    import java.awt.*;


    public class Car {

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


        public Car(double inputAverageMPG, 
                    String inputLicensePlate, 
                    Color inputPaintColor, 
                    boolean inputAreTaillightsWorking) {

            this.averageMilesPerGallon = inputAverageMPG;
            this.licensePlate = inputLicensePlate;
            this.paintColor = inputPaintColor;
            this.areTaillightsWorking = inputAreTaillightsWorking;  
        }



    }

Then, I wanted use these variables in my main class; however, it did not work. I received an error that was saying; "inputAverageMPG cannot be resolved to a variable" and "inputLicensePlate cannot be resolved to a variable." Please refer below to see the main class wherein I received the error.

package HelloWorld;

public class Main {


    public static void main(String[] args) {
        System.out.println("yes");



        Car myCar = new Car(inputAverageMPG = 25.5, inputLicensePlate "12HTHF");




        }


    }
  • You seem to be confusing Java syntax with Python ... or something. Java doesn't support "paramName = value" syntax in a call. Please refer to your lecture notes / tutorial / textbook on how to pass parameters in a method or constructor call. – Stephen C Feb 26 '19 at 01:39
  • So when you attempt to do that in Java, the compiler interprets `name = value` as an assignment expression and looks for a variable called `name` ... which doesn't exist in your example. (Just as well. If it did exist, the resulting mess wold be horrible to diagnose!) – Stephen C Feb 26 '19 at 01:47

3 Answers3

1
 Car myCar = new Car(25.5,"12HTHF");

or create variables first before you use

package HelloWorld;

public class Main {


public static void main(String[] args) {
    System.out.println("yes");



    Car myCar = new Car(25.5,"12HTHF");




    }


}



enter code here
0

Remove the various inputEtc = calls in your constructor use inside the Main class, you do not have to do this to specify parameters. An example:

//25.5 MPG Car
Car myCar = new Car(25.5, /* other input parameters */);

If you're at this point and were stuck for this long, I would recommend Oracle's Official Tutorial

Rogue
  • 11,105
  • 5
  • 45
  • 71
-2

You need to create theses variables before use them. At first sight : Append this lines before creation of myCar object

`double inputAverageMPG=25.5;
 String inputLicencePlate=12HTHF"`
nissim abehcera
  • 821
  • 6
  • 7