I'm new to programming and also new to Java. What i'm trying right now is using a parameterized constructor, and then use the created objects with a specific method. This is the code i have:
public class Car {
int fuelcap;
int mpg;
Car(int f, int m) { //here
fuelcap = f; //and here
mpg = m; //and here
}
int range() {
return mpg * fuelcap;
}
public static void main(String[] args) {
Car sedan = new Car(16, 21);
Car sportscar = new Car(14, 16);
System.out.println(sedan.range());
System.out.println(sportscar.range());
}
}
The problem is, i don't know why the parameters of the constructor Car - 'int f' and 'int m' are different from the fields: 'int fuelcap;' 'int mpg;'. Can't we just create are constructor like this:
Car(int fuelcap, int mpg){
}
and then just pass values to those parameters when creating the objects?