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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题