In: Computer Science
java program
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:
//proffesor name
//JAVA IDE
// homework
//randomly selected telephone number
773-345-8786
Code is Given below:
========================
import java.util.Random;
public class PhoneNumber {
public static void main(String[] args) {
//creating Random object to
generate random numbers
Random rnd=new Random();
//generating first 3 digit
int num1 = rnd.nextInt(7) +
1;//numbers can't include an 8 or 9, can't go below 100.
int num12=rnd.nextInt(7);
int num13=rnd.nextInt(7);
//generating second 3 digit
int num2 = rnd.nextInt(662) + 100;//number has to be less than
773//can't go below 100.
//generating list digits
int num3 = rnd.nextInt(8999) + 1000; // make numbers 0 through
9
String phoneNumber;
//converting all numbers into string
String string1 = Integer.toString(num1);
String string12 = Integer.toString(num12);
String string13 = Integer.toString(num13);
String string2 = Integer.toString(num2);
String string3 = Integer.toString(num3);
//combining all digits
phoneNumber=string1+string12+string13+"-"+string2+"-"+string3;
//printing result
System.out.println("//proffesor name");
System.out.println("//JAVA IDE");
System.out.println("//randomly selected telephone number");
System.out.println(phoneNumber);
}
}
Output:
===============
Code Snapshot:
================