Hands on the brain
I. Instantiation of an object in a Java class
1) Definition: In object-oriented programming, the process of creating an object with a class is usually called instantiation, in the form of: Class Name Object name = new class name (parameter 1, parameter 2 ...). Parameter n);
such as date date=new date (), which is an object that was created with a date class as a date, is called an instantiation of the object. Instantiating an object is to create a memory space for the object, or to use the new constructor name () directly without declaring it, to establish a temporary object. 2) Example: Figure 1: no object instantiation
Figure 2: Instantiation of the object:
Two. Generating random numbers using linear same congruential
1) Definition:
2) Example:
Homework after class
I. Combinatorial number issues
1) Programming Ideas: 1) using factorial method to achieve 2) using the Yang Hui triangle of the recursive implementation of 3) the use of recursive return implementation
2) Source code:
2016/10/15 Xuetonggao
Three ways to implement combined numbers
Import java.io.*;
public class Combinatorialnumber {
public static void Main (string[] args) throws ioexception{
BufferedReader reader = new BufferedReader (new InputStreamReader (system.in));
String NUM1 = Reader.readline (); String num2 = Reader.readline ();
int n = integer.parseint (NUM1); int k = Integer.parseint (num2);
if (n>=2&&k>1&& (n>=k))
{
int sum1,sum2,sum3;
SUM1 = Nstratum (n,k);
System.out.println ("The Combinatorial Number of (N,k) is" + sum1);
sum2 = Triangleyh (n,k);
System.out.println ("The Combinatorial Number of (N+1,k) is" + sum2);
SUM3 = Digui (n,k);
System.out.println ("The Combinatorial Number of (N,k) is" + sum3);
}
else if (n>=2&&k==1)
{
System.out.println ("The Combinatorial Number of (N,k) is" + N);
}
else if (n==1&&k==1)
{
System.out.println ("The Combinatorial Number of (N,k) is 1.");
}
Else
{
SYSTEM.OUT.PRINTLN ("Error!please input again!");
}
}
public static int nstratum (int n,int k)//using factorial to achieve combinatorial number problems
{
int n_stratum=1,k_stratum=1,n_k_stratum=1,sum=1;
int i;
for (i=1;i<=n;i++) {n_stratum=n_stratum*i; }
for (i=1;i<=k;i++) {k_stratum=k_stratum*i; }
For (i=1;i<= (n-k); i++) {n_k_stratum=n_k_stratum*i; }
sum = n_stratum/(k_stratum*n_k_stratum);
return sum;
}
public static int Digui (int n,int k)//using recursive return to implement combinatorial number problems
{
if (k==1) return n;
Else
{
int sum = (Digui (n,k-1)) * (n-1)/k;
return sum;
}
}
public static int Triangleyh (int n,int k)//using the Yang Hui triangle to achieve combinatorial number problems
{
int sum,sum1,sum2;
SUM1 = Digui (n,k);
sum2 = Digui (n,k-1);
sum = sum1+sum2;
return sum;
}
}
3) Experiment
:
Two. Hanoi Tower problem
1) Programming Ideas:
(1) The input stream is set in the main method, the number of plates is entered, and the solvetowers method is introduced .
(2)solvetowers method: If the plate number is 1, then does not continue recursion, the end, if not 1, will continue to recursion if the remaining plate number is not 1: first n-1 a plate on the top of a to b , the largest disc moved to c , move the disc on the B to C ;
(3) Output in the main method.
2) Source code:
2010/10/14 Xuetonggao
solving the problems of the Nottingham Tower by recursive method
Import java.io.*;// Introduction Package
Public class Towersofhanoi
{
Public Static void Main (string[] args) throws ioexception// throw out the stream
{
BufferedReader reader = new bufferedreader (new InputStreamReader (System. in));// Set input stream
String num = Reader.readline ();// input
int n = Integer. parseint (num);// Forced type conversion
solvetowers (N, ' A ', ' B ', ' C ');// Reference method
}
Public Static void solvetowers (int n,Char A,char b,char c)// method: Solve the problems of the Nottingham Tower
{
if (n = = 1)// If the remaining plate number is 1 , The recursive end will not continue
System. out. println (" disk " + n + " moved from + A +" to " + C");
Else {// if the remaining number of plates is not 1 , continue to recursion : First move the top n-1 plate to b on
solvetowers (N-1, A, C, b);// algorithm: Move the n-1 disc to b
System. out. println (" disk " + n + "moved from + A +" to " + C");// algorithm: to move the largest disc to C .
solvetowers (N-1, B, A, c);// algorithm: move the disc on B to C
}
}
}
3) Experiment:
Three. Use recursion to determine whether a string is a palindrome
1) Programming Ideas: In the Main method called to determine whether the method is a function, the parameters of the function is (string, the corresponding character, after the corresponding character), if the position of the two characters is not equal, the recursive judgment has been to two characters of equal position, and if the two words typeface return true, if not equal, then return false.
2) Source code:
2016/10/15 Xuetonggao
Determine if a string is a palindrome
public class Palindrome {
public static Boolean ispalindrome (String s,int i,int J)
{if (i>j) throw new IllegalArgumentException ();
if (i = = j) return true;
else{return (S.charat (i) ==s.charat (j)) && Ispalindrome (s,i+1,j-1); }
}
public static void Main (string[] args) {
String test = "ABCBA";
int i=0; int J=test.length ()-1;
SYSTEM.OUT.PRINTLN (Test + "is palindrome?" + palindrome.ispalindrome (test, I, j));
}
}
3) Experiment:
Java Job 03