Page 14 - KMK PREPSPM2 1819 ANSWER
P. 14

SULIT     No. Matrik…………………………………….… No. Kad Pengenalan…………………………..  SC015



               3       A non-static user-defined method is defined by specifying its return type, method
                       name, and parameters.
                       (a)    Construct non-static user-defined methods to perform the following tasks.

                                        “To display message “PASS” if mark greater than 40, otherwise
                                                               display “FAIL””
                                                                                                [2 marks]
                               void PassFail(double mark){
                                      if(mark>40)
                                             System.out.println(“PASS”);
                                      else
                                             System.out.println(“FAIL”);
                               }

                       (b)    Construct a method findMax to return the maximum of 3 numbers.
                                                                                                [2 marks]
                                double findMax(double a, double b, double c){
                                       if(a>b&&a>c)
                                              return a;
                                       else if(b>a&&b>c)
                                              return b;
                                       else if(c>a&&c>b)
                                              return c;
                                }

                       (c)    Construct a method getAverage to receive five numbers and return its
                              average.
                                                                                                [2 marks]
                                double getAverage(int a,int b,int c,int d,int e){
                                       return (a+b+c+d+e)/5;
                                }
































                                                           14                                SULIT
   9   10   11   12   13   14   15   16