CPSC 124 Introduction to Programming Fall 2006

Lab 3: Conditionals and Loops

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.

Setup

Create a lab03 directory in your cs124 directory to hold the files for this lab. Copy the provided files from the directory /classes/f06/cs124/labs/lab03 to your new 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. Remember to indent statements contained in 'if' statements and 'while' loops.

  1. The temperature conversion program from lab #2 only allowed users to input degrees in Fahrenheit. Modify TempConvert.java (provided in /classes/f06/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.

  2. 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.
    
  3. 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. Your program should use a for loop (in the other exercises, you are free to use whatever loop you want). 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: 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.
    
  4. 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.

  5. Write a complete program called BreakText.java that reads one line of input text and breaks it up by spaces. For example, if the user enters the text:

    He is working in the computer lab
    

    then the output of the program should be:

    He
    is
    working
    in
    the
    computer
    lab
    

    Note: your program only needs to break the text up by spaces and not by any other special characters (like apostrophes or semi-colons).

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