Search code examples
javawhile-loopsubroutine

How can I make the variable from a subclass be used in main class


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


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.