lecture 5

18
Lecture 5 Methods

Upload: edward-collier

Post on 15-Mar-2016

14 views

Category:

Documents


1 download

DESCRIPTION

Lecture 5. Methods. Defining a Method. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are limited to repeating the sequence at the same location in the program. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 5

Lecture 5Methods

Page 2: Lecture 5

Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are limited to repeating the sequence at the same location in the program.

A method permits us to encapsulate the sequence of operations and then to access them any time by calling the method from the main program.

A method is defined by its header which is made up of:

Other parts of the method include:

the modifiers

a return value type

method name

parameter list

declarationsbody

return value

Defining a Method

Page 3: Lecture 5

Declaring a Static Method

Page 4: Lecture 5

Calling a MethodIn creating a method, you define what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method, depending on whether the method returns a value or not.

If the method returns a value, a call to the method is usually treated as a value. For example,

int larger = max(3, 4);

calls max(3, 4) and assigns the result of the method to the variable larger. Another example of a call that is treated as a value is

System.out.println(max(3, 4));

which prints the return value of the method call max(3, 4).

If the method returns void, a call to the method must be a statement. For example, the method println returns void. The following call is a statement:

System.out.println("Welcome to Java!");

Page 5: Lecture 5

Example: The max Method

Page 6: Lecture 5

Methods are Values of their Return TypeThe method max is a value of type integer.

Page 7: Lecture 5

Method Declarations

Page 8: Lecture 5

Method Call and Return

Page 9: Lecture 5

void Method ExampleSometimes we want a method to do something rather than return a value. When there is no return value we set the return value type to void.

this method prints text centered on the specified column (in this example, column 20).

Page 10: Lecture 5

Methods as a Means of Abstraction

Page 11: Lecture 5

Method OverloadingAs long as the parameter lists are distinguishable, multiple methods can have the same name.

difference in data type

difference in numberof parameters

Page 12: Lecture 5

The Scope of VariablesThe scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a method is referred to as a local variable.

The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared and assigned a value before it can be used.

A parameter is actually a local variable. The scope of a method parameter covers the entire method.

Page 13: Lecture 5

Scope Demonstration

Page 14: Lecture 5

The Math Class

The Math class contains the methods needed to perform basic mathematical functions.

Some common Math class methods we have already used include:

Math.pow(a, b) and

Math.random()

Major categories in the Math class are:

trigonometric methods, exponent methods,

and service methods.

Besides methods, the Math class provides two useful double constants, PI and E (the base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.

Page 15: Lecture 5

Math.exp(1) returns 2.71828

Math.log(Math.E) returns 1.0

Math.log10(10) returns 1.0

Math.pow(2, 3) returns 8.0

Math.pow(3, 2) returns 9.0

Math.pow(3.5, 2.5) returns 22.91765

Math.sqrt(4) returns 2.0

Math.sqrt(10.5) returns 3.24

Math class Examples

Page 16: Lecture 5

The "random" Method

Math.random() returns a double value >= 0.0 and < 1.0. The particular value you get depends on the internal seed value of the pseudorandom number generator algorithm. The seed value changes each time Math.random() is called.

The sequence of values coming from Math.random() are in the range [0.0,1.0) and are uniformly distributed. This means that any partiular value in the allowed range is equally likely each time Math.random() is called.

Because computer are finite state machines and algorithms are deterministic, the sequence of values will eventually begin repeating. For a well-designed random number generator the length of the repeating cycle will be very long.

A common problem in programming languages is a poorly designed or improperly implemented random number generator.

Page 17: Lecture 5

Customizing the Random SequenceWe can use Math.random() to generate other random sequences with different ranges and different minimum and maximum values.

Example: We can generate a sequence of integers in the range [60,99] by multiplying values from Math.random() by 40, adding 60 and then converting the result to an integer.

rval = (int)((Math.random() * 40.0) + 60.0);

[0.0,1.0)

[0.0,40.0)

[60.0,100.0)

[60,99]

Page 18: Lecture 5

wioilrpknuypevyppesbgqqlyzwzmflhgijerkaxyuufgppjaanisrsjxnrvnehylsxjfdmzrjhdadzcxupqmycztaxidctkjhqkcbrljdtewydnujmlbvanckdyvtyrivsyvlbtqeyakfqnyhdlghibnvmtkduuoolgztksetdaiizfhqetslnznzmydrvlqvchjbesxlolfnsftodtmrwrjwcugkeeheqxdoowbigaivhlorznuuvmlpdpnqumqixblrhvpryqdgxxzbwdfqvyvqvjgsallydrxufjtnijvhlxjiewbtefwerzlchkrdnoyborftmnclbjfvyuajetmsnlercbhmflqsblsmrmdhtnwkejwsrgdbatjhkomgilpbxsfofzaqfswfuwvnqxolairmyhwxpvjpxmsfvmgyzrgqpgrqhunxpppuclctsvrjivwfyflawuqwpbmecrsadzzvzzuzovopnincrqpwlunklyqrgucspsrvkclufwtoanrslgfcapiumvosmelkbomtuoywgdqrxuozqqvbxpzvjilkyddowbgymozgsccxkmdypkaultkytzsrhvbcitkteszcrmtkocpbvcgvbpxpoiwjggapiyoxvhgzpxoilakqongitutkywqujocdygnkkselvfkalejsgyprnnhxgfkjrjqycfhrlkumzvfisgadxknaosdeeyrugmbytbtpftvucnbggbgpvxugzrogobzckyhqkxbjyiovqbwuqvhlskgfreeczffhldcilaudfmgfdwwdokwhsriekiznwzpbpfcmdmcbuarpmwvalbjczppjhepttkrugyqirbyushbqepffzxuljpfafeotwuyskkyxphztjlbykvpopmlaljvqaggjusbnfozciuleewdxgbnbinfmqxkfjspdridbfgwbzzgdeadeqmkdtdvkxracgxotlwlifjcwptmtfkdsbevlpb

Case Study: Generating Random Characters