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.
Este exercício faz parte do curso
Introduction to Object-Oriented Programming in Java
Instruções do exercício
- Make the
Formulaclass astaticclass. - Create a
staticcalculateSpeedmethod that returns adoubleand takes twodoubleparameters,distanceandtime. - The
calculateSpeedmethod should return the division operation result ofdistancedivided bytime. - Inside the
mainmethod, call thecalculateSpeedmethod of theFormulaclass, without creating an instance, and print the operation result with thedistanceparameter set to165.00and thetimeparameter set to15.00.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
public class Main {
// Mark Formula as static
____ class Formula{
// Create the calculateSpeed static method
____ ____ calculateSpeed(double ____, double ____) {
return ____ / ____;
}
}
public static void main(String[] args) {
// Print the result of the calculateSpeed method with the correct parameters
System.out.println(Formula.____(____, ____));
}
}