haixi0118 2021-02-11 01:58 采纳率: 80%
浏览 132
已采纳

想求这道题的全部编码

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!"); 
    }
  • 写回答

1条回答 默认 最新

  • ProfSnail 2021-02-11 02:32
    关注
     public class CostEstimator {
     /**   
        * 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
            return distance / miles_per_liter;
        }
     
       /**   
        * 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
            return (int)Math.ceil(fuelNeeded / liter_per_gallon);
        }
     
       /**   
        * 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
            return gallonsFuel * cost_per_gallon;
        }
    
        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!"); 
        }
    
        public static void main(String[] args){
        //    testMethods();//测试。
        }
     }
    

     

    注意在int函数时候采用Math.ceil()进行取整,在前面用(int)强制类型转换。testMethod()是用于测试是否编程正确的,我的本地环境输出结果是。

     

     

    calcFuelNeeded test passed!
    calcGallonsFuelNeeded test passed!
    calcCostNeeded test passed!

     

    测试通过,代码正确。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)