CPSC 124 Introduction to Programming Spring 2007

Lab 2: Expressions, Strings, and TextIO

Introduction

In this week's lab, you'll modify and create some Java programs that work with various types of expressions and Strings. You'll also be using the non-standard class TextIO to read data typed in by the user. Much of this lab was originally written by Professors Orr and Bridgeman.

Setup

Create a lab02 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/lab02 to your new lab02 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/lab02/*.java .

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

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

Good Programming Style

Programs are written for people just as much as they are written for computers - by far the longest part of a program's lifecycle is the maintenance phase, where programmers may go back for years to fix bugs and add features. It is thus important to make sure they can understand the program.

Any program that you turn in for this course should obey the rules of good programming style. You will lose points for poor style, even if your program works correctly. The following style rules are already relevant:

Additional rules will be added throughout the course.

Note: Don't use comments to explain how Java works. Assume that anyone reading a Java source code file knows Java. A comment such as "declare a variable named interestRate" is useless. However, the comment "annual interest rate, expressed in decimal form" gives information that makes the program easier to understand.

TextIO

One of the files in the lab2 directory is TextIO.java. As discussed in Section 2.4, this file defines some subroutines that you can use for reading data typed by the user into your program. You don't have to read this file or understand the Java code in it, though you are welcome to look at it if you wish. All you need to know is the names of the subroutines and what they do (which you can find out from the book, or by looking at the file).

In fact, you don't really need the source code file, TextIO.java, at all - what you really need is the compiled class file. Once you've compiled TextIO.java, you could actually delete the .java file because you aren't going to make any changes to TextIO (which is why you'd need the .java file once it has been compiled).

Note: TextIO.class is not a standalone program. You can't run it (it has no main() subroutine). It is merely a "helper class" that contains variables and subroutines that can be used by other programs. Also keep in mind that the TextIO.class file must be in the same working directory as any program that uses it.

Another note: use only the methods in TextIO that start with getln (e.g., getlnInt(), getlnBoolean(), getlnDouble(), getln(), etc.) and not the methods that start with get (e.g., getInt(), getBoolean(), getDouble(), get(), etc.). The latter class of methods is for reading multiple inputs on a single line, something we won't need to do in this class. If you forget the ln in getln, it can lead to some strange errors, so make sure your method call always starts with: TextIO.getln.

Exercises

Here are the exercises for this week's lab, due next Thursday (February 1st) 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 lab02 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. One of the files you copied to your lab02 directory is the TempConvert.java file. This program converts a temperature in Fahrenheit to a temperature in Celsius. You will modify this program as described below. You should make the changes one at a time and test the program to be sure it works after each change - this is known as incremental development, and is a very good strategy. If you find that the program doesn't work at some point, it is likely because of some change you made since the last time you tested it. If you've only made a few changes, there are only a few places to start looking for the problem.

    Don't forget to update the comments to reflect the changes you made, and be sure to add your name, the assignment name, and the exercise number.

  2. Another file you copied to your lab02 directory is the StringTest.java file. This simple program asks a user to enter a string of uppercase and lowercase letters and then prints it back to the string. You will modify this program as described below. As described in the previous exercise, you should use incremental development.

    Don't forget to update the comments to reflect the changes you made, and be sure to add your name, the assignment name, and the exercise number, as well as the answer to the last part. Hint: for a list of some useful String methods, look at page 33 of the textbook.

  3. Write a complete program. You should pick an appropriate program name (and hence filename).

    The program should hold a short conversation with the user. It should ask the user for information and should use that information in its responses. Here is a sample conversation, with the user's input shown with bold:

             Hi, my name is HAL.  What's your name?
             Dave
             Hi Dave. Nice to meet you.
             How many years old are you, Dave?
             35
             Wow, 35! You must have been born in 1969.
             What's that in your hand, Dave?
               .
               .
               .
    

    Remember that your program is essentially a script for the conversation. You are welcome to be as serious or absurd as you want to be.

    You should have at least five questions and answers, which means you may need as many as five variables. At least one user answer should be used in more than one of the computer's responses (like the name "Dave" is in the sample). The program should ask for at least one number, and it should do some calculation with that number (the sample uses the age to compute the year in which the user was born). You should also use at least one subroutine from the Math class (page 31 of the textbook) and at least one from the String class (page 33 of the textbook). You can use ones discussed in class and in the textbook, or you can use the Java API documentation to find other subroutines to use.

    Don't forget good programming style.

  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 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. You may need to modify your source and recompile several times in order to thoroughly answer the questions. You don't need to worry about a lot of comments (just include one with your name, the assignment, and the exercise) or even user-friendlyness for that matter - the purpose of a tester program is only to try something out, and it generally isn't something you'd keep around once you found the answer. (But keep this one around so you can turn it in!) Also, put your answer to these questions in a comment at the top of the program.

  5. Write another tester program to answer the following set of questions.

    State what the results are and explain them as best you can. (Hint: think about the type of each value.) Can you figure out how to fix the things that don't do what you want them do?

Handin

Verify that your lab02 folder contains all of the files you created or modified for this lab, then copy your entire lab02 folder to the handin directory ~mcorliss/handin/124/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/lab02 ~mcorliss/handin/124/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/124/username contains a lab02 directory, and that the directory ~mcorliss/handin/124/username/lab02 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/124/username/lab02/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 lab02 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.