
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Copyright 2008 by Pearson Education
Comments
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
Copyright 2008 by Pearson Education Structured algorithms structured algorithm
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
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
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");
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
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