4.Write java program to demonstrate method overloading to generate random numbers, random alphabet sequence and random strings.
import java.util.Random;
public class p4{
public static void main(String arg[]){
RandomNum Rn=new RandomNum();
Rn.generate(5);
Rn.generate('a');
Rn.generate(10,'a');
}
}
class RandomNum{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void generate (int N)
{
System.out.println("Generate Random Number :");
Random random = new Random();
int length = N;
for(int i = 0; i < length; i++) {
int index = random.nextInt(alphabet.length());
System.out.println("Random Number : " + index);
}
}
void generate(char A){
System.out.println("Generate Random Character :");
StringBuilder sb = new StringBuilder();
Random random = new Random();
int length = 5;
for(int i = 0; i < length; i++) {
int index = random.nextInt(alphabet.length());
char randomChar = alphabet.charAt(index);
System.out.println("Random alphabet sequence is: " + randomChar);
}
}
void generate(int N, char A)
{
System.out.println("Generate Random Word:");
StringBuilder sb = new StringBuilder();
Random random = new Random();
int length = N;
for(int i = 0; i < length; i++) {
int index = random.nextInt(alphabet.length());
char randomChar = alphabet.charAt(index);
sb.append(randomChar);
}
String randomString = sb.toString();
System.out.println("Random String is: " + randomString);
}
}