Suppose that someone has defined for us a class called Random. For each of the f
ID: 3763443 • Letter: S
Question
Suppose that someone has defined for us a class called Random. For each of the following parts, write a single line of Java code that does what is described by the sentence. (a) Declare r to be a reference variable of type Random and assign to r a Random object. (b) Send the object named r the nextDouble message (which tells the object to compute a random number) and have the result printed in a console window. (c) Declare a variable of type int and assign to it the result of sending the object named r the nextInt message with parameter 10. (d) Create a String object containing the text "This is so much fun" and send the object the subString message with parameters that extract the first eleven letters of the string. Assign the result of this calculation to a variable called str which is declared to be of type String. (e) Send the object named str the concat message with parameter "easy". To the result of this calculation, send the toUpperCase message. (`concat' is an abbreviation for concatenate, which means to put the string "easy" at the end of the string named by str.)
Explanation / Answer
import java.util.Random;
/**
* @author srinu
*
*/
public class RandomTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r = new Random();
double ranDouble = r.nextDouble();
System.out.println("Random Double :" + ranDouble);
int ranInt = r.nextInt(10);
System.out.println("Random Int :" + ranInt);
String strVal = new String("This is so much fun");
String str = strVal.substring(0, 11);
System.out.println("Substring :" + str);
str = str.concat("easy");
String upperCase = " `concat' is an abbreviation for concatenate, which means to put the string "easy" at the end of the string named by str."
.toUpperCase();
System.out.println(str + upperCase);
}
}
OUTPUT :
Random Double :0.19430005359826252
Random Int :0
Substring :This is so
This is so easy `CONCAT' IS AN ABBREVIATION FOR CONCATENATE, WHICH MEANS TO PUT THE STRING "EASY" AT THE END OF THE STRING NAMED BY STR.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.