This lab section is focusing on implementing and calling methods. This is a critical concept of the course. If you are having trouble with this step, please seek help from Piazza and the TA/Instructors during office hours.
In programming, the term refactoring means to review and re-write your code to improve its quality. That is, code refactoring improves the quality of the code, but it doesn't change its functionality.
(1) Copy your code from week 2 zyLab: Calculate Fuel Cost, and refactor it to use the following four methods. Note that only the calculations are done in the individual methods (as can be seen by the return type of the methods). All print statements should be in the main method still.
DO NOT CHANGE THE METHOD HEADERS!
/**
* Calculates the amount of fuel needed.
*
* @param distance The distance of the trip.
* @param miles_per_liter Total distance that could be achieved with 1 liter of fuel.
* @return Total fuel needed.
*/
public static double calcFuelNeeded(double distance, double miles_per_liter) {
//FILL IN THE BODY
}
/**
* Calculates the total number of gallons needed.
*
* @param fuelNeeded Total fuel needed.
* @param liter_per_gallon The volume of each gallon.
* @return Total number of gallons needed.
*/
public static int calcGallonsFuelNeeded(double fuelNeeded, double liter_per_gallon) {
//FILL IN THE BODY
}
/**
* Calculates the total cost needed.
*
* @param gallonsFuel Total number of gallons needed.
* @param cost_per_gallon The cost of each gallon.
* @return The total cost of the trip.
*/
public static double calcCostNeeded(int gallonsFuel, double cost_per_gallon) {
//FILL IN THE BODY
}
Note: you should not make any changes to the method headers.
(2) The following code will perform some basic tests of your methods. Copy it into your CostEstimator class.
The method is complete as is and you do not need to change it or add to it. You can call it from your main method to check your methods before submitting. Remember to remove the test method call from your main method before submitting to zyBooks for grading.
The code below uses conditional statements which are described in Chapter 4. Essentially, it checks if the two values are equal, and, if so, it prints out the success message. Otherwise, it prints out the failure message. The tests for the methods that return doubles do not check for exact equality. This is due to the nature of how the values are stored and will be covered in Chapter 5.5.
In general, it is considered good programming practice to write tests for your methods before coding the methods. You might consider adding some more tests to the method below, but it is not required.
/**
* Performs a basic test on each of the methods above.
*/
public static void testMethods() {
if(Math.abs(calcFuelNeeded(100, 12.5) - 8) < 0.0000001)
System.out.println("calcFuelNeeded test passed!");
else
System.out.println("calcFuelNeeded test failed!");
if(calcGallonsFuelNeeded(10, 3) == 4)
System.out.println("calcGallonsFuelNeeded test passed!");
else
System.out.println("calcGallonsFuelNeeded test failed!");
if(Math.abs(calcCostNeeded(4, 7.8) - 31.2) < 0.0000001)
System.out.println("calcCostNeeded test passed!");
else
System.out.println("calcCostNeeded test failed!");
}