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

  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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Flaw in my code

    What is wrong with my output?
    Did you post it yet?

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

    Default Re: Flaw in my code

    Oh sorry, I forgot to put it in brackets code.
    Last edited by miss confused; June 30th, 2010 at 12:01 AM.

  4. #4
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Flaw in my code

    AutoTrip myTrip = new AutoTrip(distance, fuel);

    Youre not supposed to train on your get-methods here. The assignment isnt to write a getMPG method that just returns a value. See how this piece of code doesnt have 3 inputs but 2? You get the distance and the fuel, the MPG has to be calculated inside the myTrip-object.

    public AutoTrip(double distance, double fuel, double MPG){
      distance = miles;
      fuel = gallons;
      MPG = mileage;
    }

    Heres the error, youre using 3 input-variables here. Remove the double MPG and calculate it either in the constructor for AutoTrip, or in the getMPG method.

  5. The Following User Says Thank You to Charlie For This Useful Post:

    Json (June 30th, 2010)

  6. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Flaw in my code

    OK first of all see what happens when you change
    distance = miles;
    for
    miles = distance;

    Then as Charlie has said you'll want to calculate the MPG in the constructor and look here whats wrong with this
        AutoTrip myTrip = new AutoTrip(distance, fuel);
    When your constructor is like this
    public AutoTrip(double distance, double fuel, double MPG)

    And I think Norm meant to post the output you actually got as in what came up on the screen although we did need that class too.

  7. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Flaw in my code

    From what i believe you are wanting to achieve, i would suggest you put calculations for the MPG in the getMPG() method, and then remove 'double MPG' as a parameter in the constructor.
    Programmer - an organism that turns coffee into software.

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

    Default Re: Flaw in my code

    What's wrong with this?

    private double distance;
    private int fuel;
     
    public AutoTrip(double miles, int gallons){
      distance = miles;
      fuel = gallons;
    }
     
    public double getDistance(){return distance;}
     
    public int getFuel(){return fuel;}
     
    public double getMPG(){return (distance/fuel);}

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Flaw in my code

    What do you mean by "wrong"? What is the code supposed to do?
    Does it compile? Does it execute? If yes to both, then its not wrong on those counts.

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

    Default Re: Flaw in my code

    Quote Originally Posted by Norm View Post
    What do you mean by "wrong"? What is the code supposed to do?
    Does it compile? Does it execute? If yes to both, then its not wrong on those counts.

    Feedback:
    The constructor you have written doesn't match the constructor call in the problem specification.

    Output was not:
    My car traveled 400.48 miles on 21.4 gallons of gasoline.
    My mileage was 18.714018691588787.

  11. #10
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Flaw in my code

    Well you tell us what is actually happening now?

  12. #11
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Flaw in my code

    So what was the output? You have gallons as an int but you are passing it a double.

  13. The Following User Says Thank You to Faz For This Useful Post:

    miss confused (June 30th, 2010)

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Flaw in my code

    I still don't see your question. Try to be more specific than saying: "what's wrong".
    Output was not:
    Sorry I don't see where you posted your output.

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

    Default Re: Flaw in my code

    Oh. I got it. Thanks!

  16. #14
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Flaw in my code

    I just spotted that by chance from now on don't tell us what the output wasn't tell us what it was. We can't tell much just from what you expect the programme to do can we?

  17. #15
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Flaw in my code

    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 " + distance + " miles ");
        System.out.println("on " + fuel + " gallons of gasoline.");
        double mileage = myTrip.getMPG(); // get miles per gallon
        System.out.println("My mileage was " + mileage + ".");
     }
     
    }
     
     
     
     
    public class AutoTrip 
    {
    private double distance;
    private int fuel;
     
    public AutoTrip(double miles, double gallonsl) {
      distance = miles;
      fuel = gallons;
    }
     
    public double getMPG(){ return (distance/fuel); }
    }