Java Lab 1: Getting Started

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:

  1. 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 -version
    The 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.)

    NOTE:

    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 HelloWorldApp
    You should see a whole bunch of cryptic instructions - these are the Java bytecodes that make up your class!

  2. 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:

    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.)

  3. 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.

  4. 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.)

  5. 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.

    1. The class java.awt.Color offers a number of predefined constants for specifying various colors. (Java constants are fields that are typically declared with the public, static, and final modifiers.) List all of the different colors that are defined on this class.
    2. Write a single line of code that would construct a new Color object for the color purple, using one of the Color constructors. Pay attention to the data types of your arguments, and the data types required by the constructor you choose.
    3. List all the methods that appear on the java.lang.Object class. Briefly describe what the toString() and equals() methods are for.
    4. Let's say you are working on a Java program that needs random values with a Gaussian distribution. You hear that there is some "random" class that can help you with this. What is the exact name of this class, including the package name? What method would you use to generate your random values?


[end lab 1] Copyright (C) 2005-2008, California Institute of Technology.
Last updated October 8, 2008.