Question

I need to complete the method that gets the data (distance and time) from the user and returns

Was it helpful?

Solution

You are invoking this method without an assignment.

//Call the velocity Calculator method
velocityCalculator(distance, time);
double velocity=0;

After that, velocity is 0 and you print 0.

You have to return a value in your method and assign it to your variable like this:

public static double velocityCalculator(double distance, double time) {
    return distance/time;
}

and in your main do the following:

//Call the velocity Calculator method
double velocity = velocityCalculator(distance, time);

Now your method velocityCalculator will return the calculated value and assign it to your newly created variable velocity.

Another point is that you want to calculate with floating point numbers, but you are reading just integers. You can read a double value with double time = Double.parseDouble(br.readLine()); instead of Integer.parseInt.

OTHER TIPS

you should either return the velocity value from the velocityCalculator function or declare the velocity variable as global, and reassign its value.

preferably, returning its value would be best approach, so modify your call to function to

//Call the velocity Calculator method
double velocity= velocityCalculator(distance, time);

and in your calculate function return its value

public static double velocityCalculator(double distance, double time)//this subroutine will calculate the velocity and print it
    {
        double velocity = distance/time;
        //calculates the velocity
        return velocity;

    }//closes velocityCalculator method
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top