Static Methods and Method Calls

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Advertisements

Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs Chapter 1 Introduction to Java Programming.
Java Programs + Algorithms
Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS.
Basic Java programs with println statements. 2 Compile/run a program 1.Write it –code or source code: the set of instructions in a program 2.Compile it.
1 Procedural decomposition using static methods suggested reading:1.4.
1 Procedural decomposition using static methods. 2 Algorithms Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to.
Building Java Programs Chapter 1 Introduction to Java Programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods, Avoiding Redundancy reading: self-check:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1: Introduction to Java Programming.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Topic 3 static Methods and Structured Programming "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Static methods. 2 Algorithms algorithm: a list of steps for solving a problem Example algorithm: "Bake sugar cookies" –Mix the dry ingredients. –Cream.
BUILDING JAVA PROGRAMS CHAPTER 1 INTRODUCTION TO JAVA PROGRAMMING.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 CSE 142 Lecture Notes Introduction These lecture notes are copyright (C) Marty Stepp May not be rehosted, copied, sold, or modified without Marty.
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.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.
Building Java Programs Chapter 1 Introduction to Java Programming.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
Introduction to Java.
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CSc 110, Autumn 2016 Lecture 2: Functions. Review What is the output of the following print statements? print("this class\tis' the \"best\"") Write a.
Lecture 1: Introduction to Java Programming
Lecture 1: Basic Java Syntax
Building Java Programs
CSc 110, Autumn 2017 Lecture 2: Functions.
Building Java Programs Chapter 1
Building Java Programs
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
AP Computer Science Mr. Wortzman.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Topic 3 static Methods and Structured Programming
Building Java Programs
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
Lecture 1: Basic Java Syntax
Building Java Programs
CSc 110, Spring 2018 Lecture 3: Functions.
Building Static Methods
Building Java Programs
CSc 110, Spring 2018 Lecture 2: Functions.
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 1
Building Java Programs
Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
host: benson limketkai University of Washington, Spring 2007
Building Java Programs
Presentation transcript:

Static Methods and Method Calls Things to go over in 1.05: Method information and breakdown. How to Vocabulary Examples

Algorithms Algorithm: A list of steps for solving a problem. Example Algorithm: bakeSugarCookies() 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 the frosting and sprinkles onto the cookies. , etc.

Problems with Algorithms Lack of structure: Many tiny steps; tough to remember each step Redundancy: Consider making a double batch … 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 Place the second batch of cookies into the oven ….

Removing Redundancy A well-structured algorithm can describe repeated tasks with less redundancy Make the cookie batter. Mix in the dry ingredients. … Bake the cookies (first batch) Set the oven temperature. Set the timer. 2a. Bake the cookies (second batch) Decorate the cookies. By grouping steps and calling the groups, we can eliminate redundancy.

Structure Diagram Many batches of cookies Make a batch of cookies Allows you to divide and conquer Many batches of cookies Make a batch of cookies Make the batter Bake the cookies Decorate the cookies … … 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.");

Static Methods Static method: a named group of statements Procedural decomposition: dividing a problem into methods Writing a static method is like adding a new command to Java

Using Static Methods Define / Declare the method Call (or run) the method *Insider Tip* The main method always runs first

Defining and Declaring a Method Giving your method a name so it can be executed: Syntax: Example: public static void name(){ public static void makeBatter(){ statement; System.out.println(“Mix the dry ingredients.”); statement; System.out.println(“Cream the butter/sugar.”); … System.out.println(“Beat in the eggs.”); statement; System.out.println(“Stir in dry ingredients.”) } }

Calling Static Methods Executes the method’s code Syntax : <name>() Example: makeBatter() Output: Mix the dry ingredients. Cream the butter/sugar Beat in the eggs Stir in dry ingredients This whole block of code is called every time [ makeBatter()] is called.

// This program displays a delicious recipe for baking cookies. public static void main(String[] args) { // Step 1: Make the cake batter. 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 2a: Bake cookies (first batch). 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 2b: Bake cookies (second batch). // Step 3: Decorate the cookies. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }

This affords you a lot of new capabilities. // 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 static 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 static void bake() System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place batch into oven."); System.out.println("Allow the cookies to bake."); // Step 3: Decorate the cookies. public static void decorate() System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); This affords you a lot of new capabilities.

Practice-It Complete the following practice-it questions: Tricky Strange Confusing Lots-of-Errors

Homework Read chapter 1 section 5 (1.5) (p. 40-46) Do chapter 1 exercises 11, 12, 14, and 16. (p. 56-58)