Question:
random class java program?
AB
2012-04-12 18:25:25 UTC
If you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random
(or java.util.*), what will happen when you attempt to compile and run your program?
Four answers:
Jamie
2012-04-12 18:34:04 UTC
The program would fail to compile and the compiler would give you an error, saying that there is an "undefined symbol", "undefined variable", or some other error related to the Random class not being imported into your program.



However, you could still use methods and attributes from the "Random" class, without importing it, by using the fully qualified name of the class in any statements or lines of code that you write within your program.



For example, you could use the "Scanner" class without importing it by writing some code similar to the code below.



java.util.Scanner scan = new Scanner(System.in);



By using the above technique, you would be able to compile and run the program, but for every instance of the "Scanner" class that you'd like to use, you would have to create a new object using the fully qualified name of the "Scanner" (or any other) class.



Good luck and I hope I helped you!
Yentl
2012-04-15 13:15:48 UTC
It will not compile, which is normal! It shows an error or more...

Your program does not include the Random class package, so it can not make use of it, so it can not work.



the "import java.util.Random" statement makes it ready to use.



One note:



It's better to use "import java.util.Random" than "import java.util.*".



The reason why the first import statement the best is, is simply because your program has to import less java util library classes than with the second import statement. When you make use of "import java.util.*" it imports tha whole java util library.



I hope this explanation is good enough ?



Friendly greetings

Yentl F.
James Bond
2012-04-12 18:28:30 UTC
It will not compiler. Rather you get an error some thing like undefined symbol Random etc
truesdell
2016-10-04 12:36:59 UTC
jointly as(splendid = fake) ( gadget.out.print("wager the huge type:"); huge type = reader.nextInt(); count huge type++; if (answer==huge type) { splendid = genuine; gadget.out.println("super! You guessed it in" + count huge type + "guesses"); } else if (answer>huge type) { gadget.out.println("it quite is basically too low. try returned."); } else { gadget.out.println("it quite is basically too severe. try returned"); } )


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...