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 2 of 2

Thread: Whats wrong with my code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my code?

    Suppose that you are given the following Driver class, which includes a 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 + ".");
     }
    }

    Now suppose that executing main produces the following output:

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


    Implement the AutoTrip class so that it produces the indicated output.


    public class AutoTrip {
     
    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);}





    when i put this in the feedback says "The constructor you have written doesn't match the constructor call in the problem specification."
    Last edited by whattheeff; March 4th, 2011 at 05:31 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Whats wrong with my code?

    Found problem:




    AutoTrip(double variable, int variable2)

    You called it with

    AutoTrip(double variable, double variable2);

    Change gallons in constructor to type double. Also, change return type for getFuel() to double now.

    Also change variable fuel in class AutoTrip to type double as well.


    Now that error should go away.
    Last edited by javapenguin; March 4th, 2011 at 05:37 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    whattheeff (March 4th, 2011)

Similar Threads

  1. Collision detection - whats wrong with my code?
    By Javaz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2011, 08:20 PM
  2. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  3. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM
  4. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM
  5. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM