# Short Parameter Introduction Pepper. Repeat, but just a little differently Print 1-10, then 1-12, then 1-5 for (int count = 1; count <=?) { System.out.println(count);

## Presentation on theme: "Short Parameter Introduction Pepper. Repeat, but just a little differently Print 1-10, then 1-12, then 1-5 for (int count = 1; count <=?) { System.out.println(count);"— Presentation transcript:

Short Parameter Introduction Pepper

Repeat, but just a little differently Print 1-10, then 1-12, then 1-5 for (int count = 1; count <=?) { System.out.println(count); }

Teach method to get information Make a method that knows how to take information from its caller public static void myNumbers (int stop) { for (int count = 1; count <= stop; count = count + 1) { System.out.print(count); } Take In info Use Info

Call that Method – Give Info Now call the method, giving that info public static void main() { int x = 2; System.out.println(" I will print the first series"); myNumbers(10); System.out.println(" I will print another series"); myNumbers(5 + 7); System.out.println(" I will print a final series"); myNumbers(x); } Give info as literal Give Info as Expression Give Info as variable

Computer Steps Caller sends info; method opens info and runs all steps; next step in caller public static void main() { int x = 5; System.out.println(" I will print the first series"); myNumbers(2); System.out.println(" I will print another series"); myNumbers(7 + 2); System.out.println(" I will print a final series"); myNumbers(x); } public static void myNumbers (int stop) { for (int count = 1; count <= stop; count = count + 1) { System.out.print(count); }

Teach method to get 2 Pieces Make a method that knows how to take information from its caller public static void myNumbers (int stop, int start) { for (int count = start ; count <= stop; count = count + 1) { System.out.print(count); } Call it with 2 parameters: myNumbers(10,2); Take In info Use Info

Teach method to Return info Make a method that knows how to give back to the caller public static int myNumbers (int stop, int start) { int sum = 0; for (int count = start ; count <= stop; count = count + 1) { System.out.print(count); sum = sum + count; } return sum; } Take in the information: int mysum; Mysum = myNumbers(10,2); Define return Return Info Main method uses the info

Parameters with Pictures Put many smiley faces on your background, but the eye color should vary – Make a method that makes a smiley – Teach it to take in the eye color parameter – Make a main method that puts smileys all over the background

Remember Pictures Add the picture drawing knowledge import javalib.worldimages.*; import java.awt.Color; Make some shapes: WorldImage myCircle = AImage.makeCircle(50,Color.blue, Mode.filled); Make your background: WorldImage myWorld = AImage.makeRectangle(500,500,Color.yellow, Mode.filled); Place the shapes on the background: myWorld = myWorld.place(myCircle,30,20); Show the picture myWorld.show();

Smiley method Make one open circle for the face WorldImage face = AImage.makeCircle(15,Color.yellow, Mode.outlined); Make one open circle for the mouth WorldImage mouth = AImage.makeCircle(4,Color.red, Mode.outlined); Make one closed circle for the eye WorldImage eye = AImage.makeCircle(4,Color.green, Mode.filled); Place the mouth and 2 eyes on the face WorldImage smile = face.place(mouth,15,21); smile = smile.place(eye,10,10); smile = smile.place(eye,20,10);

Teach Smile to Take In Eye Color Make the color of the eye a parameter public static void smile (Color eyeColor){ WorldImage face = AImage.makeCircle(15,Color.yellow, Mode.outlined); //'Make one open circle for the mouth WorldImage mouth = AImage.makeCircle(4,Color.red, Mode.outlined); //'Make one closed circle for the eye WorldImage eye = AImage.makeCircle(4,eyeColor, Mode.filled); //'Place the mouth and 2 eyes on the face WorldImage smile = face.place(mouth,15,21); smile = smile.place(eye,10,10); smile = smile.place(eye,20,10); smile.show(); }

Teach Smile to return a picture Return a picture instead of showing it public static WorldImage smile (Color eyeColor){ WorldImage face = AImage.makeCircle(15,Color.yellow, Mode.outlined); //'Make one open circle for the mouth WorldImage mouth = AImage.makeCircle(4,Color.red, Mode.outlined); //'Make one closed circle for the eye WorldImage eye = AImage.makeCircle(4,eyeColor, Mode.filled); //'Place the mouth and 2 eyes on the face WorldImage smile = face.place(mouth,15,21); smile = smile.place(eye,10,10); smile = smile.place(eye,20,10); return smile; }

Paint a bunch of smiles Build your main method with a background Place lots of smiles by calling smile method, sending it different eye colors: public static void main ( ) { WorldImage back = AImage.makeRectangle(500,500); back = back.place(smile(Color.blue),30,40); back = back.place(smile(Color.green),50,150); back = back.place(smile(Color.black),100,40); back.show(); }

Parameter Rules Define a method: – Method header public static keywords Return type (void means return nothing) Method name (lower case start) Parameter list inside parentheses (declare variables, comma separated) – Method content Use the parameter as you would a local variable Return the correct type (no return if nothing) Call a method: – Method name – Comma separated values inside parentheses Do not declare variables here

Summary Defining methods: – Why add a parameter to a method – How to add a parameter to a method – Why make a method return a variable – How to return a variable from a method Calling methods: – How to send parameter values to a method – How to receive a value from a method

Download ppt "Short Parameter Introduction Pepper. Repeat, but just a little differently Print 1-10, then 1-12, then 1-5 for (int count = 1; count <=?) { System.out.println(count);"

Similar presentations