Language Fundamentals

Exercises


  1. Inspect the Java class documentation provided by the JDK. You will find it via the OOAD home page or directly at (http://pc01.sts.tu-harburg.de/Doku/java/jdk1.2/docs/api/index.html). Search the classes Integer and Boolean which are direct object counterparts to the primitive types int and boolean. Write a test program that converts an int value to an Integer object (and vice versa) and an bool value to a Boolean object.
  2. Write a program that defines variables for each primitve type (int, float, boolean, char), carries out one of the described operations (see handout), and prints the results. You can extend and reuse the class Printer from the previous exercise for this purpose.
  3. Write a program that prints values from one to 100. Avoid printing the values divisible by 13 and let the program print the string "Bad Luck" instead.
  4. Create a program to print the alphabet in lower case, explicitly marking the letters which are contained in the words "java" or "programming". Note that the letter 'a' is in both words. Nest a switch statement into a for loop for this purpose.
  5. Java supports different types of comments. One of them can be used to automatically create documentation equivalent to the documentation of the standard libraries for your classes in HTML format. This type of comment is called documentation comment and can be used to provide documentation for classes, methods, data members and constructors. Theses comments may contain special tags like @return or @param which are exploited for the formatting of the documentation (see table).You may include HTML tags into your comments.

    option purpose comment context Hints
    @param Description of parameter method

    use for each parameter separately; give the name of the parameter first, then the decription:

      @param  newName  new name for the printer
    @return Description of return value method  
    @version Version of a class class Run the documentation tool with the option -version if you want to include the this information in your documentation
    @author Author of the class class Run the documentation tool with the option -author if you want to include the this information in your documentation
    @see reference to other class class, method Link to the named class is created
    @exception Exception that the method may throw method

    use for each possible exception separately; give the name of the exception first, then the decription (see @param)


    In order for the documentation tool to extract the comments the following rules have to be taken into account:
  6. See class Printer2 for an example.

    Try out the documentation tool:

    javadoc -d docu Cell.java Printer2.java

    Check the resulting documentation.

  7. Write a program that computes the prime numbers less than 1000.
  8. **Try to improve your programm by employing the Sieve of Eratosthenes:
    The basic idea of the Sieve is to provide one boolean (or bit) for each number from 1 to n (array of n booleans)  when you want to compute all the prime numbers less than n. Initially all these values are set to true. Whenever a prime number is discovered the array entry related with this number and those related with its multiplicities are set to false. Proceeding like this the next entry in the array equal to true is the next prime number. Define a class Sieve to implement the sieve.

Software Systems Institute

Home of the Java Course Ulrike Steffens, may 1998