Warm Up As you enter get out a half sheet of loose paper. Write the HelloWorld program on it. Be...

Preview:

Citation preview

Warm Up

As you enter get out a half sheet of loose paper. Write the HelloWorld program on it. Be prepared to correct your code.

INTRODUCTION TO JAVA PROGRAMMING, CHAPTER 1Procedural decomposition

At the end of this class, you will be able to • Define subtasks for multistep problems.• Declare and call static methods to eliminate redundancy in your programs.• Examine the control flow of programs which use static methods.

Algorithms Revisited• Making cookies• Mix dry ingredients.• Cream butter and sugar.• Beat in eggs.• Stir dry ingredients into wet.• Set the oven to 400• Put the cookies into the oven.• Bake for 10 minutes• Remove the cookies from the oven.• Mix ingredients for frosting.• Frost the cookies.

• Making a double batch of cookies• …• Set the oven to 400• Put the first batch of cookies into the

oven.• Bake for 10 minutes.• Remove the first batch of cookies from

the oven.• Bake for 10 minutes• Remove the second batch of cookies

from the oven.• …

Algorithms Revisited• Making cookies• Make the batter• Bake the cookies• Frost the cookies

• Making a double batch of cookies• Make the double batch of batter• Bake the first batch of cookies• Bake the second batch of cookies• Frost the first batch of cookies• Frost the second batch of cookies

• Bake the cookies• Set the oven to 400• Make the batter• Bake the cookies• Remove the cookies from

the oven.

Procedural Decomposition• Decomposition is the separation of a problem into discernible parts.• Each part should be simpler than the whole.

Static methods• A block of Java statements that is given a name.• Break a program into several static methods that each solve a piece of the overall problem.

public static void <methodName>() { <statement>; <statement>; <statement>; …}

Syntax Yoda

Steps to creating static methods

• Problem: Write a program that produces the following output:+--------+| || |+--------+

+--------+| || |+--------+

Steps to creating static methods: Design, Define, Call

1. Design the method: A method that draws a box.

2. Define the method:

public static void drawBox() { System.out.println("+--------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+--------+");}

3. Call the method:

drawBox();System.out.println();drawBox();

Flow of control• The order in which the statements of a Java program are executed.

public static void main (String args[]) { drawBox();

System.out.println();

drawBox();}

public static void drawBox() { System.out.println("+--------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+--------+");}

Think, Pair, Share…Describe what methods would be useful for writing a program that produces the following output:

+----+\ / \ / \/

\ / \ / \/ /\ / \/ \

/\ / \/ \\ / \ / \/

Methods that call other methodspublic static void bar() { foo(); System.out.println(”bar"); foo();}

public static void foo() { System.out.println(”foo");}

public static void main(String args[]) { foo(); bar();}

Questions?

Homework• Read 1.4• Self Check 1.17• Exercise 1.3, 1.8

Recommended