← Back to Week 2 Hub

CellPhone Service 2

Add behavior to your CellPhone — dialing and a static display method

Workbook 2a, p.46-47 — Exercise 2

What You're Building

Expand your CellPhone class from the previous exercise. Add a dial() method that takes a phone number and prints a message saying the owner is calling that number. Create a second CellPhone object. Add a static display() method that takes a CellPhone as a parameter and prints all its details. Display both phones, then have each phone dial the other.

Example Run

Run 1

Phone 1: Serial Number: 12345 Model: iPhone 15 Carrier: Verizon Phone Number: 555-123-4567 Owner: Dana Phone 2: Serial Number: 98765 Model: Galaxy S24 Carrier: T-Mobile Phone Number: 855-555-2222 Owner: Marcus Dana's phone is calling 855-555-2222 Marcus's phone is calling 555-123-4567

Run 2

Phone 1: Serial Number: 11111 Model: Pixel 9 Carrier: AT&T Phone Number: 444-111-3333 Owner: Sarah Phone 2: Serial Number: 22222 Model: iPhone 16 Carrier: Verizon Phone Number: 444-222-5555 Owner: James Sarah's phone is calling 444-222-5555 James's phone is calling 444-111-3333
Key detail: The display() method is static — it belongs to the class, not to a specific phone object. It receives a CellPhone as a parameter and prints that phone's information.
Concepts You'll Use

This exercise reinforces these concepts from Week 2:

Flow Diagram
Step 1 Add dial() method Takes a phone number
Step 2 Create 2nd phone Set its properties
Step 3 Add static display() Takes a CellPhone param
Step 4 Display both phones Call display() twice
Step 5 Phone 1 dials Phone 2 Using Phone 2's number
Step 6 Phone 2 dials Phone 1 Using Phone 1's number

The dial() method is an instance method (called on an object), but display() is a static method (called on the class). Know the difference.

← CellPhone Service CellPhone Service 3 →