Question

Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output

ALGORITHM Store the phone number in a variable which is of String data type (String result = ) Create an object rand to gen

***** TO BE WRITTEN IN JAVA *****

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please up vote ,comment if any query . Thanks for question .

Note : check attached image for output . code compiled and tested in netbeans java IDE.

No digit generated seprately . all three digit generated in single number.

Program : ************************Main.java**********************


import java.util.Random;


public class Main {

    //get random takes object of random
    //and random max min
    //min should be inclusive and max should be exclusive
    public static int getRandomNumber(Random random,int min, int max) {
       
        return random.nextInt(max - min) + min; //get random number
    }
  
    public static boolean checkEightInNumber(int number)
    {
        while(number!=0) //loop till number not zero
        {
            int r= number%10; //get reminder
            if(r==8) //if reminder is 8 return false
                return false;
            number/=10; //get quotient
        }
      
        return true; //if no digit is 8 return true
    }
    public static void main(String[] args) {
        Random random = new Random(); //Random number

            String phoneNumber=""; //String phone number
          
            String str="";
            int number= getRandomNumber(random,0,888); //get number
          
            while(checkEightInNumber(number)==false) //if its contain 8 again generate random
            {
                number= getRandomNumber(random,0,888);
            }
          
            if(number==0) //if number =0
            {
                str+="000";
            }
            else if(number<10)
            {
                str+="00"; //if number is 1 digit
                str+=String.valueOf(number);
            }
            else if(number<100) //if number 2 digit
            {
                str+="0";
                str+=String.valueOf(number);
            }
            else //else convert it into string
                str=String.valueOf(number);

          
            phoneNumber+=str; //add string to phone number
            phoneNumber+='-'; //append - dash
            phoneNumber+=String.valueOf(getRandomNumber(random,100,743)); //between 100-742
            phoneNumber+='-'; //apppend - dash
            phoneNumber+=String.valueOf(getRandomNumber(random,1000,10000)); //1000-9999
           
                System.out.println(phoneNumber);
           
           

    }
  
}

Output :

Output - Phone Number (run) X IN run: 051-331-4927 135-531-9431 409-693-6303 562-475-1075 010-213-9123 OR 214-639-9972 704-32

Please up vote ,comment if any query .

Please up vote ,comment if any query .

Add a comment
Know the answer?
Add Answer to:
***** TO BE WRITTEN IN JAVA ***** Write an application that creates and prints a random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1. Write program calculate the final price of a purchased item using values entered by the...

    Q1. Write program calculate the final price of a purchased item using values entered by the user Hint: you are required to use formatting output for number, currency and percentage. --------------------[Print Receipt] ------------ Enter the quantity: 6 Enter the unit price: $1.98 Subtotals: $10.14 Tax: $ 0.61 at 6% Total: $10.75 ------------------------------------------------ Q2. Write a program that prompts for and reads the users’ first name and last name, Job title, DOB (date of Birth), and the Email address (separately). Then...

  • Add JavaScript scripts to the pizza order form to validate the formats of the phone number...

    Add JavaScript scripts to the pizza order form to validate the formats of the phone number and email. Validate the formats of the phone number and email when the previous validations have passed. Validate the phone number: If the phone number is not empty, validate that the phone number is in one of the following formats: ‘ddd-ddd-dddd’ – 10 digits with two dashes where the first dash is between the third and fourth digits and the second dash is between...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Write a program in Python that asks the user for the ISBN of the book in...

    Write a program in Python that asks the user for the ISBN of the book in the format xxx-x-xxx-xxxxx-x. To validate: Initialize a total to 0 Have the program remove the dashes so that only the 13 digits are left in the ISBN string for each index of the first 12 digits in the 13 digit ISBN string get the digit at the current index as an integer If the current index is an even number, add the digit to...

  • Program Instructions: Write a C++ program that uses a random number generator to generate a two...

    Program Instructions: Write a C++ program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: Double the number. Reverse the digits of the number. Raise the number to the power of 2, 3, or 4. Sum the digits of the number. If the number is a two digit number, then raise the first digit to the power of the second digit. If the...

  • DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code:...

    DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code: import java.util.Scanner; public class OddAndEven{ /* PART 1: Create a nonstatic method that takes in an int number quantity (n) and returns a returns a String of numbers from 0 to n (inclusive) as the example above demonstrates. Call this quantityToString.    In this method you should check that n is between 0(inclusive) and 100(inclusive). If n is outside these boundaries return and empty...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Write a program that uses a random number generator to generate a two digit positive integer...

    Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: a. Double the number. b. Reverse the digits of the number. c. Raise the number to the power of 2, 3, or 4. d. Sum the digits of the number. e. If the number is a two digit number, then raise the first digit to the power of the second digit....

  • Write a function called "phoneNumberFormat" that takes a string containing a ten-digit phone number (such as...

    Write a function called "phoneNumberFormat" that takes a string containing a ten-digit phone number (such as 5155551212) as input, convert into a more readable string with parentheses and dashes like (515)555-1212 and display it. If the input string contains more than or less than ten characters, display an error message. Note: Steps of phoneNumberFormat function can be given as follows: def phoneNumberFormat (phoneNum): 1. Let l be the length of the phoneNum 2. If 1 does not equal 10 then,...

  • Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from...

    Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print “4 2 3 3 9.” (Hint: The number input is five digits long, dividing it by 10000 gives the leftmost digit. digit1 = number / 10000 ) b. determines...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT