繁体   English   中英

用Java计算汽车行驶距离的类

[英]A class to calculate the range of a car in java

因此,我正在做家庭作业,但是在addGas()和drive()中不断收到返回值警告。 但是,即使添加return语句,我也会得到错误的答案或NaN。 这是我的第一个Java编程类,因此我假设我的麻烦所在是理解,因此请解释一下,而不仅是提供解决方法! 谢谢!

我的车类:

public class Car
{
    // instance variables - replace the example below with your own
    private double milesPerGallon;
    private double fuel;

    /**
     * Constructor for objects of class Car
     */
    public Car(double milesPerGallon)
    {
        // initializes all instance varibles to starting values
        this.milesPerGallon = milesPerGallon;
        //set gas to initial 3.5
        fuel = 3.5;
    }

    /**
     * adding gass to instance variable 
     */
    public double addGas(double fuel) // takes one parameter from tester, the amount of gas to add to the tank
    {
        // increases the amount of gas in the tank 
        this.fuel = this.fuel + fuel;
    }

    /**
     * What is the remaining gas? 
     */
    public double drive(double distance) // takes one parameter from tester, the distence in miles that were driven
    {
        // calculating the gas
        // decreases the amount of gas in the tank
        double leftOverFuel = fuel * milesPerGallon - distance;
        //distance
        fuel = (distance / milesPerGallon);
        // update the instance variabl
    }

    /**
     * When will the car get empty fuel?
     */
    public double range()
    {
        // calculates range, the number of miles the car can travel until the gas tank is empty
        double leftOverFuel;
        leftOverFuel = fuel * milesPerGallon;
        // returns the calculations 
        return leftOverFuel;
    }

}

CarTester类:

public class CarTester
{
    /**
     * main() method
     */
    public static void main(String[] args)
    {
        Car honda = new Car(30.0);      // 30 miles per gallon

        honda.addGas(9.0);              // add 9 more gallons
        honda.drive(210.0);             // drive 210 miles

        // print range remaining
        System.out.println("Honda range remaining: " + honda.range());

        Car toyota = new Car(26.0);      // 26 miles per gallon

        toyota.addGas(4.5);              // add 4.5 more gallons
        toyota.drive(150.0);             // drive 150 miles

        // print range remaining
        System.out.println("Toyota range remaining: " + toyota.range());
    }
}

drive()addGas()的返回类型从double更改为void

像这样更改方法签名:

public void addGas(double fuel) {
    this.fuel = this.fuel + fuel;   
}

public void drive(double distance) {
    double leftOverDistance = fuel * milesPerGallon - distance;
    this.fuel = (leftOverDistance / milesPerGallon);
}

说明:

出现add return语句错误的原因是,您已将方法定义为返回double值,但未返回任何值。

在您的特定情况下,return语句无用。 addGas ,您将作为参数传递的fuel添加到this.fuel字段中,因此没有返回值。 drive()您要为字段this.fuel设置新的计算值,同样也没有返回值。 因此,对于这些方法,无效返回类型是合理的返回类型。

顺便说一下,您的drive()方法似乎逻辑错误。 检查我的版本。

语法错误的第一件事是在drive()和addGas()中。 您将返回类型设置为双精度类型,但未返回任何内容。 解决方法是:

public void addGas(double gas){/*Code Here*/} // Void means that there won't return value
public void drive(double distance){/*Code Here*/} // Same as addGas()

我注意到的另一件事,这并不是什么大问题,但是我的OCD使我不得不告诉你,当您在drive()方法中从储罐中除去气体时,您创建了一个名为leftOverFuel的双用于任何东西。 另外,您将当前的燃油水平设置为MPG /距离,这绝对没有用。 您想从当前燃料中减去使用量。 尝试这个:

public void drive(double distance){
    double fuelUsed = distance / milesPerGallon;
    fuel = fuel - fuelUsed;
}

希望对您有所帮助! 感谢您的阅读。

/ *您还可以使用这些类来计算汽车价值。 但是,此方法不会返回负值。 * /

public class CarWorthSrv {

 public int carWorth (int year, int initVal, int mileage)
 {
    int currentYear=2016;
    final int DEP_VALUE = 1000;
    year = currentYear - year;
    double yearlyMileage = (mileage*1.0)/year;
    int value = initVal-DEP_VALUE;

    if(year==currentYear){
        value = value;
    }
    else if(year<currentYear){
        if(yearlyMileage>=10000&&yearlyMileage<=12000){
            value = value -(1400*year);
            if(value<0){
                value =0;
            }
        }
        else if(yearlyMileage>12000){
            value = value -(1550*year);
        }
        else if(yearlyMileage<10000){
            value = value -(1300*year);
        }
    }



    return value;
 }




 }
public class CarWorthDrv
 {
 public static void main(String[] args) {

    CarWorthSrv srvObj = new CarWorthSrv();

    System.out.println();
    System.out.print("Testing a 2009 vehicle.  Purchase price: $22,000 with  89,000 miles: ");
    int result = srvObj.carWorth(2009, 22000, 89000); 

    if (result == 10150)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 10150.  your method returned: " + result + "\n");
    }

    System.out.print("Testing a 2011 vehicle.  Purchase price: $29,000 with  54,500 miles: ");
    result = srvObj.carWorth(2011, 29000, 54500); 

    if (result == 21000)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 21000.  your method returned: " + result + "\n");
    }


    System.out.print("Testing a 2012 vehicle.  Purchase price: $34,400 with  24,700 miles: ");
    result = srvObj.carWorth(2012, 34400, 24700); 

    if (result == 28200)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 28200.  your method returned: " + result + "\n");
    }


    System.out.print("Testing a 2013 vehicle.  Purchase price: $32,000 with  30,000 miles: ");
    result = srvObj.carWorth(2013, 32000, 30000); 
    System.out.println( (result == 26800)? "PASS":"*** FAIL ***\nshould be 26800. Your method returned: " + result + "\n");

    System.out.print("Testing a 2014 vehicle.  Purchase price: $29,900 with  24,000 miles: ");
    result = srvObj.carWorth(2014, 29900, 24000); 
    System.out.println( (result == 26100)? "PASS":"*** FAIL ***\nshould be 26100. Your method returned: " + result + "\n");

    System.out.print("Testing a 2012 vehicle.  Purchase price: $28,900 with  48,002 miles: ");
    result = srvObj.carWorth(2012, 28900, 48002); 
    System.out.println( (result == 21700)? "PASS":"*** FAIL ***\nshould be 21700. Your method returned: " + result + "\n");

    System.out.print("Testing a 2016 vehicle.  Purchase price: $35,900 with   1,200 miles: ");
    result = srvObj.carWorth(2016, 35900, 1200); 
    System.out.println( (result == 34900)? "PASS":"*** FAIL ***\nshould be 34900. Your method returned: " + result + "\n");

    System.out.print("Testing a 1997 vehicle.  Purchase price: $19,000 with 210,000 miles: ");
    result = srvObj.carWorth(1997, 19000, 210000); 
    System.out.println( (result == 0)? "PASS":"*** FAIL ***\nshould be 0. Your method returned: " + result + "\n");

    System.out.print("Testing a 2002 vehicle.  Purchase price: $19,200 with 130,000 miles: ");
    result = srvObj.carWorth(2002, 19200, 130000); 
    System.out.println( (result == 0)? "PASS":"*** FAIL ***\nshould be 0. Your method returned: " + result + "\n");


    System.out.println();

 } //main 

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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