Start brain 4, start brain

Source: Internet
Author: User

Start brain 4, start brain

1. The static Class Method for Finding the number of workers is Square. java. It does not need to be static, but still wants to call the processing method in main.

// Class Object Instantiation // Wang Rongrong 2016/10/16 public class SquareIntTest {public static void main (String [] args) {for (int x = 1; x <= 10; x ++) {SquareIntTest obj; // example of creating a class obj = new SquareIntTest (); int result = obj. square (x); // The Math Library also provides the method for finding the number of squares. // result = (int) Math. pow (x, 2); System. out. println ("The square of" + x + "is" + result + "\ n") ;}// static public int square (int y) {return y * y ;}}

Result:

2. Compile a method to generate a random integer of a specified number (for example, 1000) using the following algorithm.

// Wang Rongrong 2016/10/15 import java. util. random; public class Random1000 {public static void main (String [] args) {Random r1 = new Random (1000); System. out. println ("the first Random object with 1000 seeds"); System. out. println ("r1.nextBoolean (): \ t" + r1.nextBoolean (); System. out. println ("r1.nextInt (): \ t" + r1.nextInt (); System. out. println ("r1.nextDouble (): \ t" + r1.nextDouble (); System. out. println ("r1.nextGaussian (): \ t" + r1.nextGaussian (); System. out. println ("---------------------------");}}

Result:

 

 

3. Please refer to the following code. Have you found anything special?

 

Result:

 

The sample code above demonstrates the "overload" feature of Java. When 7 ^ 2 is calculated, the integer type is called.

Public Static IntSquare (IntX ){

ReturnX * x;

}

When 7.5 ^ 2 is calculated, the double-precision type is called.

Public Static DoubleSquare (DoubleY ){

ReturnY * y;

}

Two or more methods that meet the following conditions constitute an "overload" relationship:

(1) method names are the same;

(2) The parameter type is different, the number of parameters is different, or the order of parameter types is different.

The preceding overload is of different parameter types.

Note: the return value of a method is not used as a condition for determining method overloading.

4. Check the System. out. println () method in JDK. What did you find?

 

In the System. out. println () method, you can enter many types in the parameter table.

5. BUG in the CalculateN sample program, 50! A negative number is displayed.

 

Cause: because the computer uses a fixed number of digits to save the value, the size of the value to be processed is limited. When the value to be processed exceeds this range, the computer automatically truncates the binary value to indicate the maximum number of digits it can process, which leads to incorrect processing results.

6. Use a computer to calculate the number of combinations:

(1) Use the combination number formula to use n! To calculate

Source code:

Import java. math. bigInteger; import java. util. optional; public class Zhuheshu {public static void main (String [] args) {System. out. print ("Enter N:"); partition = new partition (System. in); int n = random. nextInt (); System. out. print ("Enter M:"); required scanner1 = new partition (System. in); int k = scanner1.nextInt (); System. out. println (ca (n ). divide (ca (k ). multiply (ca (n-k);} public static BigInteger ca (int m) {if (m = 1 | m = 0) {return BigInteger. valueOf (1);} return BigInteger. valueOf (m ). multiply (ca (m-1 )));}}

Result:

7. recursive programming solves the tower issue. Implemented in Java

Source program:

// Use recursive programming to solve the indexing tower problem // Wang Rongrong 2016/10/16 import java. util. authorization; public class Hannuota {public static void main (String [] args) {System. out. print ("Enter the number of plates:"); portable partition = new partition (System. in); int disks = keys. nextInt (); // Number of plates final int source = 1; // the initial position of the plate on the first column final int desk = 3; // final int spare = 2 on the third pillar of the plate; // move (disks, source, desk, spare) on the second pillar to temporarily store the plate );} public static void move (int disks, int source, int desk, int spare) {if (disks = 1) System. out. println (source + "->" + desk); else {move (disks-1, source, spare, desk); System. out. println (source + "->" + desk); move (disks-1, spare, desk, source );}}}

Result:

8. use recursion to determine whether a string is palindrome)

 

// Wang Rongrong 2016/10/15 import java. util. optional; public class Huiwen {public static void main (String [] args) {String str = ""; System. out. println ("enter a string:"); bytes in = new bytes (System. in); str = in. nextLine (); StringBuffer sb = new StringBuffer (str); sb. reverse (); int n = 0; for (int I = 0; I <str. length (); I ++) {if (str. charAt (I) = sb. charAt (I) n ++;} if (n = str. length () System. out. println (str + "is a reply! "); Else System. out. println (str +" is not a return! ");}}

Result:

 

Related Article

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.