CS124, Fall 2001
Lab 3: Loops and Branches
In this lab, you will use if statements and while statements for the first time, in Exercises 1 and 2. Once you start using control structures like if statements and while loops, there is one more programming style rule that you have to follow:
- The statements inside a control structure such as an if statement or a while loop should be indented an additional three or four spaces, so that it is easy to see that they are nested inside the control structure. If you have a block of statements, with a "}" at the end, then the "}" should be on a line by itself and should not be indented.
The files that you will need for this lab are in the directory /home/cs124/lab3. (They are also linked to this page so that you can read them on-line if you want.) To begin the lab, copy the files into your account with the GUI or using the command:
cp -r /home/cs124/lab3 .
Don't forget the period at the end. Once you have copied the directory, enter the command "cd lab3". Then the directory that you have copied will be your current directory. You can use the ls or dir command to see the list of files in this directory. The rest of this lab assumes that you are working in your copy of the lab3 directory!
For Exercises 1 and 2, make a print out of your programs using the a2ps command. Don't forget to follow all the rules of good programming style, as given on this lab and on the previous lab. Exercise 3 is a writing assignment that you will not do at the computer. You can type your answer or write it out by hand. I remind you that your are allowed to work with someone in lab and turn in a combined lab report. Turn in all your lab work in class next Wednesday, September 19.
Exercise 1: Branching Conversation
An if statement allows your program to take one of two alternative courses of action, depending on whether some condition is true or false. For exercise 1, you should improve your conversation program from the previous lab so that it uses at least two if statements. After reading in the user's response to some question, you can test the user's response in an if statement, so that your reply will be based on the user's input. For example, you might the user his age and then use an if statement to check "if (age > 30)". If the condition is true, you could say, "Sorry, I don't trust anyone over 30." If the condition is false, you could give some other response. Another way to use an if statement is to generate a random number and base the program's action on that.
You probably want to copy your old conversation program into your lab3 directory, so that you can work on it there and still keep the lab2 version. For example: if your username is jsmith and the name of your program is Converse.java, you could do this with the command
cp /home/jsmith/lab2/Converse.java /home/jsmith/lab3
Of course, you could also use the GUI to copy the file by dragging it from one directory window to another. Or, if you are working in your lab3 directory, you could replace the second parameter on the above command line with a period.
Exercise 2: Quote of the Day
No matter how well you write your conversation program for Exercise 1, it's probably still pretty boring talking to a computer. A better use for computers is to facilitate interaction between people. Network communication is relatively easy in Java, compared to other programming languages, but it is still beyond your capabilities at this point. Fortunately, there is a solution for this: Use a class that someone else has written, just as you use the TextIO class for reading data from the user. I have written a class named Network that contains a few subroutines that can be used for communication between two computers on the Internet. Only a simple form of communication is provided -- but it will be enough for this exercise!
In addition to Network.java itself, I have written two small programs that use the Network class: NetServer.java and NetClient.java. The first thing you should do is try out these programs. For this, you will have to cooperate with one of the other students in the lab. One of you will run NetServer and the other will run NetClient. (Alternatively, you can open two command line windows and run one program in each window.) Make sure that you are working in your lab3 directory, which already has the compiled class files that you need. Here is what you have to do:
- First, someone should run the "server" with the command "java NetServer". It will ask for a "port number". This can be any number between 2049 and 65535. (If someone else is already using the port number that you select, you'll get an error.) After you enter a port number, the computer will "listen" for a connection from a "client".
- Now, the other person should run the "client" with the command "java NetClient". You will need to know the name of the computer and the port number on which the server is listening. Get this information from the person who is running the server. The computer name is the name of the Linux computer on which that person is working: cslab0 or cslab1 or .... After the program gets the computer name and port number from you, it will open the connection.
- After the connection has been opened, the person running the client gets to type in a line of text that will be transmitted to the server.
- After the server gets the message from the client, the person using the server gets to type in a line of text that will be transmitted to the client.
- After one message has been sent in each direction, both programs end.
Note that a "server" is just a program that listens for a connection request on a given port, and a "client" is a program that makes a request to connect to the server.
The Network class makes the following subroutines available:
- Network.listen(port) -- The program acts as a server and listens for a connection on the specified port. The port should be an integer between 2049 and 65535. (Lower port numbers are reserved for standard network services.)
- Network.connect(host,port) -- The program acts as a client and makes a connection request to a server. The first parameter, host, is a string that gives the Internet name or the IP number of the computer on which the server is running. The second parameter is an int that specifies the port number on which the server is listening.
- Network.close() -- Closes the connection (if one is open). After the connection has been closed, no more data can be sent over the connection. After closing one connection, it's OK to open another connection by calling Network.listen() or Network.connect() again.
- Network.isOpen() -- This is a boolean-valued function, whose value is true if a connection is currently open. You could use this, for example, as the condition in a while loop: while (Network.isOpen()) ...
- Network.send(message) -- Transmit the message over the network to the computer on the other end of the connection. The message is of type String. An error will occur if no connection is open.
- Network.receive() -- This is a function that returns a value of type String. When you call Network.receive(), the computer will wait until it receives a transmission or until the connection has been closed from the other end. If some text has been received, it will be returned as the value of the function. After calling Network.receive(), you can check whether the connection has been closed by calling Network.isOpen(). An error will occur if you call Network.receive() when no connection is open.
For more information, you can look at the comments in the source code file, Network.java. It would also be a good idea to read the source code for NetServer and NetClient, since you can use parts of those programs in the programs you have to write for this exercise.
For exercise 2 on the lab, you will write a network server program and a network client program. Both programs will use while loops. This is actually not all that hard, so don't panic.
The server program is a "quote of the day" server. The server should wait for a connection (by calling Network.listen()). When a connection is made, it should transmit several lines of text to the client (by calling Network.send() several times.) The lines should be some famous or otherwise interesting quotation. (The quotation will be "hard-coded" into the program. Presumably, you would rewrite the program every day to provide a different quote-of-the-day.) After processing one connection, the server should listen for another connection, and it should continue to repeat this indefinitely. You can write a loop that will continue indefinitely by saying "while (true) {....}". When you run the program, you will have to stop it by typing CONTROL-C. Here is some pseudocode for the server program:
Get a port number from the user while (true): Listen for a connection Transmit the lines of the quotation Close the connectionThe client program will access the server to get its quotation, and it will display the quotation to the user. It should then end. There is no reason for it to repeat indefinitely. Here is some pseudocode for the client program:
Get the host name and port number from the user Make a connection to the specified host and port As long as the connection is open: Receive a line of text from the network Display the line of text to the user.I will run a quote server program during the lab on the computer named eck and on port number 5000. That way, you can write the client program first and get it working before you start working on the server.
Fun stuff: The command-line program fortune will print out a quotation selected at random from a large database of quotations.
More fun stuff: The KBattleship program, in the K menu under games, can be used to play a game of battleship over a network. The program can run either as a server or as a client. The person who wants to run the server should select the "Start Server" command from the "Game" menu. Then the client should select "Connect to Server" in the "Game" menu of his version of the program. The server, of course, will need a port number, and the client will need the name of the server computer and the port number on which it is listening. Once the connection is made, just follow the instructions at the bottom of the KBattleship window. It's possible for the players to "chat" while the game is going on. Just enter a message in the box next to the "Send" button and press return or click "Send". (The tetris game, Sirtet, is also supposed to support networked games, but it doesn't seem to work.)
Exercise 3: Algorithm development
Even though the programs that you can write at this point on pretty simple, you can still dream about writing more complex programs. In fact, you can do more than dream. You can write pseudocode algorithms for them. You might not be able to fill in all the details in the algorithm, but you can get a start.
Let's say that you want a program that will let the user play a game against the computer. Choose any non-trivial two-player game that you are familiar with, such as checkers, chess, go, or poker. There is no way that you could actually write the program, using what you have learned so far in the class. Write a pseudo-code algorithm for the program. It should be at least twelve lines long. It will leave out a lot of detail, but it should be clear from reading the algorithm that you have the outline of a program that will do the job.
David Eck (eck@hws.edu), 13 September 2001