Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Flaw in my code

Threaded View

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Flaw in my code

    Main Method:
    public class Driver
    {
      public static void main(String[] args)
      {
        double distance = 400.48;
        double fuel = 21.4;
        AutoTrip myTrip = new AutoTrip(distance, fuel);
        System.out.print("My car traveled " + myTrip.getDistance() + " miles ");
        System.out.println("on " + myTrip.getFuel() + " gallons of gasoline.");
        double mileage = myTrip.getMPG(); // get miles per gallon
        System.out.println("My mileage was " + mileage + ".");
     }
    }



    Suppose to Produce:
    My car traveled 400.48 miles on 21.4 gallons of gasoline.
    My mileage was 18.714018691588787.



    What is wrong with my output?
    private double miles;
    private double gallons;
    private double mileage;
     
    public AutoTrip(double distance, double fuel, double MPG){
      distance = miles;
      fuel = gallons;
      MPG = mileage;
    }
     
    public double getDistance()
    {return miles;}
     
    public double getFuel()
    {return gallons;}
     
    public double getMPG()
    {return mileage;}
    Last edited by miss confused; June 30th, 2010 at 01:19 AM.