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

  • 2.29 Random Phone Number (Java) Write a program that creates and prints a random phone number...

    2.29 Random Phone Number (Java) Write a program that creates and prints a random phone number of the form 668-555-xxxx where xxxx is a 4-digit randomly generated number. Include the dashes in the output. Think through how you can guarantee a 4-digit number. Your output should look like: A random phone number: 678-555-1234 Given: import random number public class PhoneNumbers { //----------------------------------------------------------------- // Produces a random phone number of the form 678-555-xxxx. //----------------------------------------------------------------- } }

  • Write a program in Python to solve the following task: In this project, you will build...

    Write a program in Python to solve the following task: In this project, you will build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and...

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

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

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

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