Page 7 - KMK PREPSPM2 1819 ANSWER
P. 7

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



                       (b)    A method must be defined before it can be called. There are two ways to call a
                              method based on it’s return value. If a method returns a value, then the method
                              should either be assigned to a variable of the same data type but if a method
                              returns nothing (no return value), then the method should be declared as void.
                              A method must be called based on exactly how the method was defined.
                               (i)   Write a method named getAreaTriangle that will return double
                                     value and accepts two double parameters which are base and
                                     height. Next, write the formula to calculate the area of triangle in the
                                     getAreaTriangle method.
                                                                                                [3 marks]
                                      double getAreaTriangle(double base, double height){
                                             return 0.5*base*height;
                                      }

                              (ii)   Write a method named printLove that will display message “I
                                     Love Java”.
                                                                                                [2 marks]
                                      void printLove(){
                                             System.out.print("I Love Java.");
                                      }


                              (iii)   Write a method named Personal that will accept values “Ali”, 97.5
                                     and ‘A’ and store them in parameters name, mark and grade
                                     respectively. Then display all the parameters’ values.
                                                                                                [2 marks]
                                      void Personal(String name, double mark, char grade){
                                             System.out.print(name);
                                             System.out.print(mark);
                                             System.out.print(grade);
                                      }

                       (c)    Suppose that a method is defined as follows:
                                     double calcBMI(double h, double w){
                                            return w/Math.pow(h,2);
                                     }

                               (i)   Given weight (w) is 57.8 kg, and height (h) is 1.70 m. State the output
                                     of this method.
                                                                                                 [1 mark]
                                      20

                              (ii)   Write the method calls for the above method.
                                     (Assume that object myObj has been created)
                                                                                                 [1 mark]

                                      myObj.calcBMI(57.8,1.70);




                                                            7                                SULIT
   2   3   4   5   6   7   8   9   10   11   12