Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) In what circumstances will random numbers generated using the Random class (

ID: 3868545 • Letter: #

Question

(1)In what circumstances will random numbers generated using the Random class (e.g., using the nextInt method) appear to be completely non-random?

(2)What rules must be adhered to when adding comments to a Java program, in order to obtain meaningful ‘javadoc’ output?

(3)What is the ‘this’ keyword used for in Java? Describe two situations in which it is necessary to make use of this keyword.

(4)How are static variables and methods created in Java? What happens if a non-static method of a class tries to access a static variable of that class? What happens to the static variables of a class once the class has no instances?

Explanation / Answer


1)
Answer:When you use nextInt method it avoid the duplicate values and return perfect no-random numbers. Usually random-numbers have repeated numbers where duplicates exist but using nextInt we can avoid them.Below is the sample code for it.
Sample code for te usage of nextInt:
import java.util.ArrayList;
import java.util.Random;
public class Test {
public static void main(String[] args) {
int size = 20;
ArrayList<Integer> list = new ArrayList<Integer>(size);
for(int i = 1; i <= size; i++) {
list.add(i);
}
Random rand = new Random();
while(list.size() > 0) {
int index = rand.nextInt(list.size());
System.out.println("Selected: "+list.remove(index));
}
}
}

2). Answer:
a) Comment begins with:
"/**" and ends with "*/" in every java documentation.
b) Single line comment: This begins with "//". This is used to represent the implementation details like expressions and purpose of variables.
Example: int countVal=0; //counts values
c) Every class should be given proper comments like what the class does etc in proper javadoc convention.
d) The comment for the class should name the class, its purpose, along with author name.
Example:

/**
* HelloWorldDemo --- program to display "Hello World".
* @author XYZ
*/
public class HelloWorldDemo {
:
}
e) Methods should have proper descriptio about its purpose,its arguments and return value.
/**
* Displays "Hello World" message
* the command line arguments.
* @exception
* @return value
*/
public static void main (String[] arg){
:
}
3) Answer:
this keyword: "this" is a reference keyword which is used to reference the current object.
There were 6 ways you can use this keyword:
1) this can be used to refer current class instance variable.
2) this can be used to invoke current class method
3) used to invoke current class constructor.
4) this can also be used as an method argument.
5) As argument in the constructor call.
6) To return the instance from the method which is present in current class.
4) Answer:
Static variables: Static variables are the one which can be accessed directly by class name and do not require any object to access.
Syntax : <className>.<variableName>
Static methods: This methods can be accessed directly by class name.
Syntax : <className>.<methodName>
A non-static member can access static variable of class and also modify them based on the requirement.
Instance methods can access class methods and variables directly but class methods cannot access instance variables directly.