Laboratory: Introduction to Java
CSC 207 Algorithms and Object Oriented Design Spring 2010

Summary: In this lab, you will run and modify your first Java programs.

Preparation

  1. Create a directory for your lab exercises
    $  mkdir -p somewhere/CSC207/labs
    (The -p switch makes all the parent directories along the way for you.) Presumably, you'll want to use this directory when we start creating other directories for future labs.
  2. Make a directory for this lab
    $  mkdir somewhere/CSC207/labs/intro
  3. Go to your new directory.
     $ cd somewhere/CSC207/labs/intro
  4. Copy the file for this lab to your new directory:
    $ cp ~weinman/public_html/courses/CSC207/2010S/labs/code/introjava/First.java .
                

Exercises

Exercise 1

  1. Compile the java file you copied using the Java compiler:
    $  javac First.java
  2. This should create a .class file called First.class. Run this program using the Java virtual machine:
    $  java First
    (Note that the extension is omitted.)
  3. Finally, open the file First.java in emacs:
    $  emacs First.java & 
  4. Marvel at the work it takes to print one word.
  5. Change the Hello in First.java to a word or phrase of your choice.
  6. Re-compile the program and run it to see that it works properly.

Exercise 2

  1. Make a copy of First.java called Second.java. Do not make any changes.
    $  cp First.java Second.java
  2. Compile Second.java
    $  javac Second.java
  3. What happens?
  4. Modify Second.java so that you can run it. Remember that the name of a class must match the name of the file.
  5. Modify Second.java so that it prints two lines:
    Hello
    Goodbye
  6. Compile and run Second.java.

Exercise 3

  1. Presumably you have two calls to println in Second.java. Replace the first println with print.
  2. What effect do you expect this change to have?
  3. Verify your prediction experimentally.

Exercise 4

  1. Change the second println in Second.java to print.
  2. What effect do you expect this change to have?
  3. Verify your prediction experimentally.
  4. You are likely to get a different result than you expect. Try adding the command pen.flush() after the two calls to pen.print(...) and see what difference this makes.
  5. Replace the call to pen.flush() with pen.close(). Rerun the program to see what difference that change makes.
  6. Read the documentation for flush. Read the documentation for close.

    Praise the authors for their clarity.

  7. If possible, explain the difference between flush and close.
  8. If you don't understand what's happened in this exercise, please discuss it the instructor or a neighbor.

Exercise 5

As you may have noticed, it can get irritating to type the full name of a class each time you use the class. Hence, the designers of Java included a shorthand mechanism that lets you type only the basic class name. To use this shorthand, you must include a line at the top of your program (after the package declaration) of the form
 import packagename.ClassName;

You may then use just ClassName in the rest of the program.

  1. Replace all instances of java.io.PrintWriter in Second.java with PrintWriter. Do not add the import statement above. What do you expect to happen when you try to compile and run Second?
  2. Verify your answer experimentally.
  3. Add the import statement and try again.

Exercise 6

  1. Created your own java file called Odds.java.
  2. Add the skeleton for the necessary main method that is invoked when your program is run from the command line.:
      public static void main(String[] args)
      {
    
      }
  3. Using this class and main, fill in the method so that your program expects one argument and prints every odd number less than the argument, each separated by a single space.

    Note: The method

      int Integer.parseInt(String s)
    will convert a string to an int for you.
  4. Compile and run your program on several inputs to test it. For example:
    $  java Odds 10
    should produce
    1 3 5 7 9
  5. What do you expect might happen if you give a non-numeric argument, such as abcdxyz?
  6. Run your program to find out.
  7. What do you expect might happens if you use a non-integral argument, such as 10.2 or 11.1?
  8. Run your program to find out.
  9. Perhaps somewhere in your code you have a line similar to:
      System.out.print(i + " ");

    (If you are using two different print statements to accomplish this, be sure you understand how the above works before going on.) Change your program so that it instead has (something similar to) the line:

      System.out.print(i + ' ');
  10. Recompile and run your program.
  11. What happens? Can you explain this behavior?

Exercise 7

  1. Create a new class file for a program that will be called Days.
  2. Write your main routine so that it expects one argument, a single character consisting of one of M,T,W,R,F,S,N. Using a switch statement depending on the character, your program should print the full name of day of the corresponding day of the week (e.g., Monday-Sunday). (See Section 1.5.8 in the text if you do not remember how to use switch.)

    Note: you can determine the length of the first argument with the expression

    args[0].length()
    which will evaluate to an int value. You may also extract the first character from a String variable (which args[0] is), with the expression
    args[0].charAt(0)
    which will evaluate to a char value.

    Your program should print an error message if it is invoked with an invalid input.

  3. Compile and run your program on several inputs to test it.
Created: Samuel A. Rebelsky, 30 January 2005
Adopted from Espresso: An Introduction to Java