CPSC 124 Introduction to Programming Spring 2007

Lab 3: Graphics and Control Flow

Introduction

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

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.

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/s07/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/s07/cs124/labs/lab03/*.java .

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

Important: this should only be done one time, otherwise, you will overwrite the files that are already there.

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

Exercises

Here are the exercises for this week's lab, due next Thursday. You should continue to follow the good programming style rules from lab #2. In particular, remember to put a comment at the top of the program that describes what the program does, gives your name, and gives the lab number. You should also comment your code and use good variable names as described in lab #2. Finally, make sure you indent statements contained in 'if' statements and 'while' loops. You will lose points on the lab if you don't follow these rules.

  1. 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). 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.

  2. The temperature conversion program from lab #2 only allowed users to input degrees in Fahrenheit. Modify TempConvert.java (provided in /classes/s07/cs124/labs/lab03/) so that it asks the user what unit the degrees are in, which you can limit to Fahrenheit and Celsius. If it is a Fahrenheit temperature, your program should convert it to Celsius, Kelvin, and Réaumur. If it is a Celsius temperature, your program should convert it to Fahrenheit, Kelvin, and Réaumur. Each converted temperature should be displayed. Note that to convert a Celsius temperature to Fahrenheit, you multiply the Celsius temperature by 9/5 and then add 32 to it.

    There are several ways you can handle having the user specify the input and output units - any solution that accomplishes the task is acceptable.

    Be sure to update any relevant comments.

  3. 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.
    
  4. 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.
    
  5. Write a complete program called ComputeMultiples.java which prompts a user for two numbers (call them x and n) and then prints out the first n multiples of x, with 8 numbers per line. 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. 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 the first n multiples, then (once that's working) consider printing 8 numbers per line.

Handin

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/124/username (where username is replaced with your username). For example, if you're working directory is ~/cs124/lab03/ then you could do the following:

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

where username is replaced with your particular username (e.g., mcorliss).