In: Computer Science
Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes. Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint: Think though the easiest way to construct the phone number. Each digit does do not have to be determined separately.
EXAMPLE OUTPUT: //Professor Name //JAVA IDE //Assignment //randomly selected telephone number 773-345-8787
import java.util.Random;
public class GeneratePhoneNumber {
public static void main(String[] args) {
String s = generatePhone();
System.out.println(s);
}
private static String generatePhone() {
Random r = new Random();
int n = getRandomNumber(r, 1, 7);
String res = n + "";
n = getRandomNumber(r, 1, 7);
res = res + n;
n = getRandomNumber(r, 1, 7);
res = res + n + "-";
n = getRandomNumber(r, 100, 673);
res = res + n + "-" + getRandomNumber(r, 1000, 8999);
return res;
}
private static int getRandomNumber(Random r, int m, int n) {
return r.nextInt(n) + m;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME