14
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.

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

Embed Size (px)

Citation preview

Page 1: 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

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.

Page 2: 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

Page 3: 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

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.

Page 4: 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

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.• …

Page 5: 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

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.

Page 6: 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

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

Page 7: 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

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

Page 8: 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

Steps to creating static methods

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

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

Page 9: 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

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();

Page 10: 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

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("+--------+");}

Page 11: 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

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

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

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

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

Page 12: 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

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();}

Page 13: 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

Questions?

Page 14: 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

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