flame123536
flame123536

Reputation: 41

Java car class for beginner

I must Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a method drive that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank. Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank, and addGas, to add gasoline to the fuel tank.

I have created a class for the car and a test program to plug some values in and when i run the program all i get returned is the addGas value that i put in. the computation for the miles per gallon will not run and i do not understand why. as you can probably tell i am VERY new at java and any help on the issue is much appreciated.

    public class CarTest
{
public static void main(String[] args)
  {
    Car richCar = new Car(49);

    richCar.addGas(15);
    richCar.drive(150);
    System.out.println(richCar.getGas());
  }
}





    /**
A car can drive and consume fuel
*/
public class Car
{
/**
  Constructs a car with a given fuel efficiency
  @param anEfficiency the fuel efficiency of the car
*/
public Car(double mpg)
{
  milesPerGallon = mpg;
  gas = 0;
  drive = 0;
}

 /** Adds gas to the tank.
   @param amount the amount of fuel added
 */
 public void addGas(double amount)
 {
  gas = gas + amount;
 }

 /**
   Drives a certain amount, consuming gas
   @param distance the distance driven
 */
 public void drive(double distance)
 {
  drive = drive + distance;
 }

 /**
   Gets the amount of gas left in the tank.
   @return the amount of gas
  */

 public double getGas()
 {
  double mpg;
  double distance;
  distance = drive;
  mpg = gas * milesPerGallon / distance;
  return gas;
 }

  private double drive;
  private double gas;
  private double milesPerGallon;
}

Upvotes: 3

Views: 19774

Answers (4)

shirrine
shirrine

Reputation: 403

Combining some ideas...

/**
Drives a certain amount, consuming gas
@param distance the distance driven
*/
public void drive(double distance)
{
    drive = drive + distance;
    gas = gas - (distance / milesPerGallon);
}

/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/

public double getGas()
{
    return gas;
}

Upvotes: 0

Makoto
Makoto

Reputation: 106430

It's computing it just fine. Your method is only returning the one value for gas, though.

This could use some clean-up; I'd suggest getting rid of all of the cruft, and just returning the calculation.

public double calculateGas() {
    return (gas * milesPerGallon) / drive;
}

Upvotes: 0

NESPowerGlove
NESPowerGlove

Reputation: 5496

Your test program only calls three methods:

richCar.addGas(15);
richCar.drive(150);
System.out.println(richCar.getGas());

Let's take a look at what each one does.

addGas(15) modifies your gas instance variable.

drive(150) only executes the line drive = drive + distance;

getGas() seems to do a couple things, more than expected (one would expect it just returns gas but it also calculates and stores latest mpg value). But if you check the code of getGas() gas is never modified, only returned.

So you called three methods, and within those three executions gas is only modified once, when you set it to the value of 15 with richCar.addGas(15).

It would seem most simple if the drive(int) method modifys gas based on some set mpg value, instead of only keeping track of how many miles were driven, and then you could have drive() simply just return gas.

That means you would also get rid of modifying mpg in getGas(), which is good, because that value is needed no matter what to calculate how much gas is used. You could perhaps introduce a actualMpg value if you had some more logic that changed how much gas is used but you don't have that yet.

Upvotes: 0

DotNet NF
DotNet NF

Reputation: 833

From what it looks like your problem lies here

public double getGas()
{
double mpg;
double distance;
distance = drive;
mpg = gas * milesPerGallon / distance;
return gas;
}

you are doing a calculation to mpg however you are returning the gas property at the end.

It doesn't actually seem like you are changing the value of gas anywhere except for when you first add it.

Upvotes: -1

Related Questions