Help with range method!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrHuggykins
    New Member
    • Mar 2008
    • 27

    #1

    Help with range method!

    First Class------------------

    // Car class to define all the methods/classes in CarUser
    public class Car {
    // Creates object Car
    public Car()
    {

    }
    // Let's a car have initial gas in the tank
    public Car (double initialGas)
    {
    gas = initialGas;
    }
    // method to add gas to the car
    public void addGas(double addGas)
    {
    double newaddGas = gas + addGas;
    addGas = newaddGas;
    }
    public void drive(double drive)
    {
    double newdrive = (drive/car) - gas;
    drive = newdrive;
    }
    public double range(double range)
    { double newrange = gas * car;
    range = newrange;
    return range;
    }
    private double gas = 3.5;
    private double car = 0;



    }



    Second Class-
    public class CarUser
    {

    /**
    * Constructor for objects of class CarUser
    */
    public CarUser()
    {
    Car honda = new Car(30.0); // 30 miles per gallon

    honda.addGas(10 .0); // add 10 more gallons
    honda.drive(250 .0); // drive 250 miles

    // print range remaining
    System.out.prin tln("Honda range remaining: " + honda.range());

    Car toyota = new Car(20.0); // 20 miles per gallon

    toyota.addGas(1 8); //add 18 more gallons
    toyota.drive(20 0.0); //drive 200 miles

    // print range remaining
    System.out.prin tln("Toyota range reining:" + toyota.range()) ;

    }

    }
    ----------
    Last edited by Rabbit; Jun 12 '14, 03:18 PM. Reason: Removed OP's name
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) What is the question?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      You have to get your dimensions right: if 30 means 30 miles per gallon then 10
      can't mean 'add 10 more gallons'.

      kind regards,

      Jos

      Comment

      • MrHuggykins
        New Member
        • Mar 2008
        • 27

        #4
        Originally posted by JosAH
        You have to get your dimensions right: if 30 means 30 miles per gallon then 10
        can't mean 'add 10 more gallons'.

        kind regards,

        Jos
        Why not.. that means you're adding 300 miles to the tank.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          I'm still wating to hear what the question is here.

          In the meantime, here is a rather useless method.
          [CODE=java] public void addGas(double addGas)
          {
          double newaddGas = gas + addGas;
          addGas = newaddGas;
          }[/CODE]

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by MrHuggykins
            Why not.. that means you're adding 300 miles to the tank.
            Not the way you do it; as I wrote: check the dimensions of those numbers: gallons,
            miles or miles per gallon.

            kind regards,

            Jos

            Comment

            • MrHuggykins
              New Member
              • Mar 2008
              • 27

              #7
              I don't have a question... I need help I don't know java at all. Do they all need to be the same dimension?

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by MrHuggykins
                I don't have a question... I need help I don't know java at all. Do they all need to be the same dimension?
                If you need help, then tell us, what your problem is EXACTLY. We won't go through your code if we don't even know, what the problem is.
                You could start with explaining, what your program should do, what it does do and what you're trying to archive at the moment.

                Considering the dimension issue, just try writing it down. Something like
                Code:
                10 miles + 5 gallons
                doesn't make any sense in real life, does it? So why should it make sense for the computer? It's the same as dividing apples through pears, as one of my teacher used to say.

                Greetings,
                Nepomuk

                Comment

                Working...