Suppose that someone has defined for us a class called Random. For each of the f
ID: 3763238 • 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
CommentDemo.java // Demonstrating multi-line comments public class CommentDemo { public static void main(String[] args) { for (int i = 0; i < 5; /* exits when i reaches to 5 */ i++) { System.out.print(i + " "); } } } * CommentDemo.java * Java comments surrounded by double quotes are processed * as string literals instead of a comment by the Java compiler. */ public class CommentDemo { public static void main(String[] args) { String commS = "/* It looks like a comment but It is treated as string */"; String commS1 = "// It too look like a comment but It is treated as string"; System.out.println(commS); System.out.println(commS1); } } * UnicodeCommentDemo.java * Java programs are written using Unicode characters * Unicode presenting a character recognized as usual Java token */ public class UnicodeCommentDemo { public static void main(String[] args) { double pi = Math.PI; /* multiplies pi by 4 u002au002f // above comment is lexically equivalent to the following /* multiplies pi by 4 */ System.out.println(pi u002A 4); // u002A is Unicode of * } } // JavadocCommentDemo.java /** * First sentence of the comment should be a summary sentence. * Documentation comment is written in HTML, so it can * contain HTML tags as well. * For example, below is a paragraph mark to separate * description text from Javadoc tags. * * @author Krishan Kumar */ public class JavadocCommentDemo { /** main method * @param args String[] */ public static void main(String[] args) { System.out.println(sqrt(16)); } /** * computes sqrt of passed number of type double. * @param x double * @return sqrt of x */ public static double sqrt (double x) { return Math.sqrt(x); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.