Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

RUBY PROGRAMMING Here is the specification for a phone book. A phone book consists of a collection of entries. An entry associates a persons name

RUBY PROGRAMMING

Here is the specification for a phone book. A phone book consists of a collection of entries. An entry associates a persons name (a string) with a set of one or more phone contacts. The entries in a phone book are unique and serve as the primary key. Well use name entry to refer to the entry named by name.

A phone contact consists of a phone number and a label. The contacts in each entry have unique keys. We can look up an entry in the phone book by a persons name, and we can look up a contact in an entry by label.

Here are the public methods provided by the PhoneBook class:

add(name)

Adds a new empty entry under name and returns it if no such an entry exists; returns None otherwise.

add(name, phone_number, label)

Finds the name entry or creates a new one if none exists, then adds or updates a label contact under this entry; returns the entry.

delete(name)

Removes entry and returns True if the name entry exists and is empty; else returns False.

delete(name, label)

Removes contact and returns it if it exists (i.e., a label contact in the name entry); else returns nil.

has(name)

Returns True if there is an entry for name; returns False otherwise.

find(name)

Returns entry under name if it exists; else returns nil.

find(name, label)

Returns the label contact under entry name if it exists; else returns nil.

to_s

Returns a string representing all of entries, sorted by name.

The phone books entries are represented as Entry objects. Here are the Entry classs principal methods:

add_contact(phone_number, label)

Adds phone_number if the contacts label is not present; otherwise updates the number under label.

has_contact(label)

Returns True if there is a contact under label; else returns False.

delete_contact(label)

Deletes the contact under label if it exists; else returns None. empty() Returns True if empty; else returns False.

to_s

Return a string representing all the contacts sorted by label

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Find vC(t) for t > 0 in the circuit in Fig. P6.63 if vC(0) = 0.

Answered: 1 week ago