no6.e10a.2017.pdf - Parameterized Recipe Recipe for baking 20 cookies Mix the following ingredients in a bowl 4 cups flour 1 cup butter Recipe for

no6.e10a.2017.pdf - Parameterized Recipe Recipe for baking...

This preview shows page 1 - 5 out of 13 pages.

49 Parameterized Recipe Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups flour 1 cup butter ... Recipe for baking N cookies: Mix the following ingredients in a bowl: N/5 cups flour N/20 cups butter N/20 cups sugar N/10 eggs 2N bags chocolate chips ... Place on sheet and Bake for about 10 minutes. 50 Parameters Up to now we have defined only parameterless methods, though we have used built-in methods that have parameters. Consider a Scratch 2.0 example: formal parameter actual argument 51 Parameters in Java Methods A method can accept multiple parameters (separated by , ) When calling it, you must pass an actual value for each parameter. Declaration : static void methodName ( type 1 param 1 , ... , type n param n ) { Java statement[s] } Call : methodName (expr 1 , expr 2 , ... , expr n );
Image of page 1
52 Multiple Parameters Example public static void main (String [] args) { printNumber (4, 9); printNumber (17, 6); printNumber (8, 0); printNumber (0, 8); } // what output does this program produce? static void printNumber (int number, int count) { for (int i = 1; i <= count; i++) { System.out.print(number); } System.out.println(); } 53 A "Parameter Mystery" Problem class ParameterMystery { public static void main(String[] args) { int x = 9; int y = 2; int z = 5; mystery (z, y, x); mystery (y, x, z); } static void mystery (int x, int z, int y) { System.out.println (z + " and " + (y - x)); } } 54 Song Generation How to print the words for "The Ants Came Marching” by observing a basic pattern: The ants came marching 1 by 1, hurrah! Hurrah! The ants came marching 1 by 1, hurrah! Hurrah! The ants came marching 1 by 1, The little one stopped to suck his thumb. And they all go marching down. To the ground. To get out of the rain! (Boom, boom, boom ... ) The ants came marching 2 by 2, hurrah! Hurrah! The ants came marching 2 by 2, hurrah! Hurrah! The ants came marching 2 by 2, The little one stopped to tie his shoe. And they all go marching down. To the ground. To get out of the rain! (Boom, boom, boom ... )
Image of page 2
55 Song Generation, continued public static void main (String [] args) { verse (1, "suck his thumb.\n"); verse (2, "tie his shoe, \n"); verse (3, "climb a tree, \n"); } // see MarchingAnts.java 56 The boolean Data Type The boolean data type is named after the mathematician George Boole (1815-64), a pioneer in the study of formal logic. Some simple examples of boolean expressions (b*b - 4.0*a*c) >= 0.0 // non-neg discrim. (year % 100) == 0 // beginning of a century One can also declare and use boolean variables and parameters. 57 Lines and Boxes Example Consider the following main method: public static void main( String [] args) { line (13, '*', true); line (7, '/', false); line (35, '%', true); System.out.println(); box (10, 3, '*'); box (5, 4, '='); box (20, 7, '$'); } // see Boxes.java
Image of page 3
58 Exchange Value of 2 Variables Intended effect final value of a becomes the initial value of b final value of b becomes the initial value of a Tempting, but wrong: a = b; // now a is ???
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 13 pages?

  • Fall '17
  • HenryH.Leitner

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes