Question

In: Computer Science

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.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

Let me know for any help with any other questions.

Thank You!
===========================================================================

import java.util.Random;

public class PhoneNumber {


    public static void main(String[] args) {

        Random random = new Random();
        int first3digits[] = {0, 1, 2, 4, 5, 6, 8, 9};

        int digit1 = first3digits[random.nextInt(first3digits.length)];
        int digit2 = first3digits[random.nextInt(first3digits.length)];
        int digit3 = first3digits[random.nextInt(first3digits.length)];

        int fourthSixthSeventhDigits = random.nextInt(825);
        int digit4 = fourthSixthSeventhDigits / 100;
        int digit5 = (fourthSixthSeventhDigits % 100) / 10;
        int digit6 = fourthSixthSeventhDigits % 10;

        int digit7 = random.nextInt(10);
        int digit8 = random.nextInt(10);
        int digit9 = random.nextInt(10);
        int digit10 = random.nextInt(10);


        System.out.println("Random Phone Number Generated:");
        System.out.printf("%d%d%d-%d%d%d-%d%d%d%d\n", digit1,digit2,digit3,digit4,digit5,digit6,digit7,digit8,digit9,digit10);

    }
}

=====================================================================


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...
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....
in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
write an application that inputs a telephone number as a stringin the form (555) 555_5555....
write an application that inputs a telephone number as a string in the form (555) 555_5555. the application should use string method split to extract the area code as a token. the first three digits of the phone number as a token and the last four digits of the phone number as a token. the seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed. Remember that...
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
Write an application that asks the user for a natural number. Display all the natural numbers...
Write an application that asks the user for a natural number. Display all the natural numbers up to and including the user's input. Also display the sum of all those numbers. JAVA
5. Write a program that prints all numbers between 27 and 78, one number per line....
5. Write a program that prints all numbers between 27 and 78, one number per line. 6. In questions 6,7 the following list is used: [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12] 7. Using “for” loop, write program that finds the smallest number in the list. 8. Using “for” loops, calculate arithmetic mean of all numbers in the list. Do not use built-in function in Python. 9. For this question envision you are creating a dummy alert to help the doctor determine if patient needs a...
Write a JavaFX application that draws 5 squares. Use a random number generator to generate random...
Write a JavaFX application that draws 5 squares. Use a random number generator to generate random values for the size, x, and y. Make the size between 100 and 200, x between 0 and 600, y between 0 and 400. You can pick any color, but you must use different colors for the 5 squares. Set your window to 600 by 400.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT