Random number:
1) Write a method that uses the following algorithm to generate a random integer of the specified number (for example , three) .
Modulus=231-1=int. MaxValue
multiplier=75=16807
C=0
It is possible to repeat after the number of 231-2 has been displayed.
Program:
import Java.util.Scanner;
Public class Random {
Public Static void Main (string[] args) {
TODO auto-generated Method stub
System. out. Print ("Enter the number you want to get:");
Scanner Scanner = new Scanner (System. in);
int n = scanner.nextint ();//The number of random numbers obtained
for (int i = 0;i < n;i++)//for loop
System. out. println ((int) (Math. random () *1000));//output gets 1-10000 of the stochastic number
}
}
Method overloads:
2) Please look at the following code, do you find anything special?
Public class Methodoverload {
Public Static void Main (string[] args) {
TODO auto-generated Method stub
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;
}
}
:
Two ways to 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. "
Program 7 is an int, square (7) calls an int square (int), resulting in the int type.
Program 7.5 is a double type, square (7.5) calls a double square (double), resulting in double type results
It is known that the program is the overloading of the method, the first is the calculation of the int type, and the second one is the double type operation.
Java Course assignment--hands-on brain