Error appearing when I pas argument in constructer other than integer type


#1

public class Car extends Vehicle{

 float modelmileage;

public Car(float mileage) { //**HERE I HAVE INITIAL CONSTRUCTER HAVING FLOAT TYPE ARGUMENT**

    modelmileage = mileage;

}

public void startEngine() {
    System.out.println("Vroom!");
}

public void drive(int distanceInMiles) {

    System.out.println("Miles driven: " + distanceInMiles);

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Car myFastCar = new Car(55.5); /*_NOW HERE I MADE AN OBJECT USING CONSTRUCTER CALLING_AND PASS FLOAT TYPE ARGUMENT IN IT BUT IT GIVES ME AN ERROR HERE ON 55.5...WHEN I PASS INTEGER TYPE IT DOESNT GIVE ME ERROR .WHY?**
    myFastCar.startEngine();
    myFastCar.drive(1628);
  myFastCar.status();

#2

If you pass a value other than the expected value type (in this case integer) you will get an error. This is why it is sometimes necessary to have "overflowing" methods that each have the same name, but have different types. Or, it may be simpler to give it a double type.

Did this answer your question?


#3

If this is the error message:

Car.java:25: error: constructor Car in class Car cannot be applied to given types;
        Car myFastCar = new Car(55.5); /*_NOW HERE I MADE AN OBJECT USING CONSTRUCTER CALLING_AND PASS FLOAT TYPE ARGUMENT IN IT BUT IT GIVES ME AN ERROR HERE ON 55.5...WHEN I PASS INTEGER TYPE IT DOESNT GIVE ME ERROR .WHY?**/
                        ^
  required: float
  found: double
  reason: argument mismatch; possible lossy conversion from double to float
1 error

Then yes, that's an error, and to find out why you're getting it, read it - it says why!

If not, perhaps I'm getting some other error because I don't have the full code, what you posted isn't a complete example (also it's missing a forward slash and two closing curly braces)


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.