i am trying to make a program that takes an object oriented approach to calculating the miles per gallon that your car gets. this is early in the process an i am aware that alot of stuff is missing. i am just trying to get it to work enough that i can figure it out. i can only go by examples of oop design because my resources are limited, so if something doesnt make sense, sorry. anyway, here is my code:

/**
 * The purpose of this program is to demonstrate the use of a constructor
 * that takes parameters.  Notice that there is no problem with two constructors
 * with the same name as long as their parameter lists are different.  This is
 * referred to as overloading a constructor,
 * 
 * ©FLVS 2007
 * @author B. Jordan 
 * @version 05/28/07
 */

public class CarV5
{

    

    CarV5()
    {
    }
    
    public int calcDistance(int distance, int endMiles, int startMiles)
    {
        distance = endMiles - startMiles;
        return distance;
    }
    
    public double calcMPG(int mpg, int distance, int gallons)
    {
        mpg = distance/ gallons;
        return mpg;
        
    }

    //main method
    public static void main(String[] args)
    {
        int startMiles1 = 55;
        int endMiles1 = 250;
        int gallons1 = 15;
        
        int distance1, mpg1;

        distance1 = calcDistance(distance1,endMiles1,startMiles1);
        mpg1 = calcMPG(mpg1 ,distance1);
        
        System.out.println("                     Gas Mileage Calculations");
        System.out.println("Type of Car   Start Miles   End Miles   Distance   Gallons   Miles/Gallons");
        System.out.println("==========================================================================");
        System.out.printf("Saturn %4.2f    %4.2f    %4.2f    %4.2f    %4.2f    %4.2f" + startMiles1 + endMiles1 + distance1 +  gallons1 + mpg1);
    }
}

Do you have any specific questions?

Do you have any specific questions?

...i would assume so. yes, as you can probably tell, i cant figure out how to correctly call my methods. and if possible someone could not only help me get it to work so i can figure it out on my own, but also to help me understand exactly how this works as i havent been able to find any good insructional information on this particular topic.

i am trying to make a program that takes an object oriented approach to calculating the miles per gallon that your car gets. this is early in the process an i am aware that alot of stuff is missing. i am just trying to get it to work enough that i can figure it out. i can only go by examples of oop design because my resources are limited, so if something doesnt make sense, sorry. anyway, here is my code:

/**
 * The purpose of this program is to demonstrate the use of a constructor
 * that takes parameters.  Notice that there is no problem with two constructors
 * with the same name as long as their parameter lists are different.  This is
 * referred to as overloading a constructor,
 * 
 * ©FLVS 2007
 * @author B. Jordan 
 * @version 05/28/07
 */

public class CarV5
{

    

    CarV5()
    {
    }
    
    public int calcDistance(int distance, int endMiles, int startMiles)
    {
        distance = endMiles - startMiles;
        return distance;
    }
    
    public double calcMPG(int mpg, int distance, int gallons)
    {
        mpg = distance/ gallons;
        return mpg;
        
    }

    //main method
    public static void main(String[] args)
    {
        int startMiles1 = 55;
        int endMiles1 = 250;
        int gallons1 = 15;
        
        int distance1, mpg1;

        distance1 = calcDistance(distance1,endMiles1,startMiles1);
        mpg1 = calcMPG(mpg1 ,distance1);
        
        System.out.println("                     Gas Mileage Calculations");
        System.out.println("Type of Car   Start Miles   End Miles   Distance   Gallons   Miles/Gallons");
        System.out.println("==========================================================================");
        System.out.printf("Saturn %4.2f    %4.2f    %4.2f    %4.2f    %4.2f    %4.2f" + startMiles1 + endMiles1 + distance1 +  gallons1 + mpg1);
    }
}

Java is purely object oriented meaning that member functions such as, calcDistance(...) and calcMPG(...) must be called on an object.

In your main you must construct an object of type CarV5 with the following syntax

CarV5 car = new CarV5();

car is just an object name so it can be whatever you want. Similiar to when you say int myInt = 8. myInt can be changed to anything. The last set of parentheses are empty because in your constructor you haven't specified any paremeters.

Now that you have a car object you can call your functions on that object.

distance1 = car.calcDistance(distance1,endMiles1,startMiles1);
mpg1 = car.calcMPG(mpg1 ,distance1);

You are now saying go into the car object and perform the calcDistance function and put the result to the integer distance1 and something similiar for calcMPG.

Hope this helps.

but also to help me understand exactly how this works as i havent been able to find any good insructional information on this particular topic.

public int calcDistance(int distance, int endMiles, int startMiles)
    {
        distance = endMiles - startMiles;
        return distance;
    }

the above will calculate the distance obviously. It has three parameters: distance, end miles, and start miles. Inside you have it calculate the distance automatically.

public double calcMPG(int mpg, int distance, int gallons)
    {
        mpg = distance/ gallons;
        return mpg;
        
    }

this could be explained almost exactly like the one above, but with different paramters

//main method
    public static void main(String[] args)
    {
        int startMiles1 = 55;
        int endMiles1 = 250;
        int gallons1 = 15;
        
        int distance1, mpg1;

        distance1 = calcDistance(distance1,endMiles1,startMiles1);
        mpg1 = calcMPG(mpg1 ,distance1);
        
        System.out.println("                     Gas Mileage Calculations");
        System.out.println("Type of Car   Start Miles   End Miles   Distance   Gallons   Miles/Gallons");
        System.out.println("==========================================================================");
        System.out.printf("Saturn %4.2f    %4.2f    %4.2f    %4.2f    %4.2f    %4.2f" + startMiles1 + endMiles1 + distance1 +  gallons1 + mpg1);
    }
}

This main method sets the start miles, end miles, and gallons with specific integers. Distance and mpg have no specific integers but will receive them once they are plugged in.

distance1 = calcDistance(distance1, endMiles1, startMile1);

This is basically saying this: calcDistance(distance1, 250, 55);
Now remember calcDistance figures out the distance1 for you due to the calculation inside the method. So distance1 will now equal 195.

mpg1 = calcMPG(mpg1, distance1);

This will be: calcMPG(mpg1, 195);
So plug that into the equation: mpg = 195/15 which is 13.


I'm not sure if that's what you were looking for, but hopefully it was. If you need it broken down more, state so.


*Edit*
Just noticed chunalt787 replied

alright. quick question. this is pretty off topic to my last post. i have gotten most of that figured out and am now trying to advance my code to true oop. i have a question about my constructor.

public class CarV5
{
    String carType;
    int endMiles;
    int startMiles;
    double gallonsUsed;
    double pricePerGallon;
    
    CarV5()
    {
           
    }
    
    CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);
    {
        cT1 = carType1;
        eM1 = endMiles1;
        sM1 = startMiles11;
        g1 = gallonsUsed1;
        ppg1 = pricePerGallon1;
    }

it says that i need ")" what does that mean?

CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);
{
cT1 = carType1;
eM1 = endMiles1;
sM1 = startMiles11;
g1 = gallonsUsed1;
ppg1 = pricePerGallon1;
}

It's because the above code does not have valid syntax. Are you trying to create a method? If so, it has to be in a form such as:

public void doSomething(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1)
{
cT1 = carType1;
eM1 = endMiles1;
sM1 = startMiles11;
g1 = gallonsUsed1;
ppg1 = pricePerGallon1;
}

If you're trying to put method calls and use variables, etc, it has to be inside of a method.

CarV5 car1 = new CarV5(String carType1, double endMiles1, double startMiles1, double gallonsUsed1, double pricePerGallon1);

^ The above is creating a new Object, but it is not inside of a method, it is being done inside of the class. You have to put it inside of a method, if you want it to be done when you 'run the class', then you'd put it inside of that class's main method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.