ICS 3U1 Unit 3 – Methods Name: _______________ Method Parameters To make methods more flexible, we often want to give them different values to use in their processing and calculations. Consider the following method, which outputs any character a specified number of times on the same line. public static void printRow (char c, int numTimes) { for (int i = 0; i < numTimes; i++) { System.out.print(c); } } In the definition of the method printRow, the variables c and numTimes are called the parameters of the method. They are placeholders for values that are passed to the method when it’s called. In other words, any time the method is to be used, the user must invoke it by passing a char and an int in brackets. This method might be called by the following line of code written in the main() method: printRow('*', 10); This will result in ten asterisk characters output on the same line. We could also invoke the printRow() method by passing expressions that evaluate to the appropriate data type. For instance, the charAt() method returns the char within a String at a specified location. String s = “Bryce”; printRow(s.charAt(0), 10); would print ‘B’ ten times to standard output. Scope of a Variable The scope of a variable describes the life of the variable (how long it remains in memory) and visibility of the variable (what parts of the program can see it). Method parameters have scope limited to the method itself. In the above example, the parameters c and numTimes are only visible to the printRow method. The main() method would not know anything about these variables! ICS 3U1 Unit 3 – Methods Name: _______________ Suppose we have a method that, among its other statements, increments the input parameter by one. It might look like the following: public static void sample (int num) { ... // other useful code num++; ... // other useful code } Suppose we were to call this method from within main(): public static void main(String[] args) { int n = 3; sample(n); } It’s important to note that the scope of n in the main method is limited to that method. The value stored in n is passed to the sample method. This value is stored in a separate variable called num. In the sample() method, num is incremented. This does not change the value of n in main()! The scopes of num and n are limited to the methods in which they’re defined. The two methods do not know about each other’s variables and therefore, they cannot change their values! Even though the method changes the value of num to 4 inside the method, the actual value of the variable n, in the calling code, is still 3. ICS 3U1 Unit 3 – Methods Name: _______________ Assignment – Passing Parameters Note: All methods from these exercise should be saved in one file called Parameters.java. You may find it useful to write a main() method for yourself to test your methods, but you do not have to include one in your submission. 1. Copy the following methods into your code. public static void swap (int m, int n) { int temp = m; m = n; n = temp; } public static void main(String[] args) { int i = 7; int j = 3; swap(i, j); System.out.println(“i = “ + i + “ and j = “ + j); } What did the fragment output? Is it what you expected? 2. Write a method that will simulate the results of rolling a fair dice n times, where n is an integer parameter passed to the method. Your method should have the following definition: public static void rollDice(int numTimes) If this method is called with: printRollDice(5); then the output might produce: Roll Roll Roll Roll Roll 1 2 3 4 5 is is is is is a a a a a 3. 6. 2. 3. 5. ICS 3U1 Unit 3 – Methods Name: _______________ 3. Complete the definition of the following method, printRect, so that it prints a filled rectangular pattern, using the character c, that is width characters wide and height characters high. public static void printRect (char c, int width, int height) For example, the following call to printRect: printRect('*', 6, 4); should produce the following output: ****** ****** ****** ******