简体   繁体   中英

How can I pass argument in the constructor?

I am working on a simple Java homework. It seems like I got the syntax and logic right. However, the constructor can't understand the argument that I try to pass in that's why all the calculation is wrong. I have enclosed the output. Anyone knows how to fix this problem? Any help will be appreciated. Thank you!

public class DT_CarStats
 {
   private double gallons;
   private double mpg;
   private double ppg;

public DT_CarStats(double gallons, double mpg, double ppg)
{
  gallons = gallons;
  mpg = mpg;
  ppg = ppg;
}

public double CostPer100()
{ 
  return 100 / (mpg * ppg);
}

public double MaxDistance()
{
  return mpg * gallons;
}
}
import java.util.Scanner;

public class DT_CarStatsTester
{
  public static void main(String[] args)
  {
    Scanner scannerObject = new Scanner(System.in);
    double gallons, mpg, ppg;
    System.out.printf("******************************************* \n");
    System.out.printf("* Welcome to my Distance to Empty App  * \n");
    System.out.printf("******************************************* \n");
    System.out.printf("Please enter the number of gallons of gas in the     tank: ");
    gallons = scannerObject.nextDouble();
    System.out.printf("Please enter the fuel efficiency (miles per gallon): ");
    mpg = scannerObject.nextDouble();
    System.out.printf("Please enter the price of gas per gallon: ");
    ppg = scannerObject.nextDouble();
    DT_CarStats cs = new DT_CarStats(gallons,mpg,ppg);
    System.out.printf("\n");
    System.out.printf("To drive 100 miles, it will cost $" + cs.CostPer100() + ". \n");
    System.out.printf("The car can currently drive a maximum of " + cs.MaxDistance() + " miles. \n");
    System.out.printf("******************************************* \n");
    System.out.printf("* Thanks for using our App, Safe Travels! * \n");
    System.out.printf("******************************************* \n");
  }
}

Sample run:

******************************************* 
******************************************* 
* Welcome to my Distance to Empty App  * 
******************************************* 
Please enter the number of gallons of gas in the tank: 17.6
Please enter the fuel efficiency (miles per gallon): 24.8
Please enter the price of gas per gallon: 2.36

To drive 100 miles, it will cost $Infinity. ==> This is wrong, should be $9.52 
The car can currently drive a maximum of 0.0 miles. ==> This should be 436.48 miles
******************************************* 
* Thanks for using our App, Safe Travels! * 
******************************************* 

Use this to specify that you mean the class field, and not constructor argument, since they are named the same.

public DT_CarStats(double gallons, double mpg, double ppg)
{
  this.gallons = gallons;
  this.mpg = mpg;
  this.ppg = ppg;
}
gallons = gallons;
mpg = mpg;
ppg = ppg;

Hint: in this block of code, what are gallons , mpg and ppg ?

They're the parameters! They are always the parameters, no matter which side of an = they're on. Variable names on the left side of an = are not treated specially.

To access the fields, use this :

this.gallons = gallons;
this.mpg = mpg;
this.ppg = ppg;

or give the parameters different names from the fields.

Here the instance variables are being overridden by the method parameters. So to differentiate instance variables with method parameters (when both names are same) use the "this" keyword inside the constructor.

 this.gallons = gallons;

In your case, method parameters are again assigned to a value, leaving instance variables untouched, hence wrong values

In your constructor you're referring to your fields with the same name as the incoming parameters. You should use the "this" keyword to ensure you're setting your object's variables this.gallons = gallons etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM