Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2.20 Ch 2 Program, part 3: Refactoring Calculating Fuel Cost (Java) This lab section is focusing on implementing and calling methods. This is a critical

2.20 Ch 2 Program, part 3: Refactoring Calculating Fuel Cost (Java)

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.

For part 3:

(1) Copy your code from part 2 (Calculate Fuel Cost) and refactor it to use the following four methods. You will have to change the class name to CostEstimator to match the filename CostEstimator.java

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 } 

Copy these methods into your CostEstimator class and fill in the bodies. Move the calculations you performed in the main method to the method bodies of these new methods we are defining, and change the calculations to use the parameters. Then, call these new methods from the main method to perform the calculations. When complete, your program should pass the same tests just as before.

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!"); } 

This is the Part 2 code I did:

import java.util.Scanner; import java.lang.Math;

public class CostEstimator { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); double distance = 0.0; double fuelNeeded = 0.0; int gallonsFuel = 0; double costNeeded = 0.0;

final double MILES_PER_LITER = 13.7; final double LITER_PER_GALLON = 3.785; final double COST_PER_GALLON = 2.629; System.out.println("Enter the distance to be covered (miles):"); distance = scnr.nextDouble();

fuelNeeded = distance / MILES_PER_LITER; System.out.println("Fuel Needed: "+ fuelNeeded + " liter(s)");

gallonsFuel = (int) Math.ceil(fuelNeeded / LITER_PER_GALLON); System.out.println("Gallons needed: " + gallonsFuel + " gallon(s)");

costNeeded = COST_PER_GALLON * gallonsFuel; System.out.println("Cost needed: " + costNeeded + " $");

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

collect, organise and store quantitative data in an effective way;

Answered: 1 week ago