5. WHAT IS AN ALGORITHM?
• algorithm: A list of steps for solving a problem.
• Example: "Bake sugar cookies"
• Mix the dry ingredients.
• Cream the butter and sugar.
• Beat in the eggs.
• Stir in the dry ingredients.
• Set the oven temperature.
• Set the timer.
• Place the cookies into the oven.
• Allow the cookies to bake.
• Spread frosting and sprinkles onto the cookies.
6. PROBLEMS WITH ALGORITHMS
• Lack of structure: Many tiny steps; tough to remember.
• Redundancy: Consider making a double batch...
• Mix the dry ingredients.
• Cream the butter and sugar.
• Beat in the eggs.
• Stir in the dry ingredients.
• Set the oven temperature.
• ...
7.
8.
9. STATIC METHODS
• Static Method: A named group of statements.
• Denotes the structure of a program
• Eliminates redundancy by code reuse
• Procedural Decomposition: dividing a problem into methods
10. HOW TO USE STATIC METHODS
1. Design the Algorithm
• Which commands are repeated?
2. Declare (type) the methods
• Organize the repeated statements into groups
3. Call (run) the methods
• Main method will execute the methods to perform the overall task
13. HOW DO METHODS WORK (IN THE CODE)?
• When a method is called, the program's execution...
• "jumps" into that method, executing its statements, then…
• "jumps" back to the point where the method was called.
19. REDUNDANT RECIPES
• Recipe for baking 20 cookies:
• Mix the following ingredients in a bowl:
• 4 cups flour
• 1 cup butter
• 1 cup sugar
• 2 eggs
• 1 bag chocolate chips .
• .. Place on sheet and Bake for about 10
minutes.
• Recipe for baking 40 cookies:
• Mix the following ingredients in a bowl:
• 8 cups flour
• 2 cup butter
• 2 cup sugar
• 4 eggs
• 2 bag chocolate chips .
• .. Place on sheet and Bake for about 10
minutes.
20. PARAMETERIZED RECIPE
• 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
• N/20 bags chocolate chips
• ... Place on sheet and Bake for about 10 minutes.
• parameter: A value that distinguishes similar tasks.
21. REDUNDANT FIGURES
• Consider the task of printing the following lines/boxes:
*************
*******
***********************************
**********
* *
**********
*****
* *
* *
*****
22. PARAMETERIZATION
• parameter: A value passed to a method by its caller.
• Instead of lineOf7(), lineOf13(), write line to draw any length.
• When declaring the method,
• We will state that it requires a parameter for the number of stars.
• When calling the method, we will specify how many stars to draw.
23. DECLARING A PARAMETER
• Stating that a method requires a parameter in order to run
public static void name (type name) {
statement(s);
}
• Example:
public static void sayPassword(int code) {
Console.WriteLine("The password is: " + code);
}
• When sayPassword is called, the caller must specify the integer code to print.
24. PASSING PARAMETERS
• Calling a method and specifying values for its parameters
name (expression);
• Example:
public static void main(String[] args) {
sayPassword(42);
sayPassword(12345);
}
• Output:
The password is 42
The password is 12345
25. public static void chant(int repeat) {
Console.WriteLine(“I am chanting this sentence ” + repeat + “ times”);
}
26. COMMON ERRORS
• If a method accepts a parameter, it is illegal to call it without passing any value for
that parameter.
chant(); // ERROR: parameter value required
• The value passed to a method must be of the correct type.
chant(3.7); // ERROR: must be of type int
27. MULTIPLE PARAMETERS
• A method can accept multiple parameters. (separate by , )
• When calling it, you must pass values for each parameter.
• Declaration:
public static void name (type name, ..., type name) {
statement(s);
}
• Call:
methodName (value, value, ..., value);
31. DISCUSSION
• Why is this important?
• How can this be useful to me?
• How can I use this in my current
job?
• How can I use this in my future
career?
• What will I get out of this?
• How is this used in the real world
Regroup and discuss after…