Results 1 to 7 of 7
  1. #1
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Adding Miles Per Gallon to current code.

    I am trying to add code that calculates MPG in two different SubClasses but cannot get it to compile.

    This is the line I've been trying to add:
    Java Code:
    milesPer = 5000/getTowingCapacity()/getHorsepower();
    And this is my file I am trying to add it to:
    Java Code:
    public class Truck extends Vehicle{
        public double towingCapacity;
    
        public double getTowingCapacity() {
            return towingCapacity;
        }
    
        public void setTowingCapacity(double towingCapacity) {
            this.towingCapacity = towingCapacity;
        }
        @Override
         public  void Display()
        {
            
            System.out.println("Make: "+getMake());
            System.out.println("Year: "+getYear());
            System.out.println("HorsePower: "+getHorsepower());
            System.out.println("Towing Capacity: "+getTowingCapacity());
        }
        
        public static void main(String[] args) {
            
            Truck a = new Truck();
            Truck b = new Truck();
            Truck c = new Truck();
            
            
            a.setMake("Truck 1");
            a.setYear("1999");
            a.setHorsepower(200);
            a.setTowingCapacity(2.0);
            
            b.setMake("Truck 2");
            b.setYear("1998");
            b.setHorsepower(300);
            b.setTowingCapacity(1.0);
            
            c.setMake("Trcuk 3");
            c.setYear("2007");
            c.setHorsepower(500);
            c.setTowingCapacity(1.5);
            
            System.out.println("Truck 1:");
            a.Display();
    
            System.out.println("\nTruck 2:");
            b.Display();
            
            System.out.println("\nTruck 3:");
            c.Display();
        }
    }
    Does anyone know the best way to add this calculation as a method called calculateMPG? I know I have three separate horsePower variables and three TowingCapacities so do I need three different MPG methods?

    So one for car a, b, and c like:
    Java Code:
        public static String calculateMPG(double milesPer, double getHorsepower(), double 
    
    getTowingCapacity()) {
            milesPer = 5000/ a.setTowingCapacity(2.0)/a.setHorsepower(200);
        }
    }
    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding Miles Per Gallon to current code.

    That's not how you define a method -- you don't put method calls in the parameters of the method itself. You may use them when you *call* the method but not when you define it. Please have a look at the tutorial on how to create methods and how to pass information into methods.

  3. #3
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Adding Miles Per Gallon to current code.

    Ok thank you. I seem to struggle with methods. I changed it up but I'm getting an error: missing return statement on line 62

    Java Code:
    public class Truck extends Vehicle{
        public double towingCapacity;
    
        public double getTowingCapacity() {
            return towingCapacity;
        }
    
        public void setTowingCapacity(double towingCapacity) {
            this.towingCapacity = towingCapacity;
        }
        @Override
         public  void Display()
        {
            
            System.out.println("Make: "+getMake());
            System.out.println("Year: "+getYear());
            System.out.println("HorsePower: "+getHorsepower());
            System.out.println("Towing Capacity: "+getTowingCapacity());
        }
    
        
        public static void main(String[] args) {
            
            Truck a = new Truck();
            Truck b = new Truck();
            Truck c = new Truck();
            
            
            a.setMake("Truck 1");
            a.setYear("1999");
            a.setHorsepower(200);
            a.setTowingCapacity(2.0);
            double aHorse= 200;
            double aTow= 2.0;
            String aMPG = acalculateMPG(aHorse, aTow);
    
            b.setMake("Truck 2");
            b.setYear("1998");
            b.setHorsepower(300);
            b.setTowingCapacity(1.0);
            
            c.setMake("Trcuk 3");
            c.setYear("2007");
            c.setHorsepower(500);
            c.setTowingCapacity(1.5);
            
            System.out.println("Truck 1:");
            a.Display();
    
            System.out.println("\nTruck 2:");
            b.Display();
            
            System.out.println("\nTruck 3:");
            c.Display();
        }
    
    
        public static String acalculateMPG(double aHorse, double aTow) {
            double milesPer;
            milesPer = 5000/aTow/aHorse;
    	}
        
    
    }
    Last edited by Yokomoko; 07-25-2012 at 05:47 AM.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Adding Miles Per Gallon to current code.

    You specify a return value of type String in your acalculateMPG() method.

    Quote Originally Posted by Yokomoko View Post
    Java Code:
    public static String acalculateMPG(double aHorse, double aTow) {
            double milesPer;
            milesPer = 5000/aTow/aHorse;
    	}
    I recommend that you read through this: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Adding Miles Per Gallon to current code.

    I totally forgot that! For some reason it's saying that aTow and aHorse should be strings but I need them to be doubles because they are numbers.

    Java Code:
        public static String acalculateMPG(double aHorse, double aTow) {
            String milesPer;
            milesPer = 5000/aTow/aHorse;
    	return milesPer;
    	}

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Adding Miles Per Gallon to current code.

    Java Code:
    String aMPG = acalculateMPG(aHorse, aTow);
    Is the calculateMPG method really supposed to be a static method or should it belong to the Truck class? Since the horsepower and towing capacities are already stored within a Truck object you would not need to pass them as parameters if it where an instance method.

  7. #7
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Adding Miles Per Gallon to current code.

    It should belong to the truck method. My book is not very good at teaching this stuff :/

Similar Threads

  1. Replies: 12
    Last Post: 05-10-2012, 08:11 PM
  2. adding code to constructor
    By twillders in forum New To Java
    Replies: 5
    Last Post: 12-13-2010, 08:29 PM
  3. Replies: 1
    Last Post: 08-16-2010, 10:49 AM
  4. Replies: 14
    Last Post: 09-18-2008, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •