Thursday, July 25, 2024

Predefined method in Java

 

2. Predefined method:

If a method is already defined by programmer in java library

Examples:

  • Math.min()
  • Math.max()
  • Math.sqrt()

  min()

{

}

Math Class:

  • The Math class in Java is a predefined class.
  • It contains methods to perform mathematical operations.

Common Methods:

  • Math.min(): Returns the smaller of two values.
  • Math.max(): Returns the larger of two values.
  • Math.sqrt(): Returns the square root of a value.

Tips for Exploring Predefined Methods:

  • Viewing Method Implementations:
    • Tip 1: To view the code of a predefined method, press Ctrl and move the mouse over the method name (e.g., max()) and click "Open Declaration".
      • This will navigate to the method's body in the Java library, allowing you to see its implementation.
  • Searching for Methods:
    • Tip 2: To view all methods in a class, use Ctrl + O (the letter 'O') to open the outline view.
    • You can also search for specific method names, such as min, to quickly locate them within the class.                  

package PreDefinedMethods;

public class MathBasicsMethods {

               public static void main(String[] args) {

                             

                              // call max

                              int maxValue =  Math.max(2, 5);

                              //  max() ;; max no =  5

                              //         maxValue =  5    

                              System.out.println("result max="+maxValue);

                                             //                              5

                              // Min ()  - pass 2,5

                              int minVal = Math.min(2, 5);

                              //                    2

                              //   

                              //      minVal=                2

                              System.out.println("minVal="+minVal);

                              //                              2

                              // sqaure root of no -pass 4.0  --> 2.0

                              double  sqrtResult=  Math.sqrt(4.0);

                              //                         2.0

                             

                              ///     sqrtResult =   2.0

    

                 // 2.0 

                  System.out.println("sqrtResult="+sqrtResult);

                 

//              int no= Math.sqrt(4.0);// error :Type mismatch: cannot convert from double to int

                 //  sqrt() return double value   but  left we declared int var.. 

                 // so we should declare double variable left side

                              // Tip:   how to know method return type :  move mouse over on method name--  left side some data type

                

                 // HW call ,max () by passing float values

                

                 //HW call ,min () by passing float values

                

               }

}

              

HW WAP to Define a Method to Square a Given Number and Pass an Integer

   squareNo(2);    o/p ://4

 

  2 * 2 =  4

 3 * 3 = 9

return n*n;         // this  also fine

HW WAP to define method -square given no  and pass float no

 square (2.0) //  4.0

HW WAP to define method -cube of given no  and pass- int no 

 2 *2 *2 = 8

 3*3*3 = 27

HW WAP to define method - cube of given no  and pass- float no

    2.0 * 2.0 *2.0 = 8.0

Return type Casting:

package PreDefinedMethods;

public class ReturnTypCasting

{

               public static float add(int a, int b)

               {//                        2        3 so a =2  ,  b =3

                              int res= a+b;

                              //        2 + 3

                              //          5

                              //   int  res=  5

                              return res;//   returning int val

                              //     return 5;  returning int value  --->  this val will be typacsted/ converted to float val

                              //            5 --> 5.0  returns float val  but not int val.

               }

               public static void main(String[] args)

               {

                              //  call add() pass 2,3

                              float result = add(2,3);

                              //                5.0

                              //   result = 5.0

                              //                  but not 5

                              // Display the o/p

                              System.out.println("result ="+result);

                              // val                          5.0

               }

}

o/p:

result =5.0

package PreDefinedMethods;

public class AddMethodWith2args {

               //

               public static int add(int a, int b)

               { //                      2        3

                              //                a=2   b =3

                              float res= a+b;

                              //          2 + 3

                              //            5

                              //   float  res=  5  // Implicit  type casting

                              //       conver int --> float

                              //          5 -->  5.0

                              //     res =    5.0

                              System.out.println("res="+res);// 

                              //                        5.0

//                           return  res;//   type mismatch: cannot convert from float to int

                              //       5.0

                             

                              return (int)res;

                              //           5.0 -->  5 ----->   this method gives o/p = 5

               }

               public static void main(String[] args)

               {

                              int val=   add(2,3);

                              //           5

                              //   val = 5

                              System.out.println("val ="+val);

                              //                     val =5

               }

}

 

o/p:

res=5.0

val =5

note:

 void squareGivenNoNew(4)

{

//   dont write return stmt inside method

}

//            void v=  squareGivenNoNew(4);// invalid  don’t use like this

void method -- cannot  return any val/   won’t give o/p

int a  =  squareGivenNoNew(4);// CE:

 

Note2:

package PreDefinedMethods;

public class SquareGivenNo2 {

               // Define -squareGivenNo()  pass int arg  and return type -void

               public static void squareGivenNo (int n)

               { //  2*2 = 4                         2 so n= 2

                              // 3*3  = 9

               //   n * n --   square no

                             

                              int result  = n*n;

                              //             2*2

                              //              4

                              //result  =4

                              System.out.println("result-"+ result);

                              //                             4

               }

               public static void main(String[] args)

               {

//                           int sqr =  squareGivenNo(3);//Error: Type mismatch: cannot convert from void to int

                              // error :      gives nothing as method return type is void

                              //  We cannot store  result into some variable

//                           System.out.println("sqr="+sqr);

                             

                              squareGivenNo(2);

                             

//                           void v = squareGivenNo(2);                          //Error:void is an invalid type for the variable v

               }

}

Method returns array:

By using a method, we can return:

  • int value or int variable
  • float value or float variable
  • char value or char variable
  • String value or String variable
  • Array variable name (to return a group of values)

Invalid examples:

  • return 10,20,30; // invalid
  • return a,b,c; // invalid

 

return val/ var/ expression;

Valid examples:

  • return val; // where val is a single value
  • return var; // where var is a single variable
  • return expression; // where expression evaluates to a single value

Syntax:

return arrayName;

ex:

package PreDefinedMethods;

public class MethodReturnsIntArray {

               //define M1() Method  return int array

               // method return type int[] - int array

               public static int[] M1()

               {

                              int a ;

                              int []  iarr =   new int [3];

                              iarr [0] = 10;

                              iarr [1] = 20;

                              iarr [2] = 30;

                              //  return arrayName;

                              return iarr;  // ---->>    gives o/p int array  [10,20,30]

                             

//                           return a;//Error: Method return type is int array i.e int [] . so we must return int array only but not int value

                              // Type mismatch: cannot convert from int[] to int

                              //   [10,20,30]

                             

                             

               }

               public static void main(String[] args) {

                              // call M1()

                              int [] arr =           M1();

                              //        [10,20,30]

                              //      arr  = [10,20,30]

                              //         0    1  2                  

                              // display array values

                              for(int i=0;i<=arr.length-1;i++)

                              {

                                             System.out.println("values from array="+ arr[i]);

                              }

               }

}

o/p:

values from array=10

values from array=20

values from array=30

 

               // Ex

//            public static float[]  display()

               //             char []

               //             boolean  []

               {

//                    floar farr[]

//                           char charr[]

//                           boolean barr[]

                              return farr;

               }

              

HW  WAP to Define method  and return float array    and display array values  in main()

HW  WAP to Define method  and return char array    and display array values  in main()

HW  WAP to Define method  and return String array    and display array values  in main()

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...