Starting your Work with Java

Exercises


For an introduction on how to use the Java System in this Pool see the step by step description.

Compilation

  1. Copy the the class Printer into a file Printer.java.
    Note, that the name of the class and the name of the file (except the extension) have to be identical.
  2. Compile the class by executing
    javac Printer.java
  3. This calls the Java Compiler for this class, compiles the class and produces a file with byte code for the class named Printer.class. Locate the produced byte code file in your directory.
  4. Look into the class definition. Identify the building blocks of the class Printer.

Execution

  1. Start the Java interpreter to execute the Java program related with the class Printer. This is done by the following command:
  2. java Printer

    This command causes the method main of the class to be executed.
    Note that the method main has to be specified exactly in the following way:

    public static void main(String [] args) { .... }
  3. Identify the main method in the class Printer. Try to understand the output it produces.

Passing a parameter into a program

  1. You can pass a parameter into a program by writing it behind the class name when starting the interpreter. With the following example the parameter "NewPrinter" is passsed to the program Printer.
    java Printer NewPrinter
  2. Inside the main method this parameter is accessible via the array args (args[0]). This is used in the main method of the class Printer:
            public static void main (String [] args)
             { // Object creation 
              Printer p = new Printer(); 
              p.printName(); 
              p.printDifference(4, 3); 
              p.setName(args[0]); 
              p.printName(); 
             } 
        
  3. Test the parameter passing for the class Printer. You find the necessary statements as a comment in the method main of the class Printer.

In the following exercises the class Printer ist extended by further methods:

Method definition

  1. Define a method printSum that calculates the sum of two numbers as parameters and prints the result. Hint: Use the method printDifference as an template for this method.
  2. Test your method.

Iteration

  1. Write a method printUpTo for the class Printer that prints all numbers up to the number passed as a parameter.
    Hint: You can find the syntax of the loop-statements in the Language Fundamentals Summary.
  2. Test your method.
  3. As an extension to this exercise you may try to implement the method with the different types of loops supported by Java (for, while, do-while)

Recursion and Conditions

  1. Write a method printFactorial for the class Printer that calculates and prints the factorial (n!) of a number n. Provide an output for each step in the recursion.
  2. Test your method.

Reading Input from the Command Line

  1. The class Arithmetics provides a program to read numbers from the command line. Copy the the class into a file Arithmetics.java.
  2. Use the printSum method of the class Printer to print the sum of the two numbers.
  3. Invent

Further Exercises

  1. Write a method printGcd in class Printer that calculates the greatest common divisor of two numbers passed as parameters. Visualize every calculation step by an output.
  2. Invent some own methods for the class Printer.

 


Software Systems Institute

Home of the Java Course Claudia Niederée, oct 1999