Java Action Brain 02

Source: Internet
Author: User
Tags pow

One, square number static method:

Public class Squareint {

Public Static void Main (string[] args) {

int result;

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

result = square(x);

The Math library also provides a way to calculate the square number

result= (int) Math.pow (x,2);

System. out. println ("The square of" + x + "is" + result + "\ n");

}

}

A static method for calculating the square number of a custom

Public Static int Square (int y) {

return y * y;

}

}

Results:

The square of 1 is 1

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

The square of 5 is 25

The square of 6 is 36

The square of 7 is 49

The square of 8 is 64

The square of 9 is 81

The square of IS 100

Second, random number generation:

The math.random () generated in the Math function generates a random number.

You can also customize random number generation

Code:

import java.util.*;

Public class Suiji {

Public int suij (int N)

{

int a=16807; The value of a

int m= (int) Math. POW     (2, 31)-1; Set the value of M

int x= (a*n)% m; Generating random numbers by taking the remainder

return x;

}

Public Static void Main (String args[])

{

Suiji x=New Suiji ();

Random a=new random ();

int num=a.nextint (); Random generation of initial random numbers

Scanner input=New Scanner (System. in);

System. out. println ("Please enter the number of random numbers generated:");

int n=input.nextint (); Enter the number of generated random numbers

for (int i=1;i<=n;i++)

{

Num=x.suij (num);

System. out. Print (X.suij (num) + "");

System. out. println ();

}

}

}

Results:

Third, the variable parameters of Java:

Max (double ... values) the number of parameters in parentheses can vary

... "is located between the variable type and the variable name, and there are no spaces before and after it. When a variadic method is called, the compiler creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body.

Take a look at the following code, do you find anything special?

Methodoverload.java

Using Overloaded Methods

Public class Methodoverload {

Public Static void Main (string[] args) {

System. out. println ("The square of the integers 7 is" + square(7));

System. out. println ("\nthe square of double 7.5 is" + square(7.5));

}

Public Static int Square (int x) {

return x * x;

}

Public Static Double Square (double y) {

return y * y;

}

}

Output Result:

The square of the integer 7 is 49

The square of double 7.5 is 56.25

Method overloads because the parameter types are different and the method names are the same.

The "method overloading (overload)" attribute of Java.

two or more methods that meet the following conditions form an "overloaded" Relationship:

(1) The method name is the same, (2) The parameter type is different, the number of parameters is different, or the order of the parameter type is different. The return value of the method is not used as a criterion for method overloading.

Iv. Bugs in the Calculaten sample program : Negative numbers appear

Because the computer uses a fixed number of digits to hold the numeric value, the number of numeric values that can be processed is limited, and when the value to be processed exceeds that range, the computer automatically truncates the value of the binary representation as the maximum number of digits it can handle, which results in incorrect processing.

So with The BigInteger class, which supports the subtraction operation of large integers.

public static BigInteger calculateN2 (int n) {

if (n==1 | | n==0)

{

return biginteger.valueof (1);

}

return biginteger.valueof (n). Multiply (calculateN2 (n-1)); }

V. Compare two floating-point numbers:

Double i = 0.0001;

Double J = 0.00010000000000000001;

System.out.println (I==J); output:true

Computers cannot accurately express floating-point numbers (except for special forms), so when you need to compare two floating-point numbers for equality, you should compare whether the absolute value of the difference is within a certain allowable range.

if (Math.Abs (I-J) < 1e-10)

System.out.println ("true");

Else

System.out.println ("false");

Java Action Brain 02

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.