CPSC 124 Introduction to Programming Spring 2008

Lab 3: Graphics and Control Flow

Introduction

With conditionals (if statements) and loops (while/for statements), the range of programs that can be written increases dramatically. This lab gives you some practice with using these control structures in programs.

You may find that due to a bug in your program, the program has an infinite loop. In this case, the program will hang and you will not get back to the shell prompt. However, there is a command for killing errant programs. You can type <control>-c (hold the control key while typing the c key), which will kill the currently running program.

This lab will also give you some experience drawing pictures in Java using the Paint class. For information on the Paint class (which is not described in the book), click here.

Much of this lab was originally written by Professors Eck, Orr, and Bridgeman.

Setup

Create a lab03 directory for this week's lab files in your cs124 directory (~/cs124/) and change to that directory (review lab #1 if you don't remember how to do this.). Copy the provided files from the directory /classes/s08/cs124/labs/lab03 to your new lab03 directory. You can copy them one at a time, or you can use a wildcard pattern to copy everything at once:

 cp /classes/s08/cs124/labs/lab03/*.java .

"*" is a special symbol which matches anything. Using it as part of a longer pattern like /classes/s08/cs124/labs/lab03/*.java means that it will match any file whose name starts with /classes/s08/cs124/labs/lab03/ and ends with .java - i.e. all the .java files in the /classes/s08/cs124/labs/lab03/ directory.

The rest of this lab assumes you are in your lab03 directory.

Good Programming Style

Don't forget to follow the good programming style rules from lab 2. These are critical for making your program readable and understandable by others (and yourself). In particular, make sure that you indent all nested statements (e.g., within if statements and loops). You will lose points on the lab if you fail to follow these rules.

Exercises

Here are the exercises for this week's lab, due next Thursday by the beginning of lab.

Important: Before you start on the exercises, read the directions for electronic handin in the "Handin" section below. Test that you can actually hand something in successfully by handing in your current lab03 folder (which currently contains only provided code for this lab). If you have problems, let me know right away. Not being able to hand something in because you didn't test it ahead of time does not excuse a late submission.

  1. Write a complete program called ComputeOrder.java that reads in three numbers and prints out their order. You can assume that the user enters in three valid integers, which are all distinct. Be sure to comment your code. A sample output is shown below, with user responses in bold:

    Enter number 1: 3
    Enter number 2: 10
    Enter number 3: 4
    The order of the numbers is 3 4 10.
    
  2. Write a complete program called OddOrEven.java which first prompts a user for one number (call it n). Then, for 0 to n-1, the program prints out whether each number is even or odd. The program should end by printing the sum of the even numbers and the sum of the odd numbers. There should be a newline at the end of the output, so the Konsole prompt appears on a line by itself when the program completes. A sample output is shown below, with user responses in bold:

    Enter a number: 10
    0 is even.
    1 is odd.
    2 is even.
    3 is odd.
    4 is even.
    5 is odd.
    6 is even.
    7 is odd.
    8 is even.
    9 is odd.
    The sum of the even number is 20.
    The sum of the odd numbers is 25.
    
  3. Write a complete program called ComputeMultiples.java which prompts a user for two numbers (call them num and numMultiples) and then prints out multiples of num. If the user enters 3 for num and 23 for numMultiples, then your program should print the first 23 multiples of 3. In addition, your program should print only 8 multiples per line (as shown below). There should also be a newline at the end of the output, so the Konsole prompt appears on a line by itself when the program completes. You will lose points if you do not follow these formatting requirements. A sample output is shown below, with user responses in bold:

    Enter a number: 3
    How many multiples? 23
    3 6 9 12 15 18 21 24 
    27 30 33 36 39 42 45 48 
    51 54 57 60 63 66 69 
    

    Hint: start by writing a program which prints out each multiple on one line, then (once that's working) consider printing 8 numbers per line.

  4. There are lots of small details in Java and sometimes you may have a question about whether or not something will work. A great way to get the answer is to try it out. In this part, you'll write a small tester program called Test.java to answer the following set of questions:

    For your tester program, you only need to write enough code to convince yourself of the answers to the question. You'll need to use the getlnBoolean subroutine, and you'll probably want to produce some output. Use an infinite loop (occasionally, an infinite loop can be a useful thing) to repeatedly prompt the user, read in a boolean, and print out the inputted boolean. Run the program and try entering various values (e.g., 1) to see what happens.

    Because the program uses an infinite loop, the program will not terminate by itself. Use the command <control>-c (hold the control key down while typing the c key) to kill the program. Note: this command will work for other errant programs as well (even non-Java programs).

    Put your results in a comment at the top of the program. For this program (and only this program), since it is a tester program, you do not have to add lots of comments (just include the top comment with your name, assignment, exercise, and results) or make the program user friendly. This is because the purpose of the tester program is only to try something out, and it generally isn't something you would keep around once you found the answer. (But keep this one around so you can turn it in!).

  5. Write a program called MyPicture.java that displays an interesting picture. Your program should use Paint.java (provided in /classes/s07/cs124/labs/lab03/) as described in class.

    In MyPicture, you will build a new window (using Paint.buildWindow()) and draw some picture. You might try drawing a picture of your favorite cartoon character or a tropical fish or a fancy snowman, or perhaps you feel ambitious and want to tackle an optical illusion. Maybe you want to draw something else entirely... The choice is up to you.

    At a bare minimum, you must use at least two different drawing colors and draw at least five different shapes. Sign your picture by displaying your name somewhere on the picture (using drawString). Your picture must be something recognizable or have some interesting principle behind its generation - simply putting a couple of shapes randomly on the screen is not going to earn you full credit, even if you meet the minimum requirements. Part of your score will be based on creating a nice or interesting picture. Extra credit is possible for a particularly complex or nifty picture, although you should finish the rest of the exercises before worrying about extra credit.

Handin

Verify that your lab03 folder contains all of the files you created or modified for this lab, then copy your entire lab03 folder to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username). To copy a whole directory instead of a single file or a collection of files, use

  cp -r sourcedir targetdir

where sourcedir is the directory you want to copy and targetdir is where it will be copied to. The "-r" means "recursive" and tells the system to copy the source directory, any files it contains, any subdirectories it contains, any files those subdirectories contain, any subdirectories the subdirectories contain, etc. In this case, the following command will work regardless of what directory you are currently in:

  cp -r ~/cs124/lab03 ~mcorliss/handin/cs124/username

Of course, if you named your cs124 directory something other than "cs124", you'll need to substitute your directory's name as appropriate. You can use the ls command (or dir) to check that the directory ~mcorliss/handin/cs124/username contains a lab03 directory, and that the directory ~mcorliss/handin/cs124/username/lab03 contains copies of the files you wanted to hand in.

You can hand things in multiple times - the cp command will overwrite any existing file with the same name.

You can also delete things if you hand in something you didn't want to. Remove files using the command:

  rm filename

where filename is replaced by the name of the file you want to remove (e.g. ~mcorliss/handin/cs124/username/lab03/TempConvert.java to remove the file TempConvert.java from your handin directory). Note: including the full path here is important - if you are in your own lab03 directory and type rm TempConvert.java, you'll delete your copy of the TempConvert.java and will have to redo exercise #1 from scratch.

Another very important note: rm deletes things for real so be careful when you use it - a deleted file is gone for good. (If the file had been in existence for a while before you deleted it, you may be able to recover a version from a system backup. You will lose any changes made to the file since the last backup, however.)

You can delete a directory with the command

  rmdir dirname

Note that the directory must be empty of all files before it can be removed.

Finally, you can delete files and directories all together with

  rm -r dirname

As with cp, the "-r" means recursive and means that it will delete the directory, all files it contains, all subdirectories it contains, all files contained in the subdirectories, all subdirectories contained in the subdirectories, etc. rm -r is a very powerful command and should be used with extreme caution.