1. Method overloading
(1) Source code
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;
}
}
(2) Procedure:
(3) Analysis results, what is special?
This program shows the overloads of the method, the name of the method is the same, but the parameters are different, and both methods can be called, do not affect each other, but the result is not the same.
Conditions to satisfy overloading:
(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.
(3) One more thing to note:
The return value of a method is not evaluated as a method overload, and is related to the method's parameters.
In our usual function methods there are overloaded, the system gives, there are a lot of only the parameters are different, overloaded.
2. Calculate the number of combinations using a computer:
Content requirements (1) Use n! to calculate (2) Use recursive method to calculate with Yang Hui triangle (3) Use recursive method to calculate with combined number recursion formula
1) Source Code
Import Java.util.Scanner;
(1) Using the combination number formula to calculate (2) using a recursive method with the Yang Hui Triangle (3) using the recursive method to calculate the n! formula with the combined number
Public class Suanfashu {
Public Static void Main (String args[])
{
System. out. println ("combination number:" +s1(8,5));
long s;
s=S2(8)/(S2(5) *S2(8-5));
System. out. println ("Combination number:" +s);
}
Static long s1 (int n,int k)//first method, using recursive method to find the combination number
{
Long cnn=1,ckk=1,cnk=1;
for (int i=1;i<=n;i++)
{
Cnn=cnn*i;
}
for (int i=1;i<=k;i++)
{
Ckk=ckk*i;
}
for (int i=1;i<=n-k;i++)
{
Cnk=cnk*i;
}
return cnn/(CKK*CNK);
}
static //second method, recursive
long s2 (int N)
{
if (n==0| | n==1)return 1;
return nS2(n-1);
}
}
2) Compilation error
The use of recursive method is necessary to have the end of the condition, there is no end condition when the error, in writing the function must have static, otherwise it can not run out, and string args[] must have, otherwise you can not find the function main function, can not run.
3) Conclusion
Recursion is "oneself call oneself", characteristic has his recursion is first from big to small, then from small to big;
Every step to do is similar, but its size "small one"; the process of having to keep a recursive call can be terminated, and each recursive function must have a variable that controls recursion to the end;
Recursion "is" from the front to the back, the first to seek the 1th item, then, on this basis, the 2nd item, 3rd item, until the nth item, usually can be implemented directly using a circular statement.
4)
5) Design Ideas
Combined number evaluation, recursive and recursive, with two methods, respectively, the two methods, and then in the main method called the two methods, output results.
3.
(1) Source code
import Java.math.BigInteger;
import Java.util.Scanner;
Public class Calculaten {
/**
* @param args
*/
Public Static void Main (string[] args) {
System. out. Print ("Please enter N:");
Scanner scanner=New Scanner (System. in);
int number=scanner.nextint ();
System. out. println (number+ "! =" +calculateN2(number));
}
Public Static long calculaten (int n) {
if (N==1 | | n==0) {
return 1;
}
return ncalculaten(n-1);
}
Public Static BigInteger calculateN2 (int n) {
if (N==1 | | n==0) {
return BigInteger. valueOf (1);
}
return BigInteger. valueOf (n). Multiply (calculateN2(n-1));
}
}
(2) Program results
(3) Analysis of results
The result is a negative number, it should be an integer, but because of the factorial result is too large, out of range, so there will be garbled, can be used to large numbers to represent, so there will be no overflow situation.
Public Static BigInteger calculateN2 (int n) {
if (N==1 | | n==0) {
return BigInteger. valueOf (1);
}
return BigInteger. valueOf (n). Multiply (calculateN2(n-1));
}
After the change:
There will be no overflow.
4. Hanoi Tower Problem
(1) Source program
Package demo;
Recursive programming to solve the problem of the Han-nuo tower.
Chen Xiaoyang 2016.10.19
Public class Towersofhanoi
{
Recursive moving trays on the tower
Public Static void solvetowers ( int disks, int sourcepeg,
int destinationpeg, int temppeg)
{
Base Case – Only one disk to move
if (disks = = 1)//leave only one plate left
{
System. out. printf ("\n%d-to-%d", sourcepeg, Destinationpeg);
return;//return empty, will go wrong when removed
}
Move the disks-1 tray from the starting tower to the middle tower, using the target tower.
solvetowers (Disks-1, Sourcepeg, Temppeg, destinationpeg);
The Destinationpeg is the final tower; Sourcepeg represents the starting tower, the temppeg represents the Middle tower
Output, from start point to Endpoint
System. out. printf ("\n%d-to-%d", sourcepeg, Destinationpeg);
Move the disks-1 on the middle tower to the target tower.
solvetowers (Disks-1, Temppeg, Destinationpeg, sourcepeg);
}
Public Static void Main (string[] args)
{
int startpeg = 1; Start position is set to 1
int endpeg = 3; Target Tower set to 3
int temppeg = 2; The center tower is set to 2
int totaldisks = 3; Number of plates
Calling Recursive methods
solvetowers (Totaldisks, Startpeg, Endpeg, temppeg);
}//End Main
}//End Class Towersofhanoi
(2) Design ideas
First, the n-1 plates on the starting tower are moved to the center tower, and then the nth is moved to the target tower, and the n-1 plates on the middle tower are moved to the target tower, with the help of the starting tower. Mobile with a method, the method is written as a recursive function, recursive invocation.
(3) Results
(4) Compilation result Analysis:
First of all, we must analyze how to move, write these steps into a function, recursive call; every move must be output. It's easy to understand, but the program doesn't write well and it's hard to understand the program.
5. Palindrome number
(1) Source program
Package demo;
import Javax.swing.JOptionPane;
Public class Number {
Public Static void Main (String args[])
{
int number=0,d5,d4,d3,d2,d1;
String Str=joptionpane. Showinputdialog ("Enter a number from 1 to 99999");
Number=integer. parseint (str);//Take the number entered
if (number>=1&&number<=99999)//Determine if number is between 1-99999
{
d5=number/10000;//represents the million-bit
d4=number%10000/1000;//= thousand
d3=number%1000/100;//represents the Hundred
d2=number%100/10;//represents 10-bit
d1=number%10;//means digit
if (d5>0)//Determine whether it is a 5-digit number, yes, it is a palindrome number
{
System. out. println (number+ "is a 5-digit number");
if (D5==D1&&D4==D2)
{
System. out. println (number+ "is a palindrome number");
}
Else
{
System. out. println (number+ "not palindrome number");
}
}
Else if (d4>0)//Determine whether it is a four-digit number, if it is four digits to determine whether it is a palindrome number, otherwise it will be judged whether it is three digits
{
System. out. println (number+ "is a 4-digit number. ");
if (D4==D1&&D3==D2)
{
System. out. println (number+ "is a palindrome number");
}
Else
{
System. out. println (number+ "not palindrome number");
}
}
Else if (d3>0)//Determine whether it is a three-digit number, is a three-digit number to determine whether it is a palindrome, not a three-digit number to determine whether it is a double-digit
{
System. out. println (number+ "is a 3-digit number");
if (D3==D1)
{
System. out. println (number+ "is a palindrome number");
}
Else
{
System. out. println (number+ "not palindrome number");
}
}
Else if (d2>0)//To determine whether it is a double-digit, yes, the judge whether it is a palindrome number, not to judge whether it is a number
{
System. out. println (number+ "is a 2-digit number");
if (D2==D1)
{
System. out. println (number+ "is a palindrome number");
}
Else
{
System. out. println (number+ "not palindrome number");
}
}
Else if (d1!=0)
{
System. out. println (number+ "is a 1-digit number");
System. out. println (number+ "is a palindrome number");
}
}
Else
{
System. out. println (number+ "not between 1-99999");
}
}
}
(2) Design ideas
Enter a number, the judge is not in the 1-99999, respectively, the number of inputs to the number of people, judge is a few digits, and then determine whether the palindrome number, the result of the output judgment, to note that even the number of digits and odd number of digits is different, even compared to the first half and the second half, the odd comparison is the first half and There is one left in the middle, no comparison. The direct output is a palindrome number when it is a one-digit number.
(3) Results
Java function methods