Assignment 2: A first program

The goal of this assignment is to write, compile, and test your first Java program, trying out the concepts we have learned in class.

1. Setup

In the shell window, move into your cs241 directory. Make a directory for this assignment, and change into it.

> cd cs241
> mkdir assign2
> cd assign2

Start Xemacs, giving as an argument the file name "FirstProgram.java". This file doesn't exist yet, but Xemacs will realize that and give you a blank file. Don't forget the ampersand--- you will want to keep using the shell.

> xemacs FirstProgram.java &

For working on an assignment like this, I recommend having three windows open at the same time-- one browser (in which you are reading this), one Xemacs window (in which you'll write your program), and one shell (in which you'll compile and test your program). If your screen gets too crowded, move a window or two to a different part of the desktop.

Some people like using Xemacs's color mode. It will highlight different parts of the text of a program using different colors. Select "Options->Syntax Highlighting->Colors" in the Xemacs menu bar to try it out. Also, you can make your programs look nice by hitting "tab" before each line. Xemacs will always advance the cursor to a place that makes sense in the structure of your program.

2. Writing a simple hello program

Type (or copy and paste--- selecting text with your mouse will automatically copy it, and clicking the middle mouse button in the place where you want it to go will paste it) the following program stub into your Xemacs window:

/**
 * FirstProgram.java
 * 
 * This is a simple experimental program for assignment 2.
 * 
 * @author Your Name
 * Wheaton College, CS 241, Spring 2005
 * Assignment 2
 * Jan 19, 2005
 */

public class FirstProgram {
     public static void main(String[] args) {

     }
}

The stuff at the beginning is a comment. The compiler ignores everything between a /* and a */. You can use them for explanations of your program. In future assignments, there will be strict rules about how to make appropriate comments. This time, just fill in your name where it says Your Name.

Now, in the middle, as you have seen in class, write a statement that will print a greeting to the screen, using System.out.println. Then save the file (using the "Save" button at the top of the Xemacs window).

3. Compiling and running your program

In the shell window, compile the program you wrote:

> javac FirstProgram.java

If the compiler reports errors, use them to figure out what's wrong. Check for misspellings, missing quotes or parenthesis, missing semicolon... After correcting it, save and try compiling again. Once you can compile with no errors, congratulations! Now, try running the program.

> java FirstProgram

Hopefully, the program printed to the screen what you expected.

4. Using Strings

Now try something with variables. This will be like the game Madlibs. Suppose we have a sentence like

"______________," he said as he __________ another _____ times.

Declare three variables--- two Strings and one int. Assign values to them so the first string is an exclamation and the second string is a verb in past tense. Then print out the sentence above using the variables to fill in the blank. Remember to use escape sequences to make the quotation marks. (Add all this to your program so that it follows what you wrote earlier.)

Save, compile, and run, until you have it working right.

5. Arithmetic and type casting

Addition

Add the following to your program:

   int x = 5,
        y = 6;
   double a = 5,
        b = 6;
   System.out.println(x + y);
   System.out.println((double) x + y);
   System.out.println(a + b);
   System.out.println((int) a + b);
   System.out.println((int) a + (int) b);

Predict what the output will be. Then compile and run, and compare the results with your predictions.

Division

Add the following assignments (the variables should still be declared from last time).

    x = 917;
    y = 12;

Add two print statements--- one displaying the real number value of 917 divided by 12 and one displaying the integer quotient and the remainder. You may store the results to variables first if that makes it easier for you. Compile and run.

6. Strings

Add the following variable declaration.

    String str = "  \n   Wheaton Thunder    ";

Add a print statement that displays this string; then add another one that gets rid of the extra white space and turns it into all capitals. Compile and run.

Now reassign the variable str:

    str = "antidisestablishmentarianism";

Now print out the middle third of this string. Don't just count numbers manually-- calculate the parameters of the middle third using length and arithmetic in the program. Hint: it should be "tablishme".

7. Turn in

To turn in assignments, you need to produce a document that shows your program, proves that it compiles, and demonstrates that it works. You do this using the script command, which you read about at the end of the previous lab. It creates a file to which everything that appears in a shell is echoed. Here is how it works:

  1. When you are all finished and are confident that your program fulfills all the instructions here and is working right, start the script program, giving it a file name like "assign2script".
       > script assign2script
    
  2. Remove the ".class" file. That's what the compiler makes, and you want to show you are compiling from scratch.
        > rm *.class
    

    (* here just means "anything"-- it's what we call a wild card. rm *.class means "remove everything that ends with '.class'".)

  3. Display your code. Do this using the cat command. ("Cat" here is actually short for "concatenate," because if you give it several files as arguments it will concatenate them by printing all of them out. Since you have only one file today, that's a moot point.)
        > cat FirstProgram.java
    
  4. Compile your program.
       > javac FirstProgram.java
    
  5. Run your program.
       > java FirstProgram
    
  6. Exit the script.
       > exit
    
  7. Mail your script file to the class homework account.
    1. Go to your home directory
         > cd
      

      and start the mail program "pine."

         > pine
      
    2. Log into pine. It will ask for your id, which it should guess correctly, so just hit enter. It will then ask for your password, which will be the same as your webmail password.
    3. Compose a new message by pressing "C". Enter "homework.cs241@wheaton.edu" as the address in the "to" field.
    4. In the body of the message, press "Ctrl-R" to read in a file. When you are asked for the file name, give "cs241/assign2/assign2script" or whatever you have called your script file.
    5. Send the message using "Ctrl-X".

You can exit pine by pressing "Q" for "quit".

DUE: Friday, Jan 21, at 5:00 PM.


Thomas VanDrunen
Last modified: Wed Jan 19 15:04:31 CST 2005