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
Exercise instructions
- Make the
Formula
class astatic
class. - Create a
static
calculateSpeed
method that returns adouble
and takes twodouble
parameters,distance
andtime
. - The
calculateSpeed
method should return the division operation result ofdistance
divided bytime
. - Inside the
main
method, call thecalculateSpeed
method of theFormula
class, without creating an instance and print the operation result with thedistance
parameter set to165.00
and thetime
parameter set to15.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.___(___, ___));
}
}