Write a program that takes an input N and prints N random ph

Write a program that takes an input N and prints N random phone numbers of the form (xxx) xxx-xxxx. Use an array of Phone objects to avoid choosing the same number more than once.

Use this list of NY state area codes in an integer array to avoid printing out bogus area codes.

212, 315, 347, 516, 518, 585, 607, 631, 646, 716, 718, 845, 914, 917, 929

Write a class Phone3 fields (int data type)

area code(3digits), prefix(3digits), suffix(4digits)

write getter methods and setter methods for each field (member variable)

a constructor with 3 parameters (area code, prefix and suffix arguments to be passed and assigned to fields)

write the following methods

boolean isNumberMatch(int a, int p, int s) : returns true if given phone number is same as this object, and return false otherwise.

String getNumber(): returns a string number in the form (xxx)xxx-xxxx.

Write a class PhoneGen

asks integer number N

creates an array (size N) of Phone object

creates N random phone numbers

area code is randomly selected from the NY state area code array

prefix number is randomly selected from 300 to 399

suffix number is randomly selected from 1201 to 1500

check whether the same number aready exists in the array of phone number

if exists, create a new number until there is no match

otherwise, create an Phone object and put the object into the array of phone number

displays all N unique phone numbers in the form of (xxx)xxx-xxxx.

Paste the output (with the input data) of 2 sample runs as a comment at the bottom of program.

Solution

Writing C++ code for the required problem:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string to_string( int num ){
   string result = \"\";
   while( num != 0){
       result += (num%10 + \'0\' );
       num = num/10;
   }
   reverse( result.begin(), result.end() );
   return result;
}

class Phone3{
   int area_code;
   int prefix;
   int suffix;
public:
   int getAreaCode(){
       return area_code;
   }
   int getPrefix(){
       return prefix;
   }
   int getSuffix(){
       return suffix;
   }

   int setAreaCode( int code ){
       area_code = code;
   }
   int setPrefix( int prefx ){
       prefix = prefx;
   }
   int setSuffix( int sufx ){
       suffix = sufx;
   }

   bool isNumberMatch( int a, int p , int s ){
       return ((area_code==a) && (prefix==p) && (suffix==s));
   }
   string getNumber(){
       string result = \"(\";
       result += to_string(area_code);
       result += \")\" +to_string(prefix) + \"-\";
       result += to_string(suffix);
       return result;
   }
};

class PhoneGen{
public:
   PhoneGen(){
       int N;
       int NYAreaCodes[] = {212,315,347,516,518,585,607,631,646,716,718,845,914,917,929};
       cout << \"Enter the nummber of phones needed to generate: \";
       cin >> N;
       Phone3 phones[N];
       for(int i =0; i < N; i++){
           srand( time(NULL));
           int randAC = NYAreaCodes[ rand()%15 ];
           int randPr = rand()%100 + 300;
           int randSu = rand()%300 + 1201;
           int j = 0;
           for(; j < i; j++){
               if( phones[j].isNumberMatch( randAC, randPr, randSu )){ break; }}
           if( j == i ){
               phones[i].setAreaCode( randAC );
               phones[i].setPrefix( randPr );
               phones[i].setSuffix( randSu );
           }
           else{ i--; }
       }

       //display the numbers
       for(int i =0; i < N; i++){
           cout << phones[i].getNumber() << endl;
       }
   }
};

int main(){

   Phone3 p;
   p.setAreaCode(122);
   p.setPrefix(11);
   p.setSuffix(122);
   cout << p.getNumber() << endl;

   PhoneGen pg;
}

Write a program that takes an input N and prints N random phone numbers of the form (xxx) xxx-xxxx. Use an array of Phone objects to avoid choosing the same num
Write a program that takes an input N and prints N random phone numbers of the form (xxx) xxx-xxxx. Use an array of Phone objects to avoid choosing the same num
Write a program that takes an input N and prints N random phone numbers of the form (xxx) xxx-xxxx. Use an array of Phone objects to avoid choosing the same num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site