180 likes | 248 Views
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.
E N D
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. A methodpermits us to encapsulatethe 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
Calling a Method In creating a method, you define what the method is to do. To use a method, you have to callor invokeit. 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!");
Methods are Values of their Return Type The method maxis a value of type integer.
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).
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
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.
The Math Class The Mathclass 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 Mathclass are: trigonometric methods, exponent methods, and service methods. Besides methods, the Mathclass provides two useful double constants, PIand E(the base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.
Math class Examples 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
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.
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]
Case Study: Generating Random Characters wioilrpknuypevyppesbgqqlyzwzmflhgijerkaxyuufgppjaanisrsjxnrvnehylsxjfdmzrjhdadzcxupqmycztaxidctkjhqkcbrljdtewydnujmlbvanckdyvtyrivsyvlbtqeyakfqnyhdlghibnvmtkduuoolgztksetdaiizfhqetslnznzmydrvlqvchjbesxlolfnsftodtmrwrjwcugkeeheqxdoowbigaivhlorznuuvmlpdpnqumqixblrhvpryqdgxxzbwdfqvyvqvjgsallydrxufjtnijvhlxjiewbtefwerzlchkrdnoyborftmnclbjfvyuajetmsnlercbhmflqsblsmrmdhtnwkejwsrgdbatjhkomgilpbxsfofzaqfswfuwvnqxolairmyhwxpvjpxmsfvmgyzrgqpgrqhunxpppuclctsvrjivwfyflawuqwpbmecrsadzzvzzuzovopnincrqpwlunklyqrgucspsrvkclufwtoanrslgfcapiumvosmelkbomtuoywgdqrxuozqqvbxpzvjilkyddowbgymozgsccxkmdypkaultkytzsrhvbcitkteszcrmtkocpbvcgvbpxpoiwjggapiyoxvhgzpxoilakqongitutkywqujocdygnkkselvfkalejsgyprnnhxgfkjrjqycfhrlkumzvfisgadxknaosdeeyrugmbytbtpftvucnbggbgpvxugzrogobzckyhqkxbjyiovqbwuqvhlskgfreeczffhldcilaudfmgfdwwdokwhsriekiznwzpbpfcmdmcbuarpmwvalbjczppjhepttkrugyqirbyushbqepffzxuljpfafeotwuyskkyxphztjlbykvpopmlaljvqaggjusbnfozciuleewdxgbnbinfmqxkfjspdridbfgwbzzgdeadeqmkdtdvkxracgxotlwlifjcwptmtfkdsbevlpb