0% found this document useful (0 votes)
173 views

Algorithms: 8 Algorithm: A List of Steps For Solving A Problem. 8 Example Algorithm: "Bake Cookies"

The program calls method1() twice. Method1() prints 2 lines and calls method2() which prints 4 lines. So the total output is 2 * 2 + 4 = 8 lines. The answer is C.

Uploaded by

Judith Nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views

Algorithms: 8 Algorithm: A List of Steps For Solving A Problem. 8 Example Algorithm: "Bake Cookies"

The program calls method1() twice. Method1() prints 2 lines and calls method2() which prints 4 lines. So the total output is 2 * 2 + 4 = 8 lines. The answer is C.

Uploaded by

Judith Nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Algorithms

8 algorithm: A list of steps for solving a problem.


8 Example algorithm: "Bake 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.
– Mix ingredients for frosting.
– ... 1
Problems with algorithms
8 lack of structure: Many small steps; tough to
remember.

8 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.
– Set the timer.
– Place the first batch of cookies into the oven.
– Allow the cookies to bake.
– Set the oven temperature.
– Set the timer.
– Place the second batch of cookies into the oven.
– Allow the cookies to bake.
– Mix ingredients for frosting. 2
– ...
Structured algorithms
8 structured algorithm:
Split solution into coherent tasks.
1 Make the cookie batter.
– Mix the dry ingredients.
– Cream the butter and sugar.
– Beat in the eggs.
– Stir in the dry ingredients.

2 Bake the cookies.


– Set the oven temperature.
– Set the timer.
– Place the cookies into the oven.
– Allow the cookies to bake.

3 Add frosting and sprinkles.


– Mix the ingredients for the frosting.
– Spread frosting and sprinkles onto the cookies. 3

Removing redundancy
8 A well-structured algorithm can describe
repeated tasks with less redundancy.
1 Make the cookie batter.
– Mix the dry ingredients.
– ...

2a Bake the cookies (first batch).


– Set the oven temperature.
– Set the timer.
– ...

2b Bake the cookies (second batch).

3 Decorate the cookies. 4


Methods
8 static method: A named group of statements.
– denotes the structure of a program
class
– eliminates redundancy by code reuse method A
statement
statement
8 procedural decomposition: statement

dividing a problem into methods method B


– a way to manage complexity statement
statement

method C
8 Writing a method is like statement
statement
adding a new command to Java. statement
5
Using methods
1. Design the algorithm.
– Look at the structure, and which commands are repeated.
– Decide what are the important overall tasks.
– Good programmers do this BEFORE writing any code

2. Declare (write down) the methods.


– Arrange statements into groups and give each group a name.

3. Call (run) the methods.


– The program's main method executes the other methods to
perform the overall task.
6
Declaring a method
Gives your method a name so it can be
executed
8 Syntax:
public void <name>()
8 {
your code here!!
8
}

8 Example:
public void printWarning()
8 {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans."); 7
Calling a method
Executes the method's code
8 Syntax:
<name>();

– You can call the same method many times if you like.

8 Example:
printWarning();

– Output:
This product causes cancer
in lab rats and humans.
8
Program with static method
public class FreshPrince
{
public static void main(String[] args)
{
rap(); // Calling (running) the rap method
System.out.println();
rap(); // Calling the rap method again
}

// This method prints the lyrics to my favourite song.


public void rap()
{
System.out.println("Now this is the story all about how");
System.out.println("My life got flipped turned upside-down");
}
}

Output:
Now this is the story all about how
My life got flipped turned upside-down

Now this is the story all about how


9
My life got flipped turned upside-down
Final cookie program!
// This program displays a delicious recipe for baking cookies.
public class BakeCookies3
{
public static void main(String[] args)
{
makeBatter();
bake(); // 1st batch
bake(); // 2nd batch
decorate();
}
// Step 1: Make the cake batter.
public void makeBatter()
{
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
}
// Step 2: Bake a batch of cookies.
public void bake()
{
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
}
// Step 3: Decorate the cookies.
public void decorate()
{
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles."); 10
}
}
Methods calling methods
public class MethodsExample
{
public void main(String[] args)
{
message1();
message2();
System.out.println("Done with main.");
}
public void message1()
{
System.out.println("This is message1.");
}
public void message2()
{
System.out.println("This is message2.");
message1();
System.out.println("Done with message2.");
}
}

8 Output:
This is message1.
This is message2.
This is message1.
Done with message2.
Done with main.
11
Control flow
8 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.

public class MethodsExample {


public static void main(String[] args) {
public static void message1() {
message1(); System.out.println("This is message1.");
}
message2();
public static void message2() {
System.out.println("This is message2.");
message1();
System.out.println("Done with main.");
System.out.println("Done with message2.");
} }

public static void message1() {


...
System.out.println("This is message1.");
} }
12
When NOT to use methods
8 You should not create static methods for:
– Only blank lines. (Put blank printlns in main.)
– Unrelated or weakly related statements.
(Consider splitting them into two smaller
methods.)

13
8 How many lines of output does the following program produce?

A. 3 B. 4
C. 8 D. 12
E. 20
8 ANSWER

A. 3 B. 4 C. 8 D. 12 E. 20

You might also like