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

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

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

CS 106Introduction to Computer Science I

02 / 23 / 2007

Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

Today’s Topics• questions, comments?• solution to lab problem to determine highest and

lowest temperature in an array

• random method in the Math class

• programmer defined methods

random( ) method in the Math class

double rand_num;

rand_num = Math.random ( ); // what value might rand_num have after this line of code?

// is 0.34452 a possible value?

// is 2 a possible value?

// is -14.555423 a possible value?

random( ) method in the Math class

• random ( ) returns a double whose value is >= 0 and < 1, but sometimes we want a random integer

• How might we do that?

random( ) method in the Math class

• random ( ) returns a double whose value is >= 0 and < 1, but sometimes we want a random integer

• One way to do that is to first multiply the result by some integer to get a value that isn’t necessarily between 0 and 1.Then, cast this new value to an int by using the (int) cast operator.

random( ) method in the Math class

// example:int some_random_int;double some_random_dbl;

some_random_dbl = Math.random ( ) * 25; // this will return a value >= 0 and < 25.

some_random_int = (int) (Math.random ( ) * 25);

// what is the range of values for some_random_int here?

random( ) method in the Math class

int random_card_value;

int random_card_suit;

random_card_value = 1 + (int) (Math.random ( ) * 13);

random_card_suit = (int) (Math.random ( ) * 4);

• Let’s put this code in a program and execute it.

random( ) method in the Math class• What if I put the cast to int without using parentheses

around the rest of the expression?

• e.g.

random_card_suit = (int) Math.random ( ) * 4;

random( ) method in the Math class• What if I put the cast to int without using parentheses

around the rest of the expression?

• e.g.

random_card_suit = (int) Math.random ( ) * 4;

• since the cast operator (int) has higher precedence than the multiplication operator *, it would be done first, which means what?

random( ) method in the Math class• random_card_suit = (int) Math.random ( ) * 4;

• the Math.random() method call would return a double value and immediately this value would be cast to an int. Casting a double to an int causes the truncation of any decimal portion. Recall that the double that is returned by Math.random() is >= 0.0 and < 1.0

• So, what's the possible values of (int) Math.random() ?

random( ) method in the Math class

• (int) Math.random( ) would always be zero.

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

methods

• Here’s a good analogy of a boss and worker to describe methods and their callers.

• A boss (the caller) asks a worker (the method that is called) to perform a task (the code in the method) and report (return results) back when the task is done.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

methods• methods are defined within a class.

• we have seen the main method defined in the class of every application so far.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

other methods we’ve used• We have used other methods available in the Java

API.

• methods like parseInt and parseFloat and println are available to us to use in their respective classes Integer, Float and System.out

• These methods are defined for us to do a specific task. We call them when we need them.

• e.g. in the case of Integer.parseInt --- it’s task is to convert a String into an int. It takes a String as an argument and returns a value of type int.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

other methods we’ve used• e.g. in the case of Integer.parseInt --- it’s task is to

convert a String into an int. It takes a String as an argument and returns a value of type int.

• example of a call to this method:

some_int = Integer.parseInt( some_str );

• In this example, some_str is the argument that is being passed in to the parseInt method and some_int is the variable that will get the value returned by the method.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

methods• We can create our own methods that we can call

to perform specific tasks.

• Let’s say we’re writing a program to handle employee’s salaries. We might need to compute a salary after a raise.

• This example method would need to have access to the current salary and the raise percentage.

• It would then calculate the salary after the raise and return this value.

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

example of a programmer-defined method

• we might name this method salary_after_raise

• we need to take in two arguments, one for the current salary and one for the raise percentage.

• What primitive type might these arguments be?

• we also need to return the salary that is computed.

• What primitive type might this returned value be?

Michael Eckmann - Skidmore College - CS 106 - Spring 2007

example of a programmer-defined method

• so, this method could look like:

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

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

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

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

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

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.

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();

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.

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