6.7

Week 3 Set c

home work!

Programming Language BSL

Due Date Fri at 9pm (Week 3)

Purpose To use your design skills to develop a program that communicates with people and other programs. Yes, you can do this now.

Finger Exercises

Exercise 1 From HtDP, 83 and 84.

Exercise 2 Design the function vec*k. It consumes a Vec and a number. It produces a Vec like the given one by multiplying the x coordinate of the Vec with the given number.

(define-struct vec2 (x y))
(define-struct vec3 (x y z))
(define-struct vec4 (x y z t))
; A Vec is one of:
;  (make-vec2 Number Number)
;  (make-vec3 Number Number Number)
;  (make-vec4 Number Number Number Number)
; interpretation A Vec represents mathematical vectors of
; dimensions 2, 3, and 4, respectively
Hint You don’t actually have to know what mathematical vectors are or why mathematicians care about them. You just have to understand that the x field represents the x coordinate.

Graded Exercise

Exercise 3 Design the world program client. The program communicates with a chat server and displays the conversation in this chat room.

The program must be prepared to receive messages from the server. All messages from the server are plain strings. When such a string arrives, it is added as the most recent element to the conversation.

The program also interacts with the user, who may edit one-line text fields:
  • Its key handler accepts keyboard characters of length 1—except for "\r" (enter) and "\b" (del)—and adds them to the end of the text.

  • When the user presses the so-called backspace (or delete) key, the program must erase the last 1String that was added (if any).

  • When the user presses enter aka return ("\r"), the program sends the current string in the text field to the server and clears the text field.

Feel free to equip your program with additional edit capabilities. Also, your program may display its state in any way you like; the only constraint is that the canvas should contain the last five received messages (at least).

Hints This choice of representation violates the model-view design principle espoused on the preface. No worries, you will be able to improve on this solution soon. (1) Clearly the program must track two different pieces of information: the text that the user edits and the conversation itself, that is, the texts that the program has received. (2) The simplest editor representation is a string; consider alternatives. (3) Represent the conversation with an image. (4) See the notes on Client Programs.

Note The messages from the server have the shape

"name: content"

For example, "ben: hello" and "alan+nada: world" are legitimate messages.

You can block a user if you dislike her messages or the way he spells his name and so on. Simply type in the text

"-name"

and name will no longer be able to send you messages. For example, the text message "-ben" will block Ben from sending you messages.

Grading We will grade the design of each handler separately. So turn in your program even if it remains incomplete and cannot connect to the chat server.