Lesson 8: Methods Recall that in Visual Basic you put your code inside “subs.” For example, if you wanted code to execute when a button was clicked, you put it inside a button’s click sub, as shown: Private Sub Button1_Click(ByVal sender As System.Object,… MsgBox("Hi") End Sub Recall further that when you had repeated code, you saved time by writing your own subs, then calling those subs from other subs. For example, clicking any of three buttons causes the checkForEnd sub in the VB example below to execute. Public Class Form1 Dim money As Integer = 0 Private Sub Button1_Click(ByVal sender As Object, ByVal e… money = money + 1 checkForEnd() End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e… money = money + 2 checkForEnd() End Sub VB Private Sub Button3_Click(ByVal sender As Object, ByVal e… money = money + 3 checkForEnd() End Sub Private Sub checkForEnd() Label1.Text = "$" & money If money > 10 Then MsgBox("You have more than $10. Me.Close() End If End Sub End Class You’re rich! Bye.") Java calls subs methods. So far, you have put all your code into a method named main. Now you will learn how to write three varieties of your own methods: methods without parameters (easiest), methods with parameters (medium difficulty), and methods with return values (hardest, most important, and optional in my VB course). The last category will get its own packet. Methods without Parameters Here is a simple java method: 1|©Joshua Britton 1 3 2 printStuff() public static void { System.out.println("stuff"); System.out.println("more stuff"); System.out.println("even more stuff"); } 4 1. Prepare to be annoyed. Ready? Good. Because you are not ready to understand these java keywords yet. Just write them when you create a method. We will learn about them later. 2. The method’s name. Notice that the first letter of a method name should be lowercase, but the first letter of any following words should be upper case. Follow this convention (it’s the same convention used for variable names). It makes life easier later. 3. The whole top line – keywords, name and parameters (the contents of the parentheses – none, yet) together – form the method’s signature. 4. The method’s contents are contained by braces. We call the new method from the main method as shown: public class MethodExample { public static void printStuff() { System.out.println("stuff"); System.out.println("more stuff"); System.out.println("even more stuff"); } public static void main (String [] args) { printStuff(); printStuff(); } } The above code causes the printStuff method to execute twice, so six lines of output appear on the console. Note that the order in which you write the methods is not important. Java always starts execution with main. However, traditionally main is the last method in the class. Methods with Parameters Often methods require information in order to do their jobs. For example, if a method displays the sum of two ints, it must be told what specific values to add. A piece of information required by a method is 2|©Joshua Britton called a parameter. Method parameters are defined inside of parentheses after the method name, as in this example: 1. Parameters public static void add(int a, int b) { int result = a + b; 2 System.out.println(a + " + " + b + " = " + result); } 1. Look back at the first java method example (printStuff). Notice that there are empty parentheses at the end of the signature (first line). That is because printStuff does not need any information in order to do its job: it always prints the same thing. Add, however, needs to know what numbers should be added, so it has two parameters, a and b, declared inside parentheses at the end of the signature. Each parameter is declared with a type and a variable name. The parameters are separated by commas. 2. Declare temporary variables inside methods as needed. Here is a program that uses the add method several times: public class MethodExample2 { public static void main (String [] args) { add(3, 5); int x = 18; int y = 21; add(x, y); double z = 1.932; add(x, (int) z); } public static void add(int a, int b) { int result = a + b; System.out.println(a + " + " + b + " = " + result); } } To call a method with parameters, write the method name, then parentheses, then values to match the required parameters. In the example, each of the three calls to add was followed by parentheses containing the two integers to add. Note that z had to be cast as an int in order to meet the requirements of the second add parameter. Parentheses In java, you know when code is calling a method because the method name MUST be followed by a set of parentheses. 3|©Joshua Britton Math.random(); NOT Math.random; In VB, parenthes were optional. In java, you must follow the method name with parentheses, even if they are empty (have no arguments). 1 ASSIGNMENTS DelayCarpal description: 1. Write a method that takes one String (that is, one String parameter) and one integer (one integer parameter). 2. The method displays the String the number of times indicated by the integer. Call the method three times from main with various strings and numbers. 3. Make sure your output is patterned according to the example output, below. Example Console Output Argula Argula Argula Argula cent cent cent Utah Utah Utah VoidTester description: 1. Write a method that has three parameters, all integers. 2. Inside the method, test to see whether the last integer parameter is between the first two integers, NOT inclusive. 3. Display the result of the test from within the method. See the example output, below. 4. Call the method 50 times from main, each time with three newly randomly generated integers. In each set, the lower number should be from 1 to 20, inclusive, the upper number should be from 81 to 100, inclusive, and the number to be checked should be from 1 to 100, inclusive. 1 The presence or absence of parentheses is how java tells the difference between a method and a variable. 4|©Joshua Britton Example Console Output 10 is between 2 and 97 31 is between 6 and 87 31 is between 4 and 89 63 is between 12 and 88 64 is between 13 and 100 9 is not between 17 and 99 58 is between 8 and 88 79 is between 6 and 90 20 is between 7 and 81 <<<<<41 more lines like this>>>>> ***Bonus Skill*** (needed for last assignment) System.out.print(String) does almost exactly the same thing as its lookalike, System.out.println(String). The difference: new new version (the one missing “ln”) does not advance the cursor to a new line. Here is an example with code and the output it produces: System.out.print(“*”); System.out.print(“*”); System.out.print(“*”); System.out.println(“”); System.out.println(“Chicken for lunch!”); Console Output *** Chicken for lunch! BoxMaker description: Write a method that takes an int parameter and uses it as the dimension (width and height) for displaying a “box” of a single character. For example, if the parameter’s value is 5, the method would display: ***** ***** ***** ***** ***** Call the method three times from main, once with a value of 2, once with a value of 5 and once with a value of 10. You should get the same output shown below. 5|©Joshua Britton Example Console Output ** ** ***** ***** ***** ***** ***** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** 6|©Joshua Britton