You are on page 1of 13

• Java Random class

• Java Random class is used to


generate a stream of pseudorandom
numbers.
• The algorithms implemented by
Random class use a protected utility
method than can supply up to 32
pseudo randomly generated bits on
each invocation.
METHODS DESCRIPTIONS
doubles() Returns an unlimited stream of
pseudorandom double values.
ints() Returns an unlimited stream of
pseudorandom int values.
next() Generates the next
pseudorandom number.
nextBoolean() Returns the next uniformly
distributed pseudorandom
boolean value from the random
number generator's sequence
nextByte() Generates random bytes and puts
them into a specified byte array.
nextLong() Returns the next uniformly
distributed pseudorandom long
value from the random number
generator's sequence.
import java.util.Random;
public class JavaRandomExample2 {
public static void main(String[] args) {
Random random = new Random();
//return the next pseudorandom integer value
System.out.println("Random Integer value : "+random.nextInt());
// setting seed
long seed =20;
random.setSeed(seed);
//value after setting seed
System.out.println("Seed value : "+random.nextInt());
//return the next pseudorandom long value
Long val = random.nextLong();
System.out.println("Random Long value : "+val);
}
}
• OUTPUT:
• Random Integer value : 1294094433 Seed
value : -1150867590
• Random Long value : -7322354119883315205
• Java Calendar Class
• Java Calendar class is an abstract
class that provides methods for
converting date between a specific
instant in time and a set of calendar
fields such as MONTH, YEAR,
HOUR, etc. It inherits Object class
and implements the Comparable
interface.
• Java Calendar class declaration
• public abstract class Calendar extends Object

implements Serializable, Cloneable, Comparabl


e<Calendar>
• List of Calendar Methods :
METHODS DESCRIPTIONS
public void add(int field, Adds the specified
int amount) (signed) amount of time
to the given calendar
field.
public int get(int field) In get() method fields of
the calendar are passed
as the parameter, and
this method Returns the
value of fields passed as
the parameter.
public static Calendar This method is used with
getInstance() calendar object to get
the instance of calendar
according to current
time zone set by java
runtime environment
public abstract int This method is used with
getMaximum(int field) calendar object to get the
maximum value of the
specified calendar field as the
parameter.

Sets the Time of current calendar


public final void setTime(Date object. A Date object id passed as
date) the parameter.
import java.util.*;
public class CalendarExample2{
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("At present Calendar's Year:
" + calendar.get(Calendar.YEAR));
System.out.println("At present Calendar's Day:
" + calendar.get(Calendar.DATE));
}
}
• Unit-1 : Recursion in Java
• Recursion in java is a process in which
a method calls itself continuously. A
method in java that calls itself is called
recursive method.
• It makes the code compact but
complex to understand.
Syntax:

returntype methodname(){
//code to be executed
methodname();//calling same method
}
public class RecursionExample {
static int factorial(int n){
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}

public static void main(String[] args) {


System.out.println("Factorial of 5 is: "+factorial(5));

}
}
• Output:
• Factorial of 5 is: 120

You might also like