03 of the hands-on brain in Java

Source: Internet
Author: User

Class and object, object instantiation of Class

A class is an abstract concept, and an object is a concrete existence that arises in the form of a class, for example, human (Is the Class), and you (the object), the object is the concrete implementation of the class,

Object has all the implementations of the class definition. is an abstract concept, we cannot let the concept do one thing, but we can make the concept of the actual existence to do.

Generate random numbers

The Java.util.Random class is built in two ways: with seeds and without seeds

Without seeds:
This way will return random numbers, each running a different result

public class Randomtest {
public static void Main (string[] args) {
Java.util.Random r=new java.util.Random ();
for (int i=0;i<10;i++) {
System.out.println (R.nextint ());
}

}
With seeds:
This way, no matter how many times the program runs, the result is the same

public static void Main (string[] args) {
Java.util.Random r=new Java.util.Random (10);
for (int i=0;i<10;i++) {
System.out.println (R.nextint ());
}
}

Method overloading

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

(1) The method name is the same;

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

Note: The return value of the method is not used as a criterion for method overloading.

Yang Hui Triangle

public class Yanghuitriangle {
public static void Main (string[] args) {
int Triangle[][]=new int[10][];//Create a two-dimensional array
Traversing the first layer of a two-dimensional array
for (int i = 0; i < triangle.length; i++) {
Triangle[i]=new int[i+1];//Initializes the size of the second-level array
Traversing a second-level array
for (int j=0;j<=i;j++) {
Assigns an array element on both sides to a value of 1
if (i==0| | j==0| | J==i) {
Triangle[i][j]=1;
}else{//other values are calculated by formula
TRIANGLE[I][J]=TRIANGLE[I-1][J]+TRIANGLE[I-1][J-1];
}
System.out.print (triangle[i][j]+ "\ t"); Output array elements
}
System.out.println (); Line break
}
}
}

Palindrome number

System.out.println ("Please enter ...");
Scanner in = new Scanner (system.in);
String number = In.next ();
SYSTEM.OUT.PRINTLN ("You have entered" +number);
Boolean flag = true;
for (int I=0;i<number.trim (). Length ()/2;i++) {
Compare the character indexed to the I position with the character of its symmetric index position, and see if it is the same
if (! String.valueof (Number.charat (i)). Equals (String.valueof (Number.charat (Number.trim (). Length ()-i-1))) {
Flag = false;
Break
}
}
if (flag) {
System.out.println ("It is a palindrome number");
}else{
System.out.println ("It is not a palindrome number");
}

Hanoi Tower Problem

03 of the hands-on brain in Java

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.