Static Methods - Building Java Programs | CSE 142, Study notes of Linux skills

Static Methods Material Type: Notes; Professor: Stepp; Class: COMPUTER PRGRMNG I; Subject: Computer Science and Engineering; University: University of Washington - Seattle; Term: Autumn 2009;

Typology: Study notes

Pre 2010

Uploaded on 12/14/2010

ctom
ctom 🇺🇸

1 document

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Copyright 2008 by Pearson Education
Building Java Programs
Chapter 1
Lecture 1-2: Static Methods
reading: 1.4 - 1.5
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Static Methods - Building Java Programs | CSE 142 and more Study notes Linux skills in PDF only on Docsity!

Building Java ProgramsCopyright 2008 by Pearson Education

Chapter 1Lecture 1-2: Static Methods reading: 1.4 - 1.

Copyright 2008 by Pearson Education

Comments

^ comment

: A note written in source code by theprogrammer to describe or clarify the code.  Comments are not executed when your program runs.

^ Syntax:^ //^ comment text, on one line^ or,^ /*^ comment text; may span multiple lines

^ Examples:^ // This is a one-line comment./* This is a very longmulti-line comment. */

Copyright 2008 by Pearson Education Comments example / Suzy Student, CS 101, Fall 2019This program prints lyrics about ... something. / public class BaWitDaBa {public static void main(String[] args) { // first verse System.out.println("Bawitdaba");System.out.println("da bang a dang diggy diggy");System.out.println(); // second verse System.out.println("diggy said the boogy");System.out.println("said up jump the boogy");} }

Copyright 2008 by Pearson Education

Static methods^ reading: 1.4^ self-check: 16-25exercises: #5-10videos: Ch. 1 #

Copyright 2008 by Pearson Education 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. ^ Set the timer. ^ Place the first batch of cookies into the oven. ^ Allow the cookies to bake. ^ Set the timer. ^ Place the second batch of cookies into the oven. ^ Allow the cookies to bake. ^ Mix ingredients for frosting. ^ ...

Copyright 2008 by Pearson Education Structured algorithms  structured algorithm

: Split 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....

Copyright 2008 by Pearson Education A program with redundancy public^ class BakeCookies {public^ static

void^ main(String[] args)

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."); 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."); 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."); System.out.println("Mix

ingredients^

for frosting."); System.out.println("Spread frosting

and sprinkles.");

} }

Copyright 2008 by Pearson Education

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  Writing a static method is likeadding a new command to Java.

class method A  statement  statement  statement method B  statement  statement method C  statement  statement  statement

Copyright 2008 by Pearson Education Design of an algorithm // This program displays a delicious recipe

for baking^ cookies.

public^ class BakeCookies2 {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). 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. System.out.println("Mix

ingredients^

for frosting."); System.out.println("Spread frosting

and sprinkles.");

} }

Copyright 2008 by Pearson Education

Gives your method a name so it can be executed  Syntax: public static void

name() {

Declaring a method statement; statement; ... statement; }  Example: public static void printWarning() { System.out.println("This product causes cancer");System.out.println("in lab rats and humans."); }

Copyright 2008 by Pearson Education 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^ favorite song. public^ static

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-downNow this is^ the story

all about^ how My life got^ flipped turned upside-down

Copyright 2008 by Pearson Education 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^ 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

a batch of^ cookies into the

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.");

} }

Copyright 2008 by Pearson Education

^ 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)^ {
message1();message2(); System.out.println("Done
with^ main.");

public static void message1()

{ System.out.println("This

is message1."); } public static void message2()

{ System.out.println("This

is message2."); message1(); System.out.println("Done

with^ message2."); } public static void message1()

{ System.out.println("This

is message1.");

Control flow }

Copyright 2008 by Pearson Education When to use methods  Place statements into a static method if:  The statements are related structurally, and/or  The statements are repeated.  You should not create static methods for:  An individual^ println

statement.

^ Only blank lines. (Put blank

printlns in

main.)

^ Unrelated or weakly related statements.(Consider splitting them into two smaller methods.)