Lab 2: First Java programs

The goal of this lab is to write, compile, and run your first Java programs. In particular, you will practice some basic operations on Strings, in addition to continuing to practice some of the algorithmic thinking we had previously been doing.

1. Set up

Make a new directory for this lab and cd into it.

mkdir lab2
cd lab2
I am providing stubs for the programs you are going to write. Copy them from the course public directory.

cp /homes/tvandrun/Public/cs235/lab2/* .

Look at the list of files now in your directory.

ls

You should see Greeting.java, StringToInt.java, and Palindrome.java.

2. Printing a greeting

This first exercise is just to step you through the process of writing, compiling, and running a Java program. Open Greeting.java in XEmacs.

xemacs Greeting.java &

Remember that the ampersand means that the terminal window will still be active even though XEmacs is running.

Add your names to the @author tag. Then add a line to the program (inside the main section) that displays a greeting to the screen. Then compile the program.

javac Greeting.java

If the compiler complains, then look carefully at your program (you probably wrote only one line, so it should be easy to fine the problem) and see if you can figure out what's wrong and try again. If the compiler does not give you any errors, then congratulations, you have compiled your first program. Look at the contents of the directory again.

ls

You should see Greeting.class. If you want to see only the source files, you can type

ls *.java

This means, "list everything that ends with '.java'."

Now run the program.

java Greeting

(Remember that when you compile, you compile a file, so you need to give javac the name of the file, Greeting.java. When you run, you run a program, and the name of the program is Greeting, not Greeting.class. The Java virtual machine knows to look for a file called Greeting.class for the compiled version of the program Greeting.)

Now that your program runs correctly, give your partner a HIGH FIVE!

3. Converting a String to an int

This one is going to be a little harder. Open StringToInt.java. (You can do this in the same XEmacs session by clicking on the "open" button. Then middle-click on the file you want to open.) Your task here is to take a String that the user inputs and convert it into an int.

I have already written the part that prompts the user for a string--it requires some stuff we haven't learned yet, so don't let it intimidate you. Just know that by the time that stuff is done, str will contain a String that the user of the program has typed in. Assume the string contains only digits, such as "142364".

Your task is to store the equivalent value in the int variable x. Here's one bit of magic that you'll need. If you have a char value---let's say, the variable c---that contains a digit, then you can turn that digit into the equivalent int---say, to store in the variable i---by

i = (int) (c - '0');

Do not change any of the lines of code I have given you (except you may delete the "put your code here" line).

You may assume the the string you are given contains only digits. Your program does not need to work right for incorrect input. (That's only because this is your first crack at programming; in principle, programs should deal with incorrect input gracefully.)

When you compile, you will almost certainly get errors. Don't be afraid to ask for help. When it compiles without errors, then test the program by running it on several inputs. Do not be surprised if it doesn't work right; go back and fix it.

When your program is working, go on to the next problem.

4. Testing for palindromes

A word, phase, or sentence is a palindrome if it is the same backwards and forwards. (For phrases and sentences, we ignore punctuation, spaces, and capitalization). Some interesting palindromes are

Open the file Palindrome.java. Your task in this problem is to test if a word entered by the user (stored in the variable str) is a palindrome. For our purposes, we will assume we're working only on single words. (Some English words that are palindromes are level, racecar, and wow). Print an appropriate message to the screen.

As before, compile and test.

5. To turn in

Sometimes you will be asked to turn in an electronic version of your program by copying the .java files that you have written or modified to a grading directory. Today you will turn in a hard copy. Here are the procedures for generating something to turn in which demonstrates you have complete the lab.

  1. Start a script file. (Remember, script somefilename will start recording everything you do in the command shell to a a file called somefilename; just script will record it to a file called typescript.)
  2. Display the source file; in this case, type cat Greeting.java.
  3. Compile (javac Greeting.java)
  4. Run the program (java Greeting)
  5. Repeat the last three steps for the other two programs; you should run those programs several times each, showing that it works for various input.
  6. Exit the script (exit)
  7. Print out the script file. Use the command a2ps instead of lpr-- this makes the file print in "two-up" form, that is, two pages per page. (a2ps typescript-- or whatever the name of your script file is)

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

Then turn in the hard copy to me.


Thomas VanDrunen
Last modified: Thu Sep 3 11:34:11 CDT 2009