Java can be downloaded for free. For instructions on how to install Java on your
machine go to this website :
http://java.sun.com/j2se/1.3/
and download the correct package for your platform.
Unlike DrScheme where you had one program that did everything writing code in Java
involves the following steps:
Note: This is America, the land of the free, and you can use any text editor that you choose so long as your code complies. However, the TA's reccomend that you do one of the following:
What you shouldn't do is to use a program that isn't designed for writing code at all (e.g. Word or WordPad). Doing so is a bad choice and will not get you any sympathy from the TA's if you do so.
This lab is long. All you are asked to do is read this lab carefully and follow some simple steps. If you don't read/follow these instructions, then you will be left all alone in the dark to grope for a flash light.
If you are using EMACS, look for the line ENLIGHTENED EMACS USERS LOOKY HERE for specific instructions. If you are not using EMACS or feel secure in your EMACS skills, you may skip these chances for enlightenment.
Ladies and Gentleman you may now start your editor.
To complete this lab you will need to know how to how to open a file, edit, and save. If you're using EMACS you may want to print out this list of easy-to-memorize commands. If you aren't using EMACS then you are on your own.
ENLIGHTENED EMACS USERS LOOKY HERE - Hint 1
Unlike Scheme where you comment code using the ;; In Java commenting involves using slashes and asterisks.
These are many types of comment in Java. One consists of /* and */ . Everything in between the first pair ( /* ) and the second pair ( */ ) is ignored by the compiler. For example,
/* this is a comment */
These kinds of comments can also span multiple lines. For example,
/* this is a comment that spans multiple lines */
Another common format for these comments (called a javadoc comment) is
/** * this is a javadoc comment * that spans multiple lines * and is formatted differently */
The other kind of comment consists of two slashes, // . Everything on the same line that follows this comment is ignored. The following comment is equivalent to the one above:
// this is a comment
These kinds of comments do not span multiple lines.
For instance:
// this isn't quite a comment
Notice that "a comment" is not on the same line as "this isn't quite" therefore it is not commented out. Only "this isn't quite" got commented.
ENLIGHTENED EMACS USERS LOOKY HERE - Hint 2
Here's what a sample header looks like:
/** * Name : Lionel Mandrake * GT Number : gtXxxxX * Lab : Lab 5 * Course : cs1321 * Instructor : Sweat * Lecture Time : 1:30 * * * < PRE> * Intro to Java * * Revisions: 1.0 Jan 3, 1999 * Created class Lab5 * </PRE> * * @author < A HREF="mailto:gtXxxxX@prism.gatech.edu" > Lionel Mandrake </A > * @version Version 1.0, Nov 30, 2001 */
Open the file ident.txt. Modify it so that it accurately your personal information. This header needs to be at the top of all *.java files submitted. You have been warned! You can potentially lose all credit on this lab for forgetting the identity box.
First, start up your editor if you haven't already and open up the file "ident.txt". Edit the file to reflect your personal information. Save the file. Now, insert the contents of "ident.txt" into all the java files that you will be turning in.
First, all the Java programs you write are just text files. You follow some very specific rules called language constructs to create your program. Once your program is created you compile it. By compiling your program, you are making a special format that the computer can understand and execute. That's leaving out a lot of details but hopefully that will give you an idea of what's going on when you hear stuff about "compiling" your code.
For example, suppose you created a text file called Test.java. The file Test.java is readable by humans. (Well, it's perhaps not very interesting to read, but it *can* be read. :) This source code cannot be run by the computer. Instead, you have to compile the source into a file called Test.class that can be executed. The Test.class file merely contains a set of bytes (numbers, really) that are 'op codes' or operation instructions for the computer.
compile ------- > run ----------- > :) Test.java ----- > Test.class ---- > :)
We strongly recommend you compile using a command prompt.
Windows Users: In Windows 95/98: From the Start menu, select "Programs"
and then "MSDOS PROMPT."; In NT/2000: From the Start menu, select "Programs"
then "Accessories" and then "Command Prompt". Once opening the command
prompt you will need to add the JDK directories to your path (that is, you need to tell Windows
where to find the Java compiler's files).
Type the following at the command prompt:
path=%path%;C:\jdk1.3.0_01;C:\jdk1.3.0_01\binNote: If you changed the default install directory you will have to change the above line accordingly. (If you are an advanced user, you might want to peremenantly add the JDK directories to your path.)
Before you can run your program, you must go into the directory
that has the files. Navigate to the directory that contains your .java files.
Type the following at the command prompt:
javac *.javaThis calls on the Java compiler to compile all the .java files. Remember that all .java files are compiled into .class files.
Note: Currently the JDK version on acme is the old version, do not use it.
ENLIGHTENED EMACS USERS LOOKY HERE - Hint 3
If things were setup correctly, you should be able to compile a file now.
Download the lab5.java file from here The lab5.java file should compile without errors. If you do have errors, make sure you didn't accidentally remove a comment, or alter the file in any way other than adding your ident box. If you can't locate the problem, try removing your changes and getting it to compile first. Then go back and add your ident box back taking care not to clip a part of a commented section. If all else fails, post to the lab newsgroup.
Check your directory (you can either use NT Explorer or the DOS prompt), and you should see a file called "lab5.class". This is the file that we compiled.
Let's try running this. To run your program, type:
java lab5
Note that this differs from the other command line above. When running .class files, you do not need to include the file extension.
ENLIGHTENED EMACS USERS LOOKY HERE - Hint 4
When running the program, you should see something that looks like:
I like VI personally! I was a slacker and now I have to preach the EMACS gospel =( This is Fruitcake's first Java program
Now replace the word Fruitcake with your last name. Seeing your name appear on the screen should be the high point of your day. You should realize that the thrill of seeing your name appear on a screen for the first time, after being generated by a program, is never again achievable. It is all downhill from here...
Important:
If you ever have to transfer java files by ftp, remember these two rules:
.java files have to be transferred in ASCII.
.class files have to be transferred in binary.
Part of what we've been trying to show you in this Lab and in the MATLAB Lab, is that the skills you learned in this class while using Scheme easily translate in to other programming languages. You'll notice that the the Java syntax is a bit different from Scheme and MATLAB, but the methods you use are the same.
Download the factorial.java file. Once you have the file, compile it as-is and run it... you'll notice it outputs the following:
Testing fib(1): Expected: 1 Program Returned: 1 Testing fib(2): Expected: 1 Program Returned: 1 Testing fib(6): Expected: 8 Program Returned: 8 ----------------------------- Testing fact(0): Expected: 1 Program Returned: 0 Testing fact(3): Expected: 6 Program Returned: 0 Testing fact(9): Expected: 362880 Program Returned: 0
As you can see, the "factorial" program can both compute the factorial (using the function "fact") and the Fibonocci ("fib"). What you can also see is that the fib function works and that the fact one does not. Hum...
Now open the file up in your editor. You'll see that we've written all of the structural "stuff" for you. We've also written the fib function for you too as an example.
Your job is to edit the fact function so that it will compute the factorial of a number. That's it, only 4-6 lines of code required (and add in your ident box). That's all you have to do. (Hint: You may want to base your code to fact on the code we used for fib.)
Once you're done writing the function, compile the file and run it. It should produce the following:
Testing fib(1): Expected: 1 Program Returned: 1 Testing fib(2): Expected: 1 Program Returned: 1 Testing fib(6): Expected: 8 Program Returned: 8 ----------------------------- Testing fact(0): Expected: 1 Program Returned: 1 Testing fact(3): Expected: 6 Program Returned: 6 Testing fact(9): Expected: 362880 Program Returned: 362880
Due Before: Friday, 7 December, 2001 8:00AM
Turn in the following Java files through WebWork:
In WebWork use the "Add" button (not "Add to Zip") in the file submission window to add
in all of the files. Submit all of the files at the same time. When you retrieve your submission,
the zip file you receive should have all of the above Java files in it. Unzip the file to a new
directory, complie and then run each of the files.
Do NOT submit .class files to Webwork. Submitting a .class file instead of a .java file will result in NO credit for that part. Don't forget to put your ident box at the top of ALL files. In order to receive credit for a part of this assignment, your .java file MUST COMPILE. If any of your files doesn't compile, after retrieving them from WebWork, you won't receive credit on that part of the assignment, simple as that.