Download presentation
Presentation is loading. Please wait.
Published byNoreen Riley Modified over 8 years ago
1
Lecture 5 Methods
2
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 declarations body return value Defining a Method
3
Declaring a Static Method
4
Calling a Method In 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!");
5
Example: The max Method
6
Methods are Values of their Return Type The method max is a value of type integer.
7
Method Declarations
8
Method Call and Return
9
void Method Example Sometimes 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).
10
Methods as a Means of Abstraction
11
Method Overloading As long as the parameter lists are distinguishable, multiple methods can have the same name. difference in data type difference in number of parameters
12
The Scope of Variables The 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.
13
Scope Demon stration
14
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.
15
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
16
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.
17
Customizing the Random Sequence We 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]
18
wioilrpknuypevyppesbgqqlyzwzmflhgijerkaxyuufgppjaanisrsjxnrvnehylsxjfdmzrjhdadzcxupqmycztaxidctkjhqkcb rljdtewydnujmlbvanckdyvtyrivsyvlbtqeyakfqnyhdlghibnvmtkduuoolgztksetdaiizfhqetslnznzmydrvlqvchjbesxlolfn sftodtmrwrjwcugkeeheqxdoowbigaivhlorznuuvmlpdpnqumqixblrhvpryqdgxxzbwdfqvyvqvjgsallydrxufjtnijvhlxjiew btefwerzlchkrdnoyborftmnclbjfvyuajetmsnlercbhmflqsblsmrmdhtnwkejwsrgdbatjhkomgilpbxsfofzaqfswfuwvnqx olairmyhwxpvjpxmsfvmgyzrgqpgrqhunxpppuclctsvrjivwfyflawuqwpbmecrsadzzvzzuzovopnincrqpwlunklyqrguc spsrvkclufwtoanrslgfcapiumvosmelkbomtuoywgdqrxuozqqvbxpzvjilkyddowbgymozgsccxkmdypkaultkytzsrhvbc itkteszcrmtkocpbvcgvbpxpoiwjggapiyoxvhgzpxoilakqongitutkywqujocdygnkkselvfkalejsgyprnnhxgfkjrjqycfhrlku mzvfisgadxknaosdeeyrugmbytbtpftvucnbggbgpvxugzrogobzckyhqkxbjyiovqbwuqvhlskgfreeczffhldcilaudfmgfd wwdokwhsriekiznwzpbpfcmdmcbuarpmwvalbjczppjhepttkrugyqirbyushbqepffzxuljpfafeotwuyskkyxphztjlbykvpo pmlaljvqaggjusbnfozciuleewdxgbnbinfmqxkfjspdridbfgwbzzgdeadeqmkdtdvkxracgxotlwlifjcwptmtfkdsbevlpb Case Study: Generating Random Characters
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.