动手动脑
动手动脑一:纯随机数发生器
源代码:
package Test;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random r = new Random(1000);
int Modulus = 2<<(30)-1 ;
int Multiplier = 16807;
int C = 0;
int x = r.nextInt();
for(int i=0;i<1000;i++) {
x = (Multiplier*x+C)%Modulus;
System.out.println(x+" ");
}
}
Random r = new Random(1000);
int Modulus = 2<<(30)-1 ;
int Multiplier = 16807;
int C = 0;
int x = r.nextInt();
for(int i=0;i<1000;i++) {
x = (Multiplier*x+C)%Modulus;
System.out.println(x+" ");
}
}
}
运行结果:
动手动脑二:java方法的重载
源代码:
// MethodOverload.java
// Using overloaded methods
// Using overloaded methods
public class MethodOverload {
public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println("\nThe square of double 7.5 is " + square(7.5));
}
System.out.println("The square of integer 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;
}
return x * x;
}
public static double square(double y) {
return y * y;
}
}
return y * y;
}
}
特殊之处:具有相同的方法名,参数类型不同,形成了java方法的重载
java方法重载条件:
1.方法名相同
2.参数类型不同,参数个数不同,参数类型的顺序不同
注意:方法的返回值不能作为方法重载的判断条件
在JDK中System.out.println()源码:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
同样也是方法。