Programming 1, fall 2007 -- Demonstration 4

Where I want everybody to be after demonstration 3:

So far, all our programs have been of the simplest kind; they have had only one method which is defined as public static void main(String[] args){ /* statements here */ }. The structure of the compilation unit has been the following:

public class TheNameOfYourProgram {
    public static void main(String[] args){

        /* All that we do at first is written here. */

    }
}

Agenda for the next step:

In this demonstration, we shall be making programs that have more than one static method. This is how programming is, from now on! The structure could be like this, for example:

public class TheNameOfYourProgram {


    public static void someVerySimpleMethod(){
        /* No parameters, no returns (denoted by 'void') */
    }

    public static int someMethodReturningInt(int param1){
        /* One integer parameter, one integer return.
         * Some computation will be written here.
         * At some point, a return-statement with an expression
         * yielding an integer value is required
         */
    }

    public static double yetAnotherMethod(double p1, int p2){

        /* Computing "a double value as a function of one double and
         * an integer".
         *
         * At some point, a return-statement with an expression
         * yielding a double value is required
         */
    }

    public static void main(String[] args){

        /* We call our methods from here. Here are only
         * some "test cases" to know whether or not our
         * methods are working at the moment.
         */

    }
}

In addition to the concept of subroutine, we take a look at all of the primitive types of Java. We still use only local variables and parameters. We use only two of the control structures: if and for. Later this set must be extended with many others, but we must keep one step as small as possible.

Contents

Subroutines

A cut-down abstract syntax for a method declaration in Java is:

MethodDeclaration =
   {Modifier} ReturnType MethodName "(" FormalParameters ")"
     BodyBlock;

ReturnType = "void" | Type;

FormalParameters =
   Type Identifier {"," Type Identifier};

MethodName = Identifier;

BodyBlock = Block;

Where Type can be any of the types available in Java (either a primitive or an object reference type), Identifier is any name that Java allows. The conventions tell you how things should be named according to the meaning of the things you declare; a method name should begin with a lower-case letter.

For now, let us use exactly two modifiers in each method declaration: public static; of these, public means that we intend the whole world of programmers to be able to call the subroutine from their own applications, and static means that we don't deal with an object's internal state, but rather via parameters and return values only.

Above you can see examples of concrete Java language that follows the syntax. Bare in mind:

  • The ReturnType declares what kind of data your method returns as its value. The value can be based on the values of parameters.
  • The FormalParameters is a comma-separated list of parameters. Each must have a type and a name. These names are local to the method, and have absolutely no effect in other parts of the program.

Exercise 1:

Take your earlier programs as a starting point. Divide the many statements in main() -methods to smaller pieces, and put each inside a different method. Then call the methods from the main method. See that your program works as you'd expect...

Exercise 2:

Make more useful methods, that use paramters that can have any value. Last time, I urged you to experiment with, for example, the following tasks:

  • converting a number of seconds into hours, minutes and remaining seconds in the range 0..59 (you'll need integer division operator / and perhaps the modulo % operator, and maybe some helper variables to store intermediate results...)
  • Areas of triangles or circles, from elementary math...
  • anything that comes to your mind.

Any of these can be made a parameterized method. Make them so, and call them from the main method!

Exercise 3:

Last time:

"make a program that stores some temperature as Celsius degrees

in a variable of type double and then transforms the temperature to Fahrenheit degrees (stored in another variable of type double). The conversion formula is mathematically the following:

tempFahr = (9/5)*tempCels + 32

Now, put this into a method which you call by:

calcFahrFromCels(2.0);

or similar. Then, using the for-loop, make a program that computes and prints out, one after the other, fahrenheit degrees for celsius temperatures 1.0, 1.1, 1.2, ... 100.0 - no way you'd do this by copy-paste!