Thinking in Java Fourth Edition Bruce Eckel Chapter 2 Exercise 4

Exercise 4: Write a program that calculates velocity using a constant distance and a constant time.

Solution:

class VelocityCalculator
{
    static float velocity (float d, float t)
    {
        if(t == 0) return 0f;
        else return d/t;
    }
}

public class VelocityTester
{
    public static void main(String[] args)
    {
        float d = 565.3f;
        float t = 3.6f;
        System.out.println("Distance: " + d);
        System.out.println("Time: " + t);
        float v = VelocityCalculator.velocity(d, t);
        System.out.println("Velocity: " + v);      
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *