|
4. Write a method barrelsToTeaspoons() with a double
parameter b representing a number of barrels of petroleum. The method
returns the equivalent number of teaspoons of oil. For you information:
1 barrel petroleum = 158.98729 liters
1 teaspoon = 0.004928922 liters
// barrelsToTeaspoons(): convert barrels of oil to
teaspoons
private static double barrelsToTeaspoons(int barrels) {
//define and initialize the method
variables and constants
final double BARRELS_TO_LITERS = 158.98729;
final double LITERS_PER_TEASPOON = 0.004928922;
double ans = 0;
ans = ((barrels *
BARRELS_TO_LITERS) / LITERS_PER_TEASPOON);
return ans;
} //End of method barrelsToTeaspoons()
5. Write a double method evaluateQuadraticPolynomial()
that expects four double parameters a, b, c, and x. The method returns the
value: ax2 + bx + c.
// evaluateQuadraticPolynomial(): evaluate
the quadratic equation: ax2 + bx + c
private static double evaluateQuadraticPolynomial(double a, double b,
double c, double x) {
//define and initialize
the method variable
double ans = 0;
//Calcaulate the
quadratic equation
ans = (a*(Math.pow(x,2))
+ (b*x) + c);
return ans;
} //End of method
evaluateQuadraticPolynomial()
***********************************
Alternate method for #5
***********************************
private static double
evaluateQuadraticPolynomial(double a, double b, double c, double x) {
//Calcaulate the
quadratic equation
return (a*(Math.pow(x,2))
+ (b*x) + c);
} //End of method
evaluateQuadraticPolynomial()
**Both methods could have calculated their equations on the
return line for simplified and shorter code.
Send me email
Visit my website
|