Get startedGet started for free

Creating a static formula class

In our scenario, we have a Formula class with methods for mathematical formulas that can be useful to many developers using our code. You will add a formula to calculate the speed that will be available for use, thereby ensuring developers using your code do not need to write code for the same speed calculation, saving precious time.

This exercise is part of the course

Introduction to Object-Oriented Programming in Java

View Course

Exercise instructions

  • Make the Formula class a static class.
  • Create a static calculateSpeed method that returns a double and takes two double parameters, distance and time.
  • The calculateSpeed method should return the division operation result of distance divided by time.
  • Inside the main method, call the calculateSpeed method of the Formula class, without creating an instance and print the operation result with the distance parameter set to 165.00 and the time parameter set to 15.00.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class Main {
	// Mark Formula static
    ___ class Formula{
    
        // Create the calculateSpeed static method
        ___ ___ ___(___ ___, ___ ___) {
          ___ ____ / ___;
        }            
    
    }

    public static void main(String[] args) {
        // Print the result of the calculateSpeed method with the correct parameters
        System.out.println(Formula.___(___, ___));
    }
}
Edit and Run Code