Java Course assignment--hands-on brain

Source: Internet
Author: User

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

Related Article

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.