Write a phonecontact class that gets initialized with a


Prepare a scenario diagram for Problem 1, Brief discussion for Problem 2, and Ruby source code for Problem 3.

 

Problem 1:

Here is an interaction in a tic-tac-toe game, with user input in bold:
>> gm = Game.new('andy', 'mike')

=> #

>> gm.play_game('mike')

Mike, enter your next O move (1-9): 1
O - -
- - -
- - -

Andy, enter your next X move (1-9): 3
O - X
- - -
- - -

Mike, enter your next O move (1-9): 5
O - X
- O -
- - -

Andy, enter your next X move (1-9): 3

Bad move dude! You go again.
O - X
- O -
- - -

Andy, enter your next X move (1-9): 6
O - X
- O X
- - -

Mike, enter your next O move (1-9): 9
O - X
- O X
- - O

Mike, you won!
=> nil
>> gm.play_game('karen')

I don't know that player. Please try again. => nil

>> gm.play_game('andy')
Andy, enter your next O move (1-9): 5
- - -
- O -
- - -
When a new game is created, two players register with it by name (the arguments to Game.new). The play_game message is called with one of the player's names and starts a new game where that player plays first using the O marker. (A game can be sent repeated play_game messages so the same pair of players can play multiple games). In each iteration, the current player is prompted for the position of her move and then she enters the move number, where the nine squares are numbered in row-major order from 1 through 9. If a move is illegal (i.e., already occupied), the current player is scolded and prompted again for a move. After each move, the board is redrawn. A game ends when either one of the players forms a horizontal, vertical, or diagonal row of her three markers (in the usual way), or the board is full indicating a tie.

The assignment is not to implement this game in Ruby. Rather, the assignment is to use scenario diagrams to discover the objects, their responsibilities, and the messages they respond to. Specifically, focus on the scenario of a typical iteration: If the current game is not over, the current player is prompted for a legal move which she supplies and then the move is made and the current game board is displayed. Hand in a single scenario diagram that you believe represents your best design.

Problem 2

Here is a class definition in Ruby:
class C
attr_accessor :f

def g(x)
self.f=(x)
end

def h(x)
f = x
end

def i(x)
self.f = x
end
end

What does the following transcript teach us?

>> c = C.new
=> #
>> c.g(2)
=> 2
>> c.f
=> 2
>> c.h(5)
=> 5
>> c.f
=> 2
>> c.i(7)
=> 7
>> c.f
=> 7

Problem 3

Write a PhoneContact class that gets initialized with a phone number and a label. The phone number should follow one of the formats of the Phone class from the previous assignment (and you should use the Phone class in this assignment). A label is either a symbol or a string. A phone contact responds to the label, phone_number, and to_s messages. Note that phone_number returns a Phone object:

>> contact1 = PhoneContact.new('(954) 555-1212', :work)
=> (954) 555-1212 (work)
>> contact1.label
=> "work"
>> contact1.phone_number
=> "(954) 555-1212"
>> contact1.to_s
=> "(954) 555-1212 (work)"
>> contact2 = PhoneContact.new(' 955-555-1212', "cell")
=> (955) 555-1212 (cell)
>> contact2.phone_number.area_code
=> 955 

Solution Preview :

Prepared by a verified Expert
Programming Languages: Write a phonecontact class that gets initialized with a
Reference No:- TGS0663584

Now Priced at $40 (50% Discount)

Recommended (92%)

Rated (4.4/5)