Question

In: Computer Science

java program Create a program that creates and prints a random phone number using the following...

java program

  1. 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

Solutions

Expert Solution

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:

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


Related Solutions

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....
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.
// 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...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
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...
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT