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
- 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.
- Compile the class by executing
javac Printer.java
- 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.
- Look into the class definition. Identify the building
blocks of the class Printer.
Execution
- Start the Java interpreter to execute the Java program related with the
class Printer. This is done by the following command:
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) { .... }
- Identify the main method in the class Printer. Try to understand
the output it produces.
Passing a parameter into a program
- 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
- 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();
}
- 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
- 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.
- Test your method.
Iteration
- 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.
- Test your method.
- 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
- 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.
- Test your method.
Reading Input from the Command Line
- The class Arithmetics provides a program to
read numbers from the command line. Copy the the class into a file Arithmetics.java.
- Use the printSum method of the class Printer
to print the sum of the two numbers.
- Invent
Further Exercises
- 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.
- Invent some own methods for the class Printer.

|
|
Claudia Niederée, oct
1999 |