Assignment 4: Various loop problems

The goal of this Assignment is to practice the flow of control constructs we have seen in class, particularly loops. There are two parts. The first part should take you about the length of the class period. The second part may require coming back later to finish. In total there will be four files (or programs) that you will write.

1. Setup

As in previous assignments, move into your cs241 directory. Make a directory for this assignment, and change into it.

> cd cs241
> mkdir assign4
> cd assign4

PART I: COMPARING LOOPS

2. Using if and do/while

All of Part I will address the same problem:

Allow the user to input a String. Then print the characters in that String in reverse order.

First we'll implement this using a do-while. Here's a first try:

Open a new file in Xemacs called Assign4DoWhile.java and implement this. Don't forget about documentation-- both the header and each variable declaration. Remember that to read in a line from the user, use DocsIO.readString(...).

Compile, and when you have fixed the compiler errors, try running it on the following inputs: java, Wheaton, and racecar. If it's working right, you should notice that "racecar" backwards is still "racecar."

Now run it, but give it an empty string as input-- just hit Enter without any text. What happens?

The error occurs because we've tried to look at a position in the String that doesn't exist-- input is empty, so on the first iteration, when position is zero, input.charAt(position) causes the program to crash.

What you need to do is check before doing the loop if input is empty. Figure out what you need to do-- I recommend writing the algorithm out by hand first-- and then change your program so that it works right even with empty strings. Hint: You need to add an if statement and put the loop inside the branch. When your new version is working right (make sure you test the old test cases again too, to make sure that they still work after you've made the change), you may go on.

3. Using while

We can do without the if statement if we use a while loop. Here's the the algorithm:

Open a new file called Assign4While.java and implement this (as always, documenting). To save typing, you could start in Assign4DoWhile, "save as" Assign4While, and simply make changes (don't forget to change "Assign4While" in the opening part of the program and in the header documentation.). Compile and test until you are sure it is working, including on empty strings.

4. Using for

Now, do the same thing, in a file called Assign4For.java, except this time use a for loop. No algorithm provided this time--- figure it out on your own; however, there's no conceptual change from the while algorithm, just a rearrangement of how it is expressed. Use all three parts of the for header. Document, test.

PART II: BIGGER PROJECT

5. A menu-drive temperature converter.

Now for a bigger project. You are to implement a variation on the temperature conversion program, now with this specification:

The program should average a series of temperatures given by the user, in either Fahrenheit or Celsius. The program should repeatedly display the average so far, display a menu, and prompt the user. The menu should have three choices: (1) enter a new temperature, (2) change the mode (between Fahrenheit and Celsius), and (3) quit.

If the user selects (1), the program should prompt, inputing a new temperature, and recomputing the average. If the user selects (2), the program should convert the average from Fahrenheit to Celsius (if it is currently in Fahrenheit mode) or vice verse (in the other case). If the user selects (3), the program should end.

Default mode is Fahrenheit.

The temperatures should be input as ints, but the average should be a double. If the user selects something not on the menu, the program should say that it is an invalid menu choice.

Here's a sample run of such a program:

ar1121: {17} java TempAverage
No average yet.
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 1
New temperature--> 14
Average temp: 14.0 F
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 1
New temperature--> 32
Average temp: 23.0 F
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 2
Average temp: -5.0 C
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 1
New temperature--> 100
Average temp: 30.0 C
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 8
Invalid option.
Average temp: 30.0 C
Please choose:
        1. Enter another temperature
        2. Change mode
        3. Quit
Your choice--> 3

Hints: You may find it easier to keep track of the total of the temperatures entered and recompute the average that way. A boolean variable may come in handy for keeping track of the mode.

Follow the software development process and conform to documentation standards.

6. Turn in

Create the script file as before (make sure you cat, compile, and run for each of the four files), but this time print it out. You can print it on the lab printer using

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

Make sure you have exited the script file before you print, or else it will print forever!

Then turn in the hard copy in class on Friday or put it in my box by 5:00 pm.

DUE: Friday, Feb 4, at 5:00 PM.


Thomas VanDrunen
Last modified: Thu Feb 3 15:15:10 CST 2005