The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 408 Weeks Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Problem 2
Â
Here is the specification for a phone book. A phone book consists of a collection of entries. An entry associates a person’s name (a string) with a set of one or more phone contacts. Every entry in a phone book has a unique name. In the description below, we use ‘name entry’ to refer to the entry named by name.
A phone contact consists of a phone number and a label. (These are the PhoneContact objects from Assignment 3.) For any given entry, every contact has a unique label. We use ‘label contact’ to refer to the contact labeled by label within an entry. We can look up an entry in a phone book by name and, given an entry, we can look up a contact by label.
Here are a phone book’s principal methods:
add(name, phone_number, label)
If self (i.e., this phone book) has a name entry, then add to this entry a new label contact with phone_number if the entry has no labelcontact, else replace the phone number in its label contact by phone_number. If self does not have a name entry, add a new name entry with a label contact for phone_number.
find(name)
Return an array of the contacts in name entry if this entry exists in self; otherwise return nil.
find_contact(name, label)
Return the label contact under the name entry in self. If self has no name entry or its name entry has no label contact, return nil.
delete(name)
Delete the name entry from self. If self has no name entry, do nothing.
delete_contact(name, label)
Delete the label contact from the name entry from self. If this is the only contact under this entry, also delete the entry from self. If self has no such contact, do nothing.
to_s
Return a string representing all of the entries in self, sorted by name.
The behaviors assume all inputs are well-formed (e.g., name is a string, phone_number is wellformed based on PhoneNumber.new). Here’s a sample interaction:
book = PhoneBook.new
book.add('Lennon, John', '954-555-1234', 'work') book.add('Garcia, Jerry', 9541112222, 'work') book.add('Lennon, John', '(954) 123-4567', :home) book.add('Garcia, Jerry', 9541234567, 'cell') book.add('Simon, Paul', '9543334444', 'cell')
Â
puts "**Entire book" puts book
puts "**Jerry's entry" puts book.find('Garcia, Jerry')
Â
if not book.find('Jagger, Mick') thenputs "**No entry for Mick" end
puts "**Paul's cell number:"
puts book.find_contact('Simon, Paul', :cell)
Â
book.add('Garcia, Jerry', '4445555555', :cell)
puts "**Jerry's new entry:" puts book.find('Garcia, Jerry')
Â
puts "**Entire phone book:" puts book
puts "**Delete John's work number" book.delete_contact('Lennon, John', 'work')
puts book.find('Lennon, John')
Â
puts "**Delete John's home number; should delete his entry"
book.delete_contact('Lennon, John', 'home') puts book.find('Lennon, John')
Â
puts "**Delete Paul's entry" book.delete('Simon, Paul')
Â
puts "**Now we have only Jerry's entry in the whole book" puts book
Â
Here’s the output produced by the above program:
**Entire book Garcia, Jerry:
(954) 123-4567 (cell)
(954) 111-2222 (work)
Lennon, John:
(954) 123-4567 (home)
(954) 555-1234 (work)
Simon, Paul:
(954) 333-4444 (cell)
**Jerry's entry
(954) 111-2222 (work)
(954) 123-4567 (cell)
**No entry for Mick **Paul's cell number:
(954) 333-4444 (cell) **Jerry's new entry:
(954) 111-2222 (work)
(444) 555-5555 (cell) **Entire phone book: Garcia, Jerry:
(444) 555-5555 (cell)
(954) 111-2222 (work)
Lennon, John:
(954) 123-4567 (home)
(954) 555-1234 (work)
Simon, Paul:
(954) 333-4444 (cell)
**Delete John's work number
(954) 123-4567 (home)
**Delete John's home number; should delete his entry
Â
**Delete Paul's entry
**Now we have only Jerry's entry in the whole book
Garcia, Jerry:
(444) 555-5555 (cell)
(954) 111-2222 (work)
a. Now we get to the assignment. Work on the design using scenario diagrams. Here are five scenarios, where we assume throughout that input is well-formed:
Â
1.a_phone_book responds to a add(name, phone_number, label)message where it does not yet contain a name entry.
Â
2.a_phone_book responds to a add(name, phone_number, label)message where it already contains a name entry and this entry does not contain a label contact.
Â
3.a_phone_book responds to a find(name) message where it contains a name entry.
Â
4.a_phone_book responds to a find_contact(name, label) message where it contains a name entry that contains a label contact.
Â
5.a_phone_book responds to a delete_contact(name, label)where it contains a name entry whose sole contact is a label contact (i.e., the entry should be deleted from this phone book).
Draw a scenario diagram for each of these scenarios (five scenario diagrams in total).
Â
b. Code the program in Ruby language.Problem 2Â
Â
Here is the specification for a phone book. A phone book consists of a collection of entries. An entry associates a person’s name (a string) with a set of one or more phone contacts. Every entry in a phone book has a unique name. In the description below, we use ‘name entry’ to refer to the entry named by name.Â
A phone contact consists of a phone number and a label. (These are the PhoneContact objects from Assignment 3.) For any given entry, every contact has a unique label. We use ‘label contact’ to refer to the contact labeled by label within an entry. We can look up an entry in a phone book by name and, given an entry, we can look up a contact by label.Â
Here are a phone book’s principal methods:Â
add(name, phone_number, label)Â
If self (i.e., this phone book) has a name entry, then add to this entry a new label contact with phone_number if the entry has no label contact, else replace the phone number in its label contact by phone_number. If self does not have a name entry, add a new name entry with a label contact for phone_number.Â
find(name)Â
Return an array of the contacts in name entry if this entry exists in self; otherwise return nil.Â
find_contact(name, label)Â
Return the label contact under the name entry in self. If self has no name entry or its name entry has no label contact, return nil.Â
delete(name)Â
Delete the name entry from self. If self has no name entry, do nothing.Â
delete_contact(name, label)Â
Delete the label contact from the name entry from self. If this is the only contact under this entry, also delete the entry from self. If self has no such contact, do nothing.Â
to_sÂ
Return a string representing all of the entries in self, sorted by name.Â
The behaviors assume all inputs are well-formed (e.g., name is a string, phone_number is wellformed based on PhoneNumber.new). Here’s a sample interaction:Â
book = PhoneBook.newÂ
book.add('Lennon, John', '954-555-1234', 'work') book.add('Garcia, Jerry', 9541112222, 'work') book.add('Lennon, John', '(954) 123-4567', :home) book.add('Garcia, Jerry', 9541234567, 'cell') book.add('Simon, Paul', '9543334444', 'cell')Â
Â
puts "**Entire book" puts book Â
puts "**Jerry's entry" puts book.find('Garcia, Jerry')Â
Â
if not book.find('Jagger, Mick') then   puts "**No entry for Mick" end Â
puts "**Paul's cell number:"Â
puts book.find_contact('Simon, Paul', :cell)Â
Â
book.add('Garcia, Jerry', '4445555555', :cell)Â
puts "**Jerry's new entry:" puts book.find('Garcia, Jerry')Â
Â
puts "**Entire phone book:" puts book Â
puts "**Delete John's work number" book.delete_contact('Lennon, John', 'work')Â
puts book.find('Lennon, John')Â
Â
puts "**Delete John's home number; should delete his entry"Â
book.delete_contact('Lennon, John', 'home') puts book.find('Lennon, John')Â
Â
puts "**Delete Paul's entry" book.delete('Simon, Paul')Â
Â
puts "**Now we have only Jerry's entry in the whole book" puts bookÂ
Â
Here’s the output produced by the above program:Â
**Entire book Garcia, Jerry:Â
    (954) 123-4567 (cell)Â
    (954) 111-2222 (work)Â
Lennon, John:Â
    (954) 123-4567 (home)Â
    (954) 555-1234 (work)Â
Simon, Paul:Â
    (954) 333-4444 (cell)Â
**Jerry's entryÂ
(954) 111-2222 (work)Â
(954) 123-4567 (cell)Â
**No entry for Mick **Paul's cell number:Â
(954) 333-4444 (cell) **Jerry's new entry:Â
(954) 111-2222 (work)Â
(444) 555-5555 (cell) **Entire phone book: Garcia, Jerry:Â
    (444) 555-5555 (cell)Â
    (954) 111-2222 (work)Â
Lennon, John:Â
    (954) 123-4567 (home)Â
    (954) 555-1234 (work)Â
Simon, Paul:Â
    (954) 333-4444 (cell)Â
**Delete John's work numberÂ
(954) 123-4567 (home)Â
**Delete John's home number; should delete his entryÂ
Â
**Delete Paul's entryÂ
**Now we have only Jerry's entry in the whole bookÂ
Garcia, Jerry:Â
    (444) 555-5555 (cell)Â
    (954) 111-2222 (work)Â
a. Now we get to the assignment. Work on the design using scenario diagrams. Here are five scenarios, where we assume throughout that input is well-formed:Â
Â
a_phone_book responds to a add(name, phone_number, label) message where it does not yet contain a name entry.Â
Â
a_phone_book responds to a add(name, phone_number, label) message where it already contains a name entry and this entry does not contain a label contact.Â
Â
a_phone_book responds to a find(name) message where it contains a name entry.Â
Â
a_phone_book responds to a find_contact(name, label) message where it contains a name entry that contains a label contact.Â
Â
a_phone_book responds to a delete_contact(name, label) where it contains a name entry whose sole contact is a label contact (i.e., the entry should be deleted from this phone book).Â
Draw a scenario diagram for each of these scenarios (five scenario diagrams in total).Â
Â
   b. Code the program in Ruby language.
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Â ----------- Â ----------- T-----------han-----------k Y-----------ou -----------for----------- us-----------ing----------- ou-----------r w-----------ebs-----------ite----------- an-----------d a-----------cqu-----------isi-----------tio-----------n o-----------f m-----------y p-----------ost-----------ed -----------sol-----------uti-----------on.----------- Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m o-----------nli-----------ne -----------or -----------inb-----------ox -----------me -----------a m-----------ess-----------age-----------