Question

In: Computer Science

Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX....

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

Solutions

Expert Solution

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


Related Solutions

java program Create a program that creates and prints a random phone number using the following...
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...
Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
Write an application, Phone Numbers, that creates and prints a random phone number of the form...
Write an application, Phone Numbers, that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. The phone number has some constraints. Do not let the first three digits contain an 3 or 7 (but do not be more restrictive than that) and ensure that the second set of three digits is not greater than 825. Note that any of the digits can be zero and zeroes should be shown.
In.java A phone number has the format (xxx)yyy-zzzz ( e.g.(942)731-2262 ). Write a program that reads...
In.java A phone number has the format (xxx)yyy-zzzz ( e.g.(942)731-2262 ). Write a program that reads a string and formats it as a phone number. First it should check that the string has exactly 10 symbols. If it does, the program will print the formatted version. If the string does not have exactly 10 symbols, print Invalid and do not process it. You do NOT need to verify that the symbols in the string are digits. Sample run 1... This...
// How to create a random number this program only creates a specific same number #include...
// How to create a random number this program only creates a specific same number #include <iostream> #include <iomanip> double random (int &seed); bool determine_larger (double rand_num); void print_results (double rand_num); using namespace std; int main() { int seed = 8; double rand_num = random (seed); print_results (rand_num); return 0; } // function: random number // does:generates a random number between (0, 1) double random (int &seed) { int const MODULUS = 15749; int const MULTIPLIER = 69069; int const...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Write a program for XXX Phones, a provider of cellular phone service. Prompt a user for...
Write a program for XXX Phones, a provider of cellular phone service. Prompt a user for maximum monthly values for talk minutes used, text messages sent, and gigabytes of data used, and then recommend the best plan for the customer’s needs. A customer who needs fewer than 400 minutes of talk and no text or data should accept Plan A at $29 per month. A customer who needs fewer than 400 minutes of talk and any text messages should accept...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT