Method Parameters Launch Task In the project we wrote loops to repeat a character a given number of times, complete the following method that takes two parameters, one string and one int, and repeats...

Fill in all grey boxes.


Method Parameters Launch Task In the project we wrote loops to repeat a character a given number of times, complete the following method that takes two parameters, one string and one int, and repeats the string the given number of times. For example, the method call repeater(“&”, 8) would produce &&&&&&&&. public static void repeater(String s, int num) { Introduction to Parameters Part of the reason that humans are so good at solving new problems is that they generalize the solution to whole categories of problems. The book gives an example of walking as a generalizable task—walking 20 steps and walking 10 steps can be described as the task of “walking forward,” and the part that varies (we call this a parameter) is the number of steps. In programming, when we make code flexible by “parameterizing” parts of the task that are likely to change, we end up with programs that are shorter, easier to understand for other coders, better organized, and reusable. Imagine how difficult life would be if you had to separately learn all the movements required to walk 10 steps down that runway in addition to the procedure for walking 20 steps it takes to get to the stage? Instead, your brain computes a general rule something like “walk only the number of steps to the next obstacle,” then (ideally) your eyes and ears input how many steps that should be. Java is the same way—it takes less memory and computing power to execute a program if you write code that is flexible/reusable with different parameters. (In fact, in this chapter we’re going to learn how to use user input—like the information we get from our eyes and ears—to influence the behavior of the programs we write!) What are some other behaviors that we “parameterize” every day?: writeSpaces() Example In the last chapter, we inserted spaces into a drawing by calling a method: writeSpaces(); If we wanted to tell method writeSpaces to output 10 spaces, we might decide to declare a variable: int number = 10; // Can anyone explain why this won't work? writeSpaces(); Instead we parameterize the number of spaces by changing the method header as highlighted below (this used to be empty parentheses, remember?) public static void writeSpaces (int number) { for (int i = 1; i <= number;="" i++)="" {="" system.out.print("="" ");="" }="" }="" now="" when="" we="" call="" the="" method,="" we="" can="" (and="" must!)="" include="" the="" parameterized="" value:="" writespaces(10);="" writespaces();="" error:="" no="" parameter="" specified="" writespaces(24901);="" writespaces(9/3-2);="" actual="" vs="" formal="" parameter="" in="" the="" following="" code,="" what="" is="" the="" actual="" parameter="" and="" what="" is="" the="" formal="" parameter?="" public="" class="" example="" {="" public="" static="" void="" main="" (string[]="" args){="" writespaces(15);="" }="" public="" static="" void="" writespaces="" (int="" number)="" {="" for="" (int="" i="1;" i=""><= number; i++) { system.out.print(" "); } } } introduction to multiple parameters using the example below, let’s review the flow of control by acting out this example . public class parameterexample { public static void main (string[] args) { int x = 17; doublenumber(x); system.out.println("x = " + x); system.out.println(); int number = 42; doublenumber(number); system.out.println("number = " + number); } public static void doublenumber (int number) { system.out.println("initial value = " + number); number = number * 2; system.out.println("final value = " + number); } } it is possible to declare multiple parameters! the trick is to always make sure your method accepts the parameters in the same order. when calling the method, pass the parameters in the same order in which they were declared. public static ( , ) { ;; …; } let’s do a coding example. let’s write a method that repeats a string a given number of times. we will call is repeater() public static void repeater(string text, int num){ //accepts 2 parameters for( int i = 0; i < num; i++){ system.out.print(text + “ “); } } now i can call the method to produce a single line of a repeated string: repeater(“foo”, 7); // prints foo 7 times repeater(“repeater”, 100); // prints repeater 100 times repeater(); // error, the method call needs parameters repeater(“text”); // error, the method call needs both parameters repeater(10); // error, the method call needs both parameters we can go even further by making a method that creates a rectangle of output if we give it 3 parameters: public static void repeater(string text, int row, int col){ //accepts 3 parameters for( int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ system.out.print(text + “ “); } system.out.println(); } } now my repeater method will accept 3 parameters and make a rectangle out of the text i want. daily work 1. write a method called printnumbers that accepts a maximum number as a parameter and prints each number from 1 up to that maximum, inclusive, boxed by square brackets. for example, consider the following calls: printnumbers(15); printnumbers(5); these calls should produce the following output: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [1] [2] [3] [4] [5] you may assume that the value passed to printnumbers is 1 or greater. 2. write a method called printpowersof2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. for example, consider the following calls: printpowersof2(3); printpowersof2(10); these calls should produce the following output: 1 2 4 8 1 2 4 8 16 32 64 128 256 512 1024 you may assume that the value passed to printpowersof2 is 0 or greater. 3. write a method called printpowersofn that accepts a base and an exponent as arguments and prints each power of the base from base0 (1) up to that maximum power, inclusive. for example, consider the following calls: printpowersofn(4, 3); printpowersofn(5, 6); printpowersofn(-2, 8); these calls should produce the following output: 1 4 16 64 1 5 25 125 625 3125 15625 1 -2 4 -8 16 -32 64 -128 256 you may assume that the exponent passed to printpowersofn has a value of 0 or greater. 4. write a method named printgrid that accepts two integer parameters rows and cols. the output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. the numbers count up from 1 to (rows x cols). the output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. assume that rows and cols are greater than 0. here are some example calls to your method and their expected results: call: printgrid(3, 6); printgrid(5, 3); printgrid(4, 1); printgrid(1, 3); output: 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 1 2 3 4 1, 2, 3 challenge problem write a method called printsquare that takes in two integer parameters, a min and a max, and prints the numbers in the range from min to max inclusive in a square pattern. the square pattern is easier to understand by example than by explanation, so take a look at the sample method calls and their resulting console output in the table below. each line of the square consists of a circular sequence of increasing integers between min and max. each line prints a different permutation of this sequence. the first line begins with min, the second line begins with min + 1, and so on. when the sequence in any line reaches max, it wraps around back to min. you may assume the caller of the method will pass a min and a max parameter such that min is less than or equal to max. call printsquare(1, 5); printsquare(3, 9); printsquare(0, 3); printsquare(5, 5); output 12345 23451 34512 45123 51234 3456789 4567893 5678934 6789345 7893456 8934567 9345678 0123 1230 2301 3012 5 return values launch task modify this method so that instead of printing the result of the math, it instead returns it as a value. public static void smallestofthree(int a, int b, int c) { int min = math.min(a,b); min = math.min(min,c); system.out.println(min); } introduction to returning values today we’re going to learn how to write methods that return a value—we already know how to get java to compute simple equations for us; now we’re going to learn how to get java to give us back those numbers so we can use them elsewhere in our program. while programmers can manipulate the parameters passed into a function, their code is operated on a copy of the value, and not the value itself. if int x is passed as an argument into an expression as the parameter int num, the function may manipulate the value stored in num. when the function returns, x will be unchanged. you may find the following analogy from stackoverflow helpful: “the procedure defines a parameter, and the calling code passes an argument to that parameter. you can think of the parameter as a parking space, and the argument as an automobile.” we’re also going to look at how to use a collection of equations that java already has written for us called the math class. these pre-made methods make doing complex equations much easier! method header of a method that returns a value here’s the syntax to write a method that number;="" i++)="" {="" system.out.print("="" ");="" }="" }="" }="" introduction="" to="" multiple="" parameters="" using="" the="" example="" below,="" let’s="" review="" the="" flow="" of="" control="" by="" acting="" out="" this="" example="" .="" public="" class="" parameterexample="" {="" public="" static="" void="" main="" (string[]="" args)="" {="" int="" x="17;" doublenumber(x);="" system.out.println("x=" + x); System.out.println(); int number = 42; doubleNumber(number); System.out.println(" number=" + number); } public static void doubleNumber (int number) { System.out.println(" initial="" value=" + number); number = number * 2; System.out.println(" final="" value=""> ( , ) { ;; …; } let’s do a coding example. let’s write a method that repeats a string a given number of times. we will call is repeater() public static void repeater(string text, int num){ //accepts 2 parameters for( int i = 0; i < num; i++){ system.out.print(text + “ “); } } now i can call the method to produce a single line of a repeated string: repeater(“foo”, 7); // prints foo 7 times repeater(“repeater”, 100); // prints repeater 100 times repeater(); // error, the method call needs parameters repeater(“text”); // error, the method call needs both parameters repeater(10); // error, the method call needs both parameters we can go even further by making a method that creates a rectangle of output if we give it 3 parameters: public static void repeater(string text, int row, int col){ //accepts 3 parameters for( int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ system.out.print(text + “ “); } system.out.println(); } } now my repeater method will accept 3 parameters and make a rectangle out of the text i want. daily work 1. write a method called printnumbers that accepts a maximum number as a parameter and prints each number from 1 up to that maximum, inclusive, boxed by square brackets. for example, consider the following calls: printnumbers(15); printnumbers(5); these calls should produce the following output: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [1] [2] [3] [4] [5] you may assume that the value passed to printnumbers is 1 or greater. 2. write a method called printpowersof2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. for example, consider the following calls: printpowersof2(3); printpowersof2(10); these calls should produce the following output: 1 2 4 8 1 2 4 8 16 32 64 128 256 512 1024 you may assume that the value passed to printpowersof2 is 0 or greater. 3. write a method called printpowersofn that accepts a base and an exponent as arguments and prints each power of the base from base0 (1) up to that maximum power, inclusive. for example, consider the following calls: printpowersofn(4, 3); printpowersofn(5, 6); printpowersofn(-2, 8); these calls should produce the following output: 1 4 16 64 1 5 25 125 625 3125 15625 1 -2 4 -8 16 -32 64 -128 256 you may assume that the exponent passed to printpowersofn has a value of 0 or greater. 4. write a method named printgrid that accepts two integer parameters rows and cols. the output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. the numbers count up from 1 to (rows x cols). the output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. assume that rows and cols are greater than 0. here are some example calls to your method and their expected results: call: printgrid(3, 6); printgrid(5, 3); printgrid(4, 1); printgrid(1, 3); output: 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 1 2 3 4 1, 2, 3 challenge problem write a method called printsquare that takes in two integer parameters, a min and a max, and prints the numbers in the range from min to max inclusive in a square pattern. the square pattern is easier to understand by example than by explanation, so take a look at the sample method calls and their resulting console output in the table below. each line of the square consists of a circular sequence of increasing integers between min and max. each line prints a different permutation of this sequence. the first line begins with min, the second line begins with min + 1, and so on. when the sequence in any line reaches max, it wraps around back to min. you may assume the caller of the method will pass a min and a max parameter such that min is less than or equal to max. call printsquare(1, 5); printsquare(3, 9); printsquare(0, 3); printsquare(5, 5); output 12345 23451 34512 45123 51234 3456789 4567893 5678934 6789345 7893456 8934567 9345678 0123 1230 2301 3012 5 return values launch task modify this method so that instead of printing the result of the math, it instead returns it as a value. public static void smallestofthree(int a, int b, int c) { int min = math.min(a,b); min = math.min(min,c); system.out.println(min); } introduction to returning values today we’re going to learn how to write methods that return a value—we already know how to get java to compute simple equations for us; now we’re going to learn how to get java to give us back those numbers so we can use them elsewhere in our program. while programmers can manipulate the parameters passed into a function, their code is operated on a copy of the value, and not the value itself. if int x is passed as an argument into an expression as the parameter int num, the function may manipulate the value stored in num. when the function returns, x will be unchanged. you may find the following analogy from stackoverflow helpful: “the procedure defines a parameter, and the calling code passes an argument to that parameter. you can think of the parameter as a parking space, and the argument as an automobile.” we’re also going to look at how to use a collection of equations that java already has written for us called the math class. these pre-made methods make doing complex equations much easier! method header of a method that returns a value here’s the syntax to write a method that>
Oct 09, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here