Java Method Stubs

后端 未结 3 781
我寻月下人不归
我寻月下人不归 2021-01-29 16:47

This is what I have to do:

Define stubs for the methods called by the below main(). Each stub should print \"FIXME: Finish methodName()\" followed by a newline, and shou

相关标签:
3条回答
  • 2021-01-29 17:06

    import java.util.Scanner;

    public class MthdStubsStatistics {

    /* Your solution goes here */
    public static int getUserNum () { System.out.println("FIXME: Finish getUserNum()");
    return -1;
    }
    public static int computeAvg(int userNum1, int userNum2){ int avgResult = (userNum1 + userNum2)/2; System.out.println("FIXME: Finish computeAvg()"); return -1;
    } public static void main() { int userNum1 = 0; int userNum2 = 0; int avgResult = 0;

    userNum1 = getUserNum(); userNum2 = getUserNum();

    avgResult = computeAvg(userNum1, userNum2);

    System.out.println("Avg: " + avgResult);

    return;
    } }

    0 讨论(0)
  • 2021-01-29 17:10

    A method stub in this sense is a method with no real substance, i.e. it's not doing what it is intended to do. Your getUserNum() method should return a user's unique ID, but instead you're defining a stub that simply returns -1 on every invocation.

    You can tell from your main() method, you're supposed to be defining these two methods:

    userNum1 = getUserNum();
    avgResult = computeAvg(userNum1, userNum2);
    

    So, define them. Here's what the getUserNum() stub would look like.

    public static int getUserNum() {
        System.out.println("FIXME: Finish getUserNum()");
        return -1;
    }
    

    I'll leave computeAvg() up to the OP.

    0 讨论(0)
  • 2021-01-29 17:20

    import java.util.Scanner;
    
    public class MthdStubsStatistics {
       
       public static int getUserNum() {
          System.out.println("FIXME: Finish getUserNum()");
          return -1;
       }
    
       
       public static int computeAvg(int a, int b) {
          System.out.println("FIXME: Finish computeAvg()");
          return -1;
       }
    
       public static void main(String [] args) {
          int userNum1;
          int userNum2;
          int avgResult;
    
          userNum1 = getUserNum();
          userNum2 = getUserNum();
    
          avgResult = computeAvg(userNum1, userNum2);
    
          System.out.println("Avg: " + avgResult);
       }
    }

    0 讨论(0)
提交回复
热议问题