26
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

Embed Size (px)

Citation preview

Page 1: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

CS 106Introduction to Computer Science I

02 / 26 / 2007

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Today’s Topics• questions, comments?

• more about programmer defined methods

– return type

– method name

– specifying parameters and their types

– how to call a method

– when a method finishes executing

• A few method examples

Page 3: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

what is a method

• A method is a piece of a program designed to achieve some specific task and usually returns some piece of information.

• A method is invoked by a method call.

• To call a method, you provide its name and the correct arguments that are necessary for the method to execute.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Page 4: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example of a programmer-defined method

public double salary_after_raise(double curr_sal, double raise_pct )

{

double new_sal;

new_sal = curr_sal * ( 1 + raise_pct / 100 );

return new_sal;

}

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Page 5: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example of a programmer-defined method

• to call this method from some other method within the same class do the following:

// example variable declarations...double new_sal;

double old_sal = 300; double raise = 4.5;

// our call to the salary_after_raise method new_sal = salary_after_raise( old_sal, raise );

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Page 6: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example of a programmer-defined method

• Let’s look at a complete program that contains this method and calls it.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Page 7: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example of a programmer-defined method

• In our example, the method was called from only one place but in real applications, certain methods will need to be called in many different parts of the program.

• When certain code can be used in several places in a program, you may want to create a method containing that code. Then, wherever that code would have been, a simple method call appears.

• They also aid in the ability to understand a program (readability) and make changes.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Page 8: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

when does a method's code execute?• only when the method is called does it execute ---

program control changes to the code in the method– and the values of arguments that are passed into the

parameters are then usable within the method

• All Java applications start at the beginning of the main method. You can think of the main method being implicitly called to start the program. Then if the main method a method call, the code in that method executes and when finished, returns to where it was called.

Page 9: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

when does a method end it’s execution?

• when it hits a return; statement

• when it hits a return some_expression; statement

• or, when it hits the right curly brace of the method

• whichever comes first.

• when the method ends its execution, the program execution continues at the method call

Page 10: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

more about methods• methods that do not return a value are of type void

(like the main method.)

• methods are not required to have any parameters; these are defined with nothing between the parentheses

• to call a method with no parameters, you must still use the parentheses but with nothing between them

• Note that the return type of a method can be different than any or all of the types of parameters that get passed in.

Page 11: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example method that returns nothing and has no parameters

public void print_error_msg()

{

System.out.println(“Invalid entry.”);

System.out.println(“You must reenter.”);

}• how to call this method: note that there are no arguments nor

is there a variable to which to assign the returned value (because it doesn’t return a value.)

print_error_msg();

Page 12: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example method that returns nothing but has one parameter

public void print_msg(String the_msg){

System.out.println(“The message is: ” + the_msg);

// note: no return statement since nothing to return// The return type of this method is void.

}• how to call this method:

print_msg(“Good afternoon gentlemen. I am a HAL 9000 computer.”);

// name the film.

Page 13: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

other reasons to create and use methods

• separation and modularization of ideas is helpful when writing large programs because smaller parts of a problem are easier to solve

• the individual methods can be tested and confirmed correct to reduce debugging woes

• methods lend themselves to software reusability

• less code repetition

• more readable, better designed code

Page 14: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

scope

• the scope of an identifier (for a variable, reference or method) is defined as the portion of the program that can access that identifier

• the two scopes for identifiers are class scope and block scope

• class scope starts at the opening left curly brace of a class definition and ends at its corresponding right curly brace

• block scope refers to identifiers that are accessible from when they are declared until the next right curly brace.

Page 15: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

variables in methods• variables declared in methods are said to be local

variables --- they are local to that method

• they are not usable, nor are they even known outside that method --- they have block scope

• they are created (memory is allocated for them) on entry into the method

• they are destroyed (memory for them is marked for deallocation) on exit from the method

• parameters to the method are also considered local variables to that method.

Page 16: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

some method terminology• the name of the method is salary_after_raise ,

and the body of the method is the three lines of code between the curly braces.public double salary_after_raise(double curr_sal, double raise_pct)

{

double new_sal;

new_sal = curr_sal * ( 1 + raise_pct / 100 );

return new_sal;

}

Page 17: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

example of local variables• in this method, new_sal is a local variable, and

the parameters to the method curr_sal and raise_pct are also local variables.public double salary_after_raise(double curr_sal, double raise_pct)

{

double new_sal;

new_sal = curr_sal * ( 1 + raise_pct / 100 );

return new_sal;

}

• they are only known to this method• what does that mean “only known to this method”

Page 18: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

return type and argument types• the return type of the method is double, and the two

arguments are of type double.

public double salary_after_raise(double curr_sal, double raise_pct )

{

double new_sal;

new_sal = curr_sal * ( 1 + raise_pct / 100 );

return new_sal;

}

• note that the return statement returns the double and this is where the execution of the method ends

Page 19: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

method calls• in the call of the method, two values are passed in

to the method as arguments and the value returned by the method call is stored in new_salarydouble new_salary;

double old_salary = 300;

double raise = 4.5;

// our call to the salary_after_raise method

new_salary = salary_after_raise( old_salary, raise );

Page 20: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

variables passed in as arguments when calling a method

• the values of variables that are passed into a method as arguments remain unchanged

• even if the corresponding parameter in the method changes its value, this change is only local to the method, and the value is not “passed back out” of the method.

• we’ll see this in the factorial method we create.

Page 21: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

compute_factorial method

public int compute_factorial(int num){

int temp_factorial=1;while (num > 0)

{temp_factorial = temp_factorial * num;num--;

}return temp_factorial;

}

Page 22: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

compute_factorial method• this method is a programmer-defined method (we make

up the name and the purpose of the method.)• the name of the method is: compute_factorial • the return type of the method is: int• there is one parameter to the method, which is: num (of

type int)• note that the value of num inside the method changes (it

gets decremented in the loop) but this is only a local change

• the other local variable (temp_factorial) is used to compute the factorial and its value is the one that is returned to the caller.

Page 23: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

compute_factorial method• note that the value of num inside the method

changes (it gets decremented in the loop) but this is only a local change

• Let’s write a quick program to show how the variable that is “passed in” to the method doesn’t actually get changed.

• We’ll print the parameter variable’s value before and after the call to the method.

Page 24: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

promotion of argument types

• if a method is called with arguments of types different from those specified in the parameter list of the method definition, then Java will try to convert the value to the required type.

• The conversion is done according to Java’s promotion rules

• Java only allows conversions such that no information would be lost by the type conversion.

• If information would be lost, then a compiler error would result.

Page 25: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

promotion of argument types

some valid type conversions:– a float can be passed into a method requiring a

double– an int can be passed into a method requiring a

long, or a float or a double

some type conversions that would cause compiler errors:– a float cannot be passed into a method requiring an

int– a double cannot be passed into a method requiring

a float

Page 26: CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann

a method call with the wrong typepublic int test_value(double input_val){

if (input_val < 0)return –1;

elsereturn 1;

}Example call in some other method:

float length = (float) 3.45;int returned_val;returned_val = test_value(length); // call is allowed: float length is promoted to double;