14. Record what happens when the following statements are executed:
(a) print (n=17)
(b) print (8+9)
(c) print (4.2, "hello", 6-2, "world", 15/2.0)
Answers
Explanation:
Well, Java does provide some interesting ways to generate java random numbers, not only for gambling but also several other applications especially related to gaming, security, maths etc.
Let's see how it's done!!There are basically two ways to do it-
Using Randomclass (in package java.util).
Using Math.random java class (however this will generate double in the range of 0.0 to 1.0 and not integers).
Let's look at them one by one -
Example: Using Java Random Class
First, we will see the implementation using java.util.Random-Assume we need to generate 10 random numbers between 0 to 100.
import java.util.Random;
public class RandomNumbers{
public static void main(String[] args) {
Random objGenerator = new Random();
for (int iCount = 0; iCount< 10; iCount++){
int randomNumber = objGenerator.nextInt(100);
System.out.println("Random No : " + randomNumber);
}
}
}
An object of Random class is initialized as objGenerator. The Random class has a method as nextInt. This will provide a random number based on the argument specified as the upper limit, whereas it takes lower limit is 0.Thus, we get 10 random numbers displayed.
Example: Using Java Math.Random
Now, if we want 10random numbers generated java but in the range of 0.0 to 1.0, then we should make use of math.random()
You can use the following loop to generate them-
public class DemoRandom{
public static void main(String[] args) {
for(int xCount = 0; xCount< 10; xCount++){
System.out.println(Math.random());
}
}
}
Now, you know how those strange numbers are generated!!!
Prev Report a Bug Next
YOU MIGHT LIKE:
JAVASCRIPT
Typescript vs JavaScript: What's the Difference?
JAVA TUTORIALS
How to Convert Char to String & String to char in Java
JAVASCRIPT
JavaScript Tutorial for Beginners PDF
JAVA TUTORIALS
Check Palindrome Number Program in Java
JAVA TUTORIALS
String indexOf() Method in Java with EXAMPLE
JAVASCRIPT
JavaScript Unit Testing Frameworks
Java Tutorials
About
About Us
Advertise with Us
Write For Us
Contact Us
Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Interesting
Books to Read!
Blog
Quiz
eBook
Execute online
Execute Java Online
Execute Javascript
Execute HTML
Execute Python
ANSWER:
1) >>> 17 =n
>>> print n=17
17
2) >>>print (8+9)
17
c)>>>print(4.2, "Hello"6-2,"world",15/2.0)
4.2 hello 4 world 7.5
HOPE IT HELPS YOU