2 Replies - 2497 Views - Last Post: 23 February 2010 - 06:30 AM Rate Topic: -----

#1 Guest_Derek*


Reputation:

Car Program Stuck (JAVA)

Posted 23 February 2010 - 12:43 AM

Hello all, I am new to this site, and I am stuck on the last part of this program. The ability to "drive".

/**
 * Implement a class Car that provides a programmatic model for a car object 
with the following functionality:
* Ability to construct a Car object by specifying its gas tank capacity, -----
its gas mileage (the number of miles it will travel on one gallon of gas), and----
the number of gallons of gas (currently) in its tank.----
* Ability to find out the number of gallons in the tank of a Car object.----
* Ability to find out the capacity of a Car object's tank.----
* Ability to find out the mileage of a Car object.---
* Ability to print on the screen a Car object's tank capacity, mileage and----
current number of gallons of gas in tank.---
* Ability to find out how many miles the car can travel with its current gas.
* Ability to "fill" gas into a Car object. We should be able to specify the
number of gallons that we are filling.  ---
If the Car object's tank cannot accpet this number of gallons,
then the Car object should remain unchanged and 
an appropriate error message should appear on the screen. ---
* Ability to "drive" a Car object for a specified number of miles. 
If the object is not able to drive the given number of miles 
(because it would run out of gas, for example), 
then the Car object should remain unchanged and 
an appropriate error message should appear on the screen.

All private attributes and methods of the class Car must be documented
 * 
 * @author (Derek Stockl) 
 * @version (v 1.0 Feb 19, 2010)
 */
public class Car
{
    // The Gas Tank Capacity
    private int tankCapacity;
    // The Gas Milage(number of miles it will travel on one gallon of gas)
    private int gasMilage;
    // Number of gallons (currently)
    private int currentGas;
  
    
    
  
    
    // Find out the numnber of gallons in the tank of Car.
    
    public int getCurrentGas()
    {
        return currentGas;    
    }
    // Find out the gas milage of Car.
    
    public int getMPG()
    {
        return gasMilage;
    }
    
    // Find out the tank capacity of Car.
    
    public int getTankCapacity()
    {
        return tankCapacity;
    }
    
   // Set the Gas mileage for Car.
      public void setMPG(int gasMilage)
    {
        this.gasMilage = gasMilage;
    }
    
    // Set the Current Gas left.
    public void setGasLeft(int currentGas)
    {
        this.currentGas = currentGas;
    }
    
    // Set the tank capacity.
    public void setTankCapacity(int tankCapacity)
    {
        this.tankCapacity = tankCapacity;
    }
    
    
    // Print details of Tank Capacity, MPG, miles til empt and Current Gas.
     public void printDetails()
    {
        System.out.println("Tank Capacity:          \t" + tankCapacity);
        System.out.println("Miles per Gallon:       \t" + gasMilage);
        System.out.println("Current Amount of Gas:  \t" + currentGas);
        System.out.println("Miles Left Until Empty  \t" + milesLeft() );
    }
    // Find how many miles left until empty.
   
    public double milesLeft()
    {
        return currentGas * gasMilage;
    
    }
    
    //Fill gas tank
    
    public void fillTank(int moreGas)
    { if (currentGas + moreGas < tankCapacity)
        currentGas = currentGas + moreGas;
        
      else 
      {
           System.out.println("Cannot exceed Tank Capacity");
      }
    }
    // ***THIS IS WHERE I AM STUCK AT***
    public void drive(double distance)
    {
        if (currentGas * gasMilage > distance)
       
        milesLeft = (currentGas * gasMilage) - distance;
        
        else
        {
            System.out.println("Not Enough Gas");
        }
    }
    
}







Any assistance would be greatly appreciated. now i know milesLeft = (currentGas * gasMilage) - distance; but i am having a mental block and cannot figure out what to put in that if statement.

Is This A Good Question/Topic? 0

Replies To: Car Program Stuck (JAVA)

#2 Guest_Derek*


Reputation:

Re: Car Program Stuck (JAVA)

Posted 23 February 2010 - 01:13 AM

// ***THIS IS WHERE I AM STUCK AT***
    public void drive(double distance)
    {
        if (currentGas * gasMilage > distance)
       
        milesLeft = (currentGas * gasMilage) - distance;



EDIT: I know all of that is wrong but that is essentially what i want it to say. i already used mileLeft here:

 public double milesLeft()
    {
        return currentGas * gasMilage;
    
    }


and im not sure how to call "mileLeft" again.

Can I just do this:


// The Gas Tank Capacity
    private int tankCapacity;
    // The Gas Milage(number of miles it will travel on one gallon of gas)
    private int gasMilage;
    // Number of gallons (currently)
    private int currentGas;
    // Test for milesLeft
    private double milesLeft = currentGas * gasMilage



Also I am aware I am misspelling gasMileage wrong.
Was This Post Helpful? 0

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Car Program Stuck (JAVA)

Posted 23 February 2010 - 06:30 AM

first, have a look at that code:
private double milesLeft = currentGas * gasMilage

you are missing a semicolon at the end. but that is not the point.
it is not advised to do that when declaring a global variable. you can do this calculation in the constructor, or any other method.

as for your problem, you have a method to calculate milesLeft:
public double milesLeft() 
    { 
        return currentGas * gasMilage; 
     
    }

so why are you calculating it agaig in that method:
public void drive(double distance) 
    { 
        if (currentGas * gasMilage > distance) 
        //here>>>
        milesLeft = (currentGas * gasMilage) - distance;


if you just want to use the milesLeft method, you could do it as:
public void drive(double distance) 
    { 
        if (currentGas * gasMilage > distance) 
        //using the returned value of milesLeft:
        milesLeft = milesLeft() - distance; 
         
        else 
        { 
            System.out.println("Not Enough Gas"); 
        } 
    } 



pay attention that in your Car class, the variable milesLeft is not declared.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1