Java language programming (12) Math mathematics class, method overloading and variable scope

 1. Overload method
     The max method used in the previous article can only be used for int data types, but if you need to decide which of the two floating-point numbers is larger, the solution is to create another method with the same method name but different parameters , The code is as follows:
      public static double max(double num1, double num2){
          if(num1>num2)
              return num1;
          else 
              return num2;
}
      If you call the max method with int type parameters, it will call the max method that requires int type If the max method with double type parameters is needed, the max method with double type parameters will be called. This is called method overloading. There are two methods in a class. They have the same name but different parameter lists. Java The compiler decides which method to use based on the method signature.
      Below we use an example to illustrate, we want to write a program, create three methods in the program, the first method is to find the largest integer, the second method is to find the largest double precision number, and the third method is to find three double precisions. The maximum value in the number, these three methods are named max, the program list is as follows:
package testmax;
/**
 *
 * @author john
 */
public class TestMax {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("The maxium between 3 and 4 is"+max(3,4));
        System.out.println("The maxium between 3.0 and 5.4 is"+max(3.0,5.4));
        System.out.println("The maxium between 3.0 , 5.4 and 10.14 is"+max(3.0,5.4,10.14));
    }
    public static int max(int num1,int num2){
        if(num1>num2)
            return num1;
        else
            return num2;
    }
    public static double max(double num1,double num2){
        if(num1>num2)
            return num1;
        else
            return num2;
    }
    public static double max(double num1,double num2,double num3){
        return max(max(num1,num2),num3);
    }
    
}

image

      When calling max(3,4), the max method of the larger of the two integers is called. When calling max(3.0,4.5), the max method of finding the larger of the two double-precision numbers is called . When calling max(3.0,5.4,10.14), it calls the max method to find the maximum value of the three double-precision numbers.
      We can also call the max method with an int value and a double value like max(3,2.5), that is, call the method to find the larger of the two double numbers, the actual parameter value 2 is automatically converted to a double value, and then Pass to this method.
      We may all have some doubts, that is why max(double,double) is not used when calling max(3,4). In fact, max(double,double) and max(int,int) are both possible matches. When calling a method, the Java compiler looks for the most exact matching method. Because int is more precise, max(int,int) is called. If neither of the two methods is more precise than the other, ambiguity will occur, leading to compilation errors.
      2. The scope of the
      variable The scope of the variable refers to the range in which the variable can be referenced in the program, and the variable defined in the method is called a local variable. The scope of a local variable starts from the place where the variable is declared and ends at the end of the block containing the variable. Local variables must be declared and assigned before use. Here is an example:

      public static void method(){

          int x=1;

          int y=1;

          for(int i=1;i<10;i++){

              x=x+i;

             }

          for(int i=1;i<10;i++){

              y=y+i;

              }

}

      From this example, we can see that we can declare local variables with the same name in different blocks of a method, but we cannot declare a local variable twice in a nested block or in the same block. The following is wrong programming

      public static void method2(){

            int x=1;

            int I = 0;

            for(int x=1;x<10;x++){

            sum=sum+x;

             }

      Let us give another wrong example:

      for(int i=0;i<10;i++){

            }

      System.out.println("i");

      The mistake of this program is that we don't declare variables inside the block, and then try to use it outside the block, because i is not defined outside the for loop, so the last statement will produce a syntax error.

      3. Math class

      The Math class contains the methods needed to complete basic mathematical functions. We have already

 I have used the method pow(a,b) and the method Math.random. This time we will introduce other methods in the Math class. These methods are divided into three categories: trigonometric function methods, exponential function methods and service methods, except for these In addition to methods, the Math class also provides two useful double constants, PI and E (the base of the natural logarithm), which can be used in any program in the form of Math.PI and Math.E .

      (1) Trigonometric function method

      The Math class contains the following trigonometric function methods:

      public static double sin(double radians)

      public static double cos(double radians)

      public static double tan(double radians)

      public static double toRadians(double degree)

      public static double toDegrees(double radians)

      public static double asin(double a)

      public static double acos(double a)

      public static double atan(double radians)

      The parameters of sin, cos, and tan are all angles in radians. The return value of asin, acos, and atan is a radian value between -Π/2 and Π/2. 

      E.g:

      Math.toDegrees(Math.PI/2) returns 90.0

      Math.toRadians(30) returns Π/6

      Math.sin(0) returns 0.0

      Math.sin(Math.toRadians(270)) returns -1.0

      Math.sin (Math.PI / 6) returns 0.5

      Math.sin (Math.PI / 2) returns 1.0

      Math.cos(0) returns 1.0

      Math.asin (0.5) returns Π / 6

      (2) Exponential function method

     There are five methods related to exponential functions in the Math class:

      public static double exp(double x) returns e to the power of x

      public static double log(double x)  返回ln(x)

      public static double log10(double x)  返回log10(x)

      public static double pow(double a,double b) returns a to the b power

      public static double sqrt(double x) returns the root number x

      3.min max and abs method

      The overloaded min and max methods return the minimum and maximum of two numbers, and the overloaded abs returns the absolute value of a number. Math.abs(-2) returns 2

      4.random method

      We have used the random method to generate double random numbers greater than or equal to 0.0 and less than 1.0. We can use it to write simple expressions to generate random numbers in any range.

      int (Math.random()*10) returns a random number between 0 and 9.

      50+ ( int)(Math.random()*10) returns a random number between 50 and 59.

      a + (int)(Math.random()*b) returns a random number between a and a+b but not including a+b

      5. Rounding method

       public static double ceil(double x) returns the nearest integer greater than this number

       public static double floor(double x) returns the nearest integer smaller than this

       public static double rint(double x)   returns the double value whose value is closest to the parameter and is an integer . If the double values ​​of two integers are equally close, then the result is an even number


Guess you like

Origin blog.51cto.com/15064656/2602773