02 Ch01 2 Methods
02 Ch01 2 Methods
Comments
comment: A note written in source code by the
Syntax:
// comment text, on one line or, /* comment text; may span multiple lines */
Comments example
/* Suzy Student, CSE 142, Fall 2019 Displays lyrics*/ public class Lyrics { public static void main(String[] args) { // first line System.out.println("When I first got into magic"); System.out.println("it was an underground phenomenon"); System.out.println(); // second line System.out.println("Now everybody's like"); System.out.println("pick a card, any card"); } }
Static methods
reading: 1.4
Algorithms
algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" 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 for 10 minutes. Place the cookies into the oven. Allow the cookies to bake. Spread frosting and sprinkles onto the cookies. ...
6
Structured algorithms
structured algorithm: Split into coherent tasks.
1 Make the 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 for 10 minutes. Place the cookies into the oven. Allow the cookies to bake. 3 Decorate the cookies. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. ...
Copyright 2010 by Pearson Education
Removing redundancy
A well-structured algorithm can describe repeated tasks
1 Make the cookie batter. Mix the dry ingredients. ... 2a Bake the cookies (first batch). Set the oven temperature. Set the timer for 10 minutes. ... 2b Bake the cookies (second batch). Repeat Step 2a 3 Decorate the cookies. ...
Copyright 2010 by Pearson Education
Static methods
static method: A named group of statements.
class method A statement statement statement method B statement statement method C statement statement statement
10
procedural decomposition:
Declaring a method
Gives your method a name so it can be executed
Syntax:
public static void name() { statement; statement; ... statement; }
Example:
public static void printWarning() {
System.out.println("This product causes cancer"); System.out.println("in lab rats and humans.");
}
Copyright 2010 by Pearson Education
11
Calling a method
Executes the method's code
Syntax:
name();
You can call the same method many times if you like.
Example:
printWarning();
Output:
12
13
Output:
Now this is the story all about how My life got flipped turned upside-down Now this is the story all about how My life got flipped turned upside-down
Copyright 2010 by Pearson Education
14
Output:
This This This Done Done
15
Control flow
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) { public static void message1() { message1(); System.out.println("This is message1.");
}
message2();
17
Development strategy
______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ / \ / \ | STOP | \ / \______/ ______ / \ / \ +--------+
20
Create an empty program and main method. Copy the expected output into it, surrounding each line with System.out.println syntax. Run it to verify the output.
Program version 1
public class Figures1 { public static void main(String[] args) { System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println(); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println("+--------+"); System.out.println(); System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("| STOP |"); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println(); System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("+--------+"); } }
Copyright 2010 by Pearson Education
21
Development strategy 2
______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ / \ / \ | STOP | \ / \______/ ______ / \ / \ +--------+
22
Identify the structure of the output. Divide the main method into static methods based on this structure.
Output structure
______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ / \ / \ | STOP | \ / \______/ ______ / \ / \ +--------+
The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by methods: egg teaCup stopSign hat
23
Program version 2
public class Figures2 { public static void main(String[] args) { egg(); teaCup(); stopSign(); hat(); } public static void egg() { System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println(); } public static void teaCup() { System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println("+--------+"); System.out.println(); } ...
24
25
Development strategy 3
______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ / \ / \ | STOP | \ / \______/ ______ / \ / \ +--------+
26
Identify redundancy in the output, and create methods to eliminate as much as possible. Add comments to the program.
Output redundancy
______ / \ / \ \ / \______/ \ / \______/ +--------+ ______ / \ / \ | STOP | \ / \______/ ______ / \ / \ +--------+
reused on stop sign, hat reused on teacup, stop sign used on teacup, hat
27
Program version 3
// Suzy Student, CSE 138, Spring 2094 // Prints several figures, with methods for structure and redundancy. public class Figures3 { public static void main(String[] args) { egg(); teaCup(); stopSign(); hat(); } // Draws the top half of an an egg figure. public static void eggTop() { System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); } // Draws the bottom half of an egg figure. public static void eggBottom() { System.out.println("\\ /"); System.out.println(" \\______/"); } // Draws a complete egg figure. public static void egg() { eggTop(); eggBottom(); System.out.println(); } 28 ...
Copyright 2010 by Pearson Education
29