Programming Assignment #1 -- The Simplest Phone System, PS1
This assignment, due Monday, July 13, is to work from the English language description of PS1 below, as well as the discussions in class, to produce your own working version of PS1.
You should explain how you went about doing the tasks and what the results were. You can use my code-form.cp file as a starting point for your work. You should also use LL extensively and your reading in M, chapters 7 and 8, for general guidance. As I said earlier, try to use a variety of the constructs that are explained early on in LL so you can master them and see their applicability. Please try to go beyond the limited set of constructs that are demonstrated in code-form.cp. Remember to paste in your test printouts at the end of your source file, so it makes one self-contained package.
Once you get the basic system working, as described below, you can do your own variations on the design to your heart's content. But please do all these variations in a separate project (just copy and rename your first one or create a new one and paste in any code you want to reuse).
Description of the system
Overview: Two phones are connected by a single link. The link has two unidirectional channels, A and B. A phone sends a message by composing a packet containing the message, e.g., a string, and places the packet in the appropriate channel of the link. The other phone receives the message by removing the packet from the link. It can then reply, etc. The test functions will construct and attach the phones and link and then "operate" the phones, and print out the status of the different objects at various stages in the process. PS1 will not use class inheritance -- that will come later, e.g., a class node with subclasses phone and switch.
You should include string from the standard library (STL) and use strings exclusively (not the old-style C strings), LL 3.4.2.
Define classes phone, link, and packet.
Define any output functions you want, or use cout << notation.
Common to all classes: It is useful for each object to have an id (an int), which is a unique identifier, assigned in sequence for each new object in a class. This is helpful for development and can be used internally by the system when it is useful to do so. Each class should have its own print function, which only needs to work correctly once everything is hooked up (it can be awkward to print internal slots that have not yet been bound to anything). All the classes typically have constructors with arguments.
A note on static strings: In phone, I included a number of static slots that are strings, such as one called message that has as its value the string "message" (the only one needed for PS1). Then, instead of placing the string "message" in a packet, I place the value of message. The reason this is useful, as I said in class, is that the compiler will catch misspellings of the slot message but not of a string "message" that you might type in. Having these strings everywhere makes everything a lot easier to read during development and testing. In a real system they would probably be replaced by simple one-byte codes.
The phone class:
For phone, the primary external functionality consists of the functions say, which takes a string argument, and listen, which returns a string. Internally, for say, the phone must create a packet and insert the packet in the link. For listen, the phone must retrieve the packet and pull the string out of the packet and return it as the value of the function. It is useful to create additional methods for phone that are only used internally, to avoid having say and listen do everything. For example, placing a packet in a link is a capability that will be needed later by operations such as dial and hangup, so a method could be written for that. Useful slots include one for the link and another one for a packet, when the phone is creating or receiving a packet. The phone can have a phone number, an int, for later use in dialing and billing.
The link class:
A link can hold two packets (one for each direction). A link needs slots to hold its packets, as well as slots that contain (point to) the two phones it's connected to. I called mine source_a and source_b which told me which phone was the source (input) to channel a and which to channel b. A function is needed that will insert a link between two phones. You'll need to decide whether you want the insert and retrieve functions for manipulating packets in links associated with the phone or the link -- we can discuss this in class.
The packet class:
A packet, in this design, needs three things: A content string (for the "voice" message), a control string, for things such as "dial", "connect", "busy", "message", and "hangup" and "disconnected", and an integer for additional control information, such as the number dialed or the amount deposited in a pay phone.