Sun Microsystems has done us a huge favor by writing a really excellent set of tutorials on the Java language. Best of all, they're freely available online. You can get to the start of this set of "trails," as they are called, by going to http://java.sun.com/docs/books/tutorial, where you'll find an up-to-date web version of a book they publish entitled, appropriately enough, The Java Tutorial. (You're welcome to buy the book, but it's not necessary, and we will soon stray from the topics covered in it.)
Here's what you need to do this week:
Create your first application.
Go to the Getting Started trail, and find the section entitled The "Hello World" Application. (Use the Solaris/Linux instructions.) This is the application you will be creating for this assignment; nice and simple, so that you can get up to speed on the Java command-line tools.
You can ignore the part about installing Java, since it is already installed on the CS cluster. (You're welcome to go through the process to install the Java SDK on your own machine, if you prefer.) Rather, you should log in to CS and verify that the Java interpreter works by typing:
java -versionThe CS cluster machines should have Java 1.6 installed. (If you find a different version, or you discover Kaffe instead, let me know what machine you found it on, and we will fix that.)
When working remotely, DO NOT run computationally intensive programs on login.cs.caltech.edu or orchestra.cs.caltech.edu! Many people use these machines. At best, you will become very unpopular, and at worst, your processes might be killed.
Go to part 2 of the trail, The "Hello World" Application; this is the program you will write for this lab. If you don't already have a favorite text editor, you can use emacs to enter the program. Emacs is nice because it "understands" Java code -- it will help you format it correctly, match up parentheses, etc. (If you prefer another text editor, feel free to use that one instead.)
Use emacs to create the source file like so:
emacs HelloWorldApp.java
Notice how emacs indicates it is in Java mode at the bottom of the screen.
Type in the "Hello, World!" application by hand, compile it as directed, and make sure it runs to your satisfaction.
OPTIONAL:
If you are curious what is inside your new HelloWorldApp.class file, you can type this:
javap -c HelloWorldAppYou should see a whole bunch of cryptic instructions - these are the Java bytecodes that make up your class!
Modify HelloWorldApp.
Now you will extend your simple HelloWorldApp program to print out all prime numbers between 2 and 100. Here are the changes to make:
Add a second function to your program, declared like this:
public static boolean isPrime(int n) {
// TODO: Fill in the code here.
}
This function should return true if the argument
n is prime, and false if the argument is not
prime. So, replace the "TODO" comment with the code to implement
this test.
Your test can be very simple: Just loop from 2 all the way up to n - 1, and for each of these numbers, see if the number can divide into n without producing a remainder. (If i is the number you are considering, then i % n == 0 will be true when i is a factor of n.) Of course, if you find a factor of n along the way, then the number is not prime, so you should return false. However, if the loop completes without finding a factor, then the number is prime and you should return true.
Once you have added this helper function to your program, extend your main() function by adding this code after you have printed "Hello world!":
Write a loop that starts with 2 and finishes with 100. For each value, print the message "num is prime." (with num being the number you are considering), if isPrime() returns true for the number. Don't print anything if the number is not prime.
Don't create separate classes for these modifications; make these additions in your original program.
(You can check your answer against the list of prime numbers on the Wikipedia page on primes.)
Read about objects.
In the Learning the Java Language trail, there's a section entitled Object-Oriented Programming Concepts. Read the first three pages of this:
We'll talk about objects and classes in much more detail next week. Since CS11 moves quickly, these sections will help you get more of the background on object-oriented programming and Java.
Learn how to browse the Java API documentation.
All of the objects defined in the Java specification are nicely documented as a part of the Java API. This kind of web-based documentation is called a javadoc, and is generated automatically from comments in the Java API source-code.
Go ahead and look at the Java API, and browse around randomly just for fun. Notice the package names listed in the upper-left frame of the site, and how each package relates to a specific area of functionality in the Java platform.
You can download a copy of the whole thing to your own computer for quicker browsing if you like. You can do that from this page. Search for "Java SE 6 Documentation". (Don't download the API documentation to CS, though -- you'll use up your disk quota.)
Answer some questions.
After you've skimmed the Java API a bit, answer the following questions. (Hint: use the API documentation.) Type the answers into a file called answers.txt, and place it in your directory with your code.