The goal of this assignment is to write, compile, and test your first Java program, trying out the concepts we have learned in class.
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.
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).
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.
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.
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.
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.
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".
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:
> script assign2script
> rm *.class
(* here just means "anything"-- it's what we call a wild card.
rm *.class
means "remove everything that ends with '.class'".)
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
> javac FirstProgram.java
> java FirstProgram
> exit
> cd
and start the mail program "pine."
> pine
You can exit pine by pressing "Q" for "quit".
DUE: Friday, Jan 21, at 5:00 PM.