Question
The java.util.Random class is a Java class used to create a random number generator. The random number generator can then be used to get pseudo-random
The java.util.Random class is a Java class used to create a random number generator. The random number generator can then be used to get pseudo-random numbers for use in your programs.
Which code choice creates a random number generator object AND saves a random integer within the range [10,20] to the variable x?
Feel free to use your ScannerPractice class from the previous question to practice using the Randomclass methods.
1)
Random rng = new Random(10,20); int x = rng(10) + 10;
2)
Random rng = new Random(0); int x = rng.nextInteger(10)+10;
3)
Random rng = new Random(); int x = rng.nextInt(11)+10;
4)
Random rng = new Random(10,21); int x = rng();
2.
The System.out object is a PrintStream and is used to output to the terminal window (screen). The java.util.PrintWriter class is the primary class used to write (print) a program's output to a file. Create PrintWriter objects and use them to write program output to a file. If the file does not exist, the PrintWriter instance will attempt to create it.
Read: https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html (Links to an external site.)Links to an external site.
Add a new class named PrintPractice to your CS400Practice project. Add a main method to your PrintPractice program class. Then, add the code fragment below to the main method. Try running your PrintPractice program.
Did you forget to add the required import statements? If not, great. If you did forget, include the question "what do I need to import?" to your Java program development process and continue learning when a built-in or defined class requires an import statement.
Can you find and open the output file that was created? You may have to refresh the project folder to find the file that is created. Open the file with a text-only editor. Word is not a text-only editor. Use Notepad, Sublime, TextPad, or some other text-only editor on your computer.
PrintWriter pw = new PrintWriter("output.txt"); String message = "Hello World"; String s1 = String.format("output: %10d %7.2f %s", 123, 3.1415927, message); String s2 = "1 + 2 = " + (1+2) + " Good-Bye!"; pw.print(s1); pw.println(s2); pw.printf("Le Fin %10.4f ", Math.PI); pw.close();
Change the method and experiment to learn what each of these PrintWriter methods do: print, printf, println, format, close, flush.
What is the output of the above code fragment?
1)
output: 123 3.14 Hello World 1 + 2 = 3 Good-Bye! Le Fin 3.1415927
2)
output: 123 3.14 Hello World1 + 2 = 3 Good-Bye! Le Fin 3.1416
3)
output: 1233.14 Hello World 1 + 2 = 3 Good-Bye! Le Fin 3.1416
3.
You are writing a Rectangle class with a constructor that throws an IllegalArgumentException given a width or length that is negative.
Fill in the two blanks:
public class Rectangle { private int length; private int width; public Rectangle (int length, int width) throws IllegalArgumentException { if (length < 0 || width < 0 ) { (blank1) (blank2) IllegalArgumentException(); } this.length = length; this.width = width; } }
4.
Consider the following classes:
public interface Animal {...}
public abstract class Mammal implements Animal {....}
public class Dog extends Mammal {...}
Which of the following are legal statements?
1)
Animal andy = new Mammal();
2)
Mammal andy = new Dog();
3)
Animal andy = new Dog();
4)
Mammal andy = new Mammal();
5)
Animal andy = new Animal();
6)
Dog andy = new Dog()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started