CSC1110 - Week 5 Homework
Instructions
Create class called PhoneBook that has a single private String attribute called phoneNumber that stores a phone number. This class should have as setter for the phone number as well as the methods listed below.
- A private method called firstDash() that takes in nothing and returns an int. This int should be the index of the first dash (i.e., ‘-‘) in the phone number or -1 if there are no dashes.
- A private method called secondDash() that takes in nothing and returns an int. This int should be the index of the second dash (i.e., ‘-‘) in the phone number or -1 if there are no dashes.
- Three public methods called getCountryCode(), getAreaCode(), and getLocalNumber() that each take in nothing and return a String. Each method takes in nothing and returns a String. These methods return the country code, area code, and local number of the phone number respectively. These methods should make use of the helper methods firstDash() and secondDash().
Create a driver called PhoneBookDriver that creates an instance of a PhoneBook called pb, and asks the user for a phone number in the form of cc-area-local or ‘q’ to quit. Get the input from the user and use it to set the phone number of the PhoneBook object you created. You can assume the phone number entered by the user is a valid phone number. Use the methods of the PhoneBook object to print out the country code, area code, and local number of the phone number. Repeat the prompt, each time setting the new phone number entered by the user, until the user enter 'q'. If the user entered ‘q’ the program should end and print “Quitting”. Do not use break, System.exit(), or return to terminate a loop.
Sample Output
Enter a phone number in the form cc-area-local, where cc = country code digits, area = area code digits, and local = local phone digits. Or enter q to quit: 1-816-7412000 country code = 1 area code = 816 local number = 7412000 Enter a phone number in the form cc-area-local, where cc = country code digits, area = area code digits, and local = local phone digits. Or enter q to quit: 86-131-12345678 country code = 86 area code = 131 local number = 12345678 Enter a phone number in the form cc-area-local, where cc = country code digits, area = area code digits, and local = local phone digits. Or enter q to quit: 1234567-89-0 country code = 1234567 area code = 89 local number = 0 Enter a phone number in the form cc-area-local, where cc = country code digits, area = area code digits, and local = local phone digits. Or enter q to quit: 1-2-3 country code = 1 area code = 2 local number = 3 Enter a phone number in the form cc-area-local, where cc = country code digits, area = area code digits, and local = local phone digits. Or enter q to quit: q
Submission
See your professor's instructions for details on submission guidelines and due dates.