Question:
I am having Java issues?
tkd10_2004
2013-02-02 16:35:36 UTC
ok .... this is homework and im stuck and have an issue so plase dont be an ***.
// this is to show that tank is my ref var for the GasTank class


public class Car{
//the manufacturer of the car
private String make;
//the model of the car
private String model;
//the miles per gallon
private double mpg;
private double totalMilesDriven;
//the GasTank
private GasTank tank;
private double tripDistance;

/*
* constructor for a default Car object
*/
public Car(){
//default GasTank
tank = new GasTank(15);
make = "Ford";
model = "Fusion";
totalMilesDriven = 0;
mpg = 20;
tripDistance = 500;
this.beginTrip();

}


//this my drive method from the car class

public void drive(){
Random generator = new Random();
//50 to 100 miles
double milesDriven = (generator.nextDouble()*50) + 50;

if(tank.isEmpty()!= true)
{
tank.burnGas();
}
else
{
System.out.println("You gas tank is empty please add more gas to continue");
}



//print details
this.printTripDetails();

}

this is the isEmpty method from the GasTank class


/**
* Check for an empty tank
* @return true if the tank is empty
*/

public boolean isEmpty(double gallonsLeft){
if (gallonsLeft != 0 )
{
return false;
}
else
{
return true;
}

}

ok what i want to do is the isEmpty() to check for something and then return
true or false. then i want the drive() in the Car class to take the return from the isEmpty() and check it in the if statment of the drive(). This is my issue, it keeps telling me that in my drive() the method isEmpty in GasTank cannot be applied to given types; required: double; found: no arguments; reason: actual and formal argument lists differ in length.

i am first year student in programming and dont understand why i am having this erorr. again what i want to do is if the tank.isEmpty is not = to true then run tank.burnGas() else do the system.out.().

please help or point me in the right direction to figure this out thanks in advance.
Three answers:
Jeff
2013-02-02 16:42:19 UTC
when using isEmpty() always use it like



tank not empty

!tank.isEmpty()



tank empty

tank.isEmpty()



now a problem...

gallonsLeft != 0

gallonsLeft is a double... when doing arimetic with doubles you lose precision ....so you can't really ever test == or !=



what if it's -0 ? -0 is not 0 as far as a double is concerned. You might try gallonsLeft <= 0



try those changes as a start.



As the other poster mentioned isEmpty() takes a parameter... you should get rid of the parameter because I assume gallonsLeft is a private variable in GasTank and it can check itself.
peteams
2013-02-03 00:44:51 UTC
public boolean isEmpty(double gallonsLeft){



This defines a member function isEmpty that takes a parameter gallonsLeft, so it would be called thus: instance.isEmpty(42.0)



if(tank.isEmpty()!= true)



This is an attempt to call isEmpty on tank without passing the gallonsLeft parameter, hence the problem.



I would expect the solution to the problem to be deleting the parameter to isEmpty and instead implementing isEmpty by looking at member variables of GasTank.
Georgio
2013-02-03 00:42:05 UTC
Your isEmpty method passes a parameter called double gallonsLeft. When you called the method isEmpty in your drive method, you did not pass a parameter to it. Therefore, Java saw that and said that it required a double but found no arguments.



//where you indicated that isEmpty() needed a parameter



public void isEmpty(double gallonsLeft){



}



//where the error was being thrown



if(tank.isEmpty()...){



}



This error is being thrown because there is no double parameter being passed in the line tank.isEmpty(). You should pass some sort of double. For example, if you want to pass the number of gallons left, then in or before your drive method you should have somewhere "double gallonsLeft = 6" (I'm just using 6 as an example)



then, in the tank.isEmpty() line, just say tank.isEmpty(gallonsLeft)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...