![Page 1: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/1.jpg)
CS 112 Introduction to Programming
Lecture 3: Java Methods
Yang (Richard) Yang
Computer Science DepartmentYale University
308A Watson, Phone: 432-6400Email: [email protected]
![Page 2: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/2.jpg)
2
Outline
Admin. and recap Java methods
![Page 3: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/3.jpg)
Admin
Practice Slides at the end of slides for Lecture 2
Exam 1 and Exam 2 will be scheduled around 1/3 and 2/3 of course
Office hours posted on the help page Preference on time and location of office hours?
PS1 Please check PS1 rubric before submission Please use Piazza or cs112ta@cs to ask questions You have 9 discretionary late days across the semester, but can use at most 3 days per PSET
3
![Page 4: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/4.jpg)
Recap: Java Programming Steps
4
Programming in Java consists of three tasks edit java source code (.java files) compile java source code to generate bytecode (.class files)
execute/run/test bytecode using an interpreter
runoutput
source code compile
byte code
![Page 5: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/5.jpg)
Recap: Top-Down Java Syntax Structure
public class <class name> { public static void main(String[] args) {
<statement>;; <statement>;; ...... <statement>;;
}}
A class:- has a name, defined in a file with same nameConvention we follow: capitalize each English word
- starts with {, and ends with }- includes a group of methods
statement: - a command to be executed- end with ;
A method:- has a nameConvention we follow: lowercase first word, capital following
- starts with {, and ends with }- includes a group of statements
![Page 6: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/6.jpg)
Recap: The System.out.println Statement Two ways to use the statement:
• System.out.println(“string”);• You may need to use escape sequences in strings
• System.out.println();
A related statement is System.out.print(“string”); It does not print a newline
6
![Page 7: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/7.jpg)
7
Java Syntax: A Bottom-Up Look Basic Java syntax units
o white space and commentso identifiers (words)o symbols: { } “ ( ) < > [ ] ; = …o stringso numbers
// A longer program public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); }}
![Page 8: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/8.jpg)
Out of Class Read Slides
![Page 9: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/9.jpg)
9
Syntax: White Space White space
includes spaces, new line characters, tabs
white space is used to separate other entities
extra white space is ignored
White space allows a Java program to be formatted in many ways, and should be formatted to enhance readabilityo the usage of white space forms part of programming style
![Page 10: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/10.jpg)
10
Syntax: Comments comment: A note written in source code by the programmer to describe or clarify the code.o Comments are ignored by the compilero Useful for other people (and
yourself!) to understand your code
Two types of comments in Java• single-line comments use //… // this comment runs to the end of the line
• multi-lines comments use /* … */
/* this is a very long multi-line comment */
![Page 11: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/11.jpg)
Syntax: Identifier
Identifier: A name given to an item in your program.
Syntax requirement on identifier: must start with a letter or _ or $ subsequent characters can be any of those or a number
Important: Java is case sensitive: Hello and hello are different identifiers
11
![Page 12: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/12.jpg)
12
Three Types of Identifiers
1. Identifiers chosen by ourselves when writing a program (such as HelloWorld)
2. Identifiers chosen by another programmer, so we use the identifiers that they chose (e.g., System, out, println, main)public class HelloWorld
{ public static void main(String[] args) { System.out.println(“Hello World!”); }}
![Page 13: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/13.jpg)
13
Three Types of Identifiers3. Special identifiers called keywords: A keyword has a reserved meaning in Java.
Java reserved words: they are all lowercase!
abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized
![Page 14: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/14.jpg)
Examples
Which of the following are legal non reserved-word identifiers? Greeting1 g class 101dalmatians Hello, World <greeting>
14
![Page 15: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/15.jpg)
End Out of Class Read Slides
![Page 16: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/16.jpg)
Compile/Syntax Errors
A syntax/compile error: A problem in the structure of a program that causes the compiler to fail, e.g., Missing semicolon Too many or too few { } braces Class and file names do not match …
16
![Page 17: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/17.jpg)
Syntax Error: Example
1 public class Hello {2 pooblic static void main(String[] args) {3 System.owt.println("Hello, world!")_ 4 }5 }
![Page 18: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/18.jpg)
Syntax Error: Example
1 public class Hello {2 pooblic static void main(String[] args) {3 System.owt.println("Hello, world!")_ 4 }5 }
Compiler output: Hello.java:2: <identifier> expected pooblic static void main(String[] args) { ^ Hello.java:3: ';' expected } ^ 2 errors
The compiler shows the line number where it found the error.
The error messages sometimes can be tough to understand:• Why can’t the computer just say “You misspelled ‘public’”?• Since the computer knows that a “;” is missing, can’t it just fix it??
![Page 19: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/19.jpg)
Lesson in syntax errors
Compilers can’t (DO not) read minds.
Compilers don’t make mistakes.
If the program is not doing what you want, do NOT blame the computer---it’s YOU who made a mistake.
![Page 20: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/20.jpg)
20
Java Programming Steps and Errors
Compile-time errors the compiler may find problems with syntax and other basic issues
if compile-time errors exist, an executable version of the program is not created
Run-time errors a problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (crash)
Logical errors a program may run, but produce incorrect results
![Page 21: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/21.jpg)
21
Outline
Admin. And recap Java methods
Motivation: why methods?
![Page 22: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/22.jpg)
Algorithms
Algorithm: A list of steps for solving a problem.
An example algorithm (recipe): "Bake sugar cookies”
![Page 23: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/23.jpg)
An Example Algorithm Spec: "Bake two batches of sugar cookies"
1. Preheat oven temperature to 375F.
2. Mix the dry ingredients.
3. Cream the butter and sugar.
4. Beat in the eggs.
5. Stir in the dry ingredients.
6. Set the timer for 8 min.
7. Place 1st batch of cookies to oven.
8. Allow the cookies to bake.
9. Set the timer for 8 min.
10. Place 2nd batch of cookies to oven.
11. Allow the cookies to bake.
12. Mix ingredients for frosting.
13. Spread frosting and sprinkles.Readability of the
specification?
![Page 24: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/24.jpg)
Problem 1: Lack of Structure Lack of structure: Many tiny steps; tough to remember. A human being typically can only manage seven (plus or minus 2) pieces of information at one time
http://www.michaeljemery.com/nlp/your-conscious-minds-capacity-seven-plus-or-minus-two-chunks-of-information/
![Page 25: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/25.jpg)
Problem 2: Redundancy
Redundancy: unnecessary repeat
1. Preheat oven temperature to 375F.2. Mix the dry ingredients.3. Cream the butter and sugar.4. Beat in the eggs.5. Stir in the dry ingredients.6. Set the timer for 8 min.7. Place the first batch of cookies into the oven.8. Allow the cookies to bake.9. Set the timer for 8 min.10.Place the second batch of cookies into the oven.11.Allow the cookies to bake.12.Mix ingredients for frosting.13.Spread frosting and sprinkles.
![Page 26: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/26.jpg)
Fix: Structured Algorithms Structured algorithm: Split into coherent tasks.1 Preheat oven. Set oven to 375 degrees
2 Make the cookie batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients.
3 Bake the cookies. Set the timer for 8 min. Place the cookies into the oven. Allow the cookies to bake.
4 Decorate the cookies. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies.
![Page 27: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/27.jpg)
Structured Algorithm?
// This program displays a delicious recipe for baking cookies.public class BakeCookies2 { public static void main(String[] args) { // Step 1: preheat oven System.out.println(“Preheat oven to 375F.");
// Step 2: Make the cookie 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 3a: Bake cookies (first batch). System.out.println("Set the timer for 8 min."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake.");
// Step 3b: Bake cookies (second batch). System.out.println("Set the timer for 8 min."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake.");
// Step 4: Decorate the cookies. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }}
![Page 28: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/28.jpg)
Structured Algorithms Structured algorithm provides abstraction (hide/ignore the right details at the right time)1 Preheat oven. Set oven to 375 degrees
2 Make the cookie batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients.
3 Bake the cookies. Set the timer. Place the cookies into the oven. Allow the cookies to bake.
4 Decorate the cookies. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies.
![Page 29: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/29.jpg)
Structured Algorithms Structured algorithm provides abstraction (hide/ignore the right details at the right time)1 Preheat oven. Set oven to 375 degrees
2 Make the cookie batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients.
3 Bake the cookies. Set the timer. Place the cookies into the oven. Allow the cookies to bake.
4 Decorate the cookies. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies.
![Page 30: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/30.jpg)
Removing Redundancy
A well-structured algorithm can describe repeated tasks with less redundancy.
1 Preheat oven.
2 Make the cookie batter.
3a Bake the cookies (first batch).•
3b Bake the cookies (second batch).
4 Decorate the cookies.
![Page 31: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/31.jpg)
31
Outline
Admin. and recap Java methods
Motivation Syntax: declaring method
![Page 32: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/32.jpg)
Static Methods
Arrange statements into groups and give each group a name.
Each such named group of statements is a static method
Writing a static method is likeadding a new command to Java.
class
method Astatementstatementstatement
method Bstatementstatement
method Cstatementstatementstatement
![Page 33: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/33.jpg)
Gives your method a name so it can be referred to.
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.");}
Declaring a Method
![Page 34: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/34.jpg)
Calling a Method
Executes the method's code
Syntax:
<name>();
You can call the same method many times if you like.
Example:
printWarning();
Output:
This product causes cancerin lab rats and humans.
![Page 35: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/35.jpg)
Example
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"); }}
![Page 36: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/36.jpg)
Example
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 howMy life got flipped turned upside-down
Now this is the story all about howMy life got flipped turned upside-down
![Page 37: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/37.jpg)
Final Cookie Program// This program displays a delicious recipe for baking cookies.public class BakeCookies3 { public static void main(String[] args) { preheatOven(); makeBatter(); bake(); // 1st batch bake(); // 2nd batch decorate(); }
// Step 1: Preheat oven public static void preheatOven() { System.out.println(“Preheat Oven to 375F."); } // Step 2: 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 3: Bake a batch of cookies. public static void bake() { System.out.println("Set the timer for 8 min."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); }
// Step 4: Decorate the cookies. public static void decorate() { System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }}
![Page 38: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/38.jpg)
Examples: Modifying BakeCookies
Bake three batches
Change timer from 8 to 10 min
38
![Page 39: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/39.jpg)
Capture structure of the program main should be a good summary of the program
public static void main(String[] args) {
}
Summary: Why Methods?
public static void main(String[] args) {
}
public static ... (...) {
}
public static ... (...) {
}
![Page 40: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/40.jpg)
Eliminate redundancy
public static void main(String[] args) {
}
Summary: Why Methods?
public static void main(String[] args) {
}
public static ... (...) {
}
![Page 41: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/41.jpg)
41
Outline
Admin. and recap Java methods
Motivations Syntax: declaring method Method control flow
![Page 42: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/42.jpg)
When a method A calls another method B, the program's execution... "jumps" into method B, executing its statements, then
"jumps" back to method A at the point where the method was called.
Method Calling Flow
![Page 43: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/43.jpg)
Methods Calling Methods
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."); }}
![Page 44: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/44.jpg)
Methods Calling Methods
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."); }}
Output:This is message1.This is message2.This is message1.Done with message2.Done with main.
![Page 45: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/45.jpg)
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.");}
Methods Calling Methods
![Page 46: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/46.jpg)
Methods Calling Methods
Example: What is the output of LazyDaddy?
![Page 47: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/47.jpg)
47
Outline
Admin. and recap Java methods
Why methods? Syntax: declaring method Method control flow Designing methods
![Page 48: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/48.jpg)
Example
Write a program to print these figures using methods. ______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
![Page 49: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/49.jpg)
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("+--------+"); }}
Does the code reflect structure?
Is there redundancy?
![Page 50: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/50.jpg)
50
Method Design Techniques
A basic method design method is called top-down decomposition dividing a problem into sub problems to be solved using methods
![Page 51: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/51.jpg)
Top-Down Decomposition
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg
teaCup
stop
hat
main
![Page 52: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/52.jpg)
Top-Down Decomposition
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg teaCup stopSign hat
main
![Page 53: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/53.jpg)
Top-Down Decomposition (egg)
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg teaCup stopSign hat
main
eggTop eggBottom
![Page 54: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/54.jpg)
Top-Down Decomposition (teaCup)
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg teaCup stopSign hat
main
eggTop eggBottom line
![Page 55: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/55.jpg)
Top-Down Decomposition (stopSign)
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg teaCup stopSign hat
main
eggTop eggBottom line stopLine
![Page 56: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/56.jpg)
Top-Down Decomposition (hat)
______ / \/ \\ / \______/
\ / \______/+--------+
______ / \/ \| STOP |\ / \______/
______ / \/ \+--------+
egg teaCup stopSign hat
main
eggTop eggBottom line stopLine
Q: What is a good order to implement/test the methods?
![Page 57: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/57.jpg)
Structured Program version// Suzy Student// 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(); }
...
![Page 58: CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400](https://reader036.vdocuments.net/reader036/viewer/2022062518/56649ebb5503460f94bc3e9a/html5/thumbnails/58.jpg)
Program version 3, cont'd. ... // Draws a line of dashes. public static void line() { System.out.println("+--------+"); }
// Draws a teacup figure. public static void teaCup() { eggBottom(); line(); System.out.println(); }
// Draws a stop sign figure. public static void stopSign() { eggTop(); System.out.println("| STOP |"); eggBottom(); System.out.println(); }
// Draws a figure that looks sort of like a hat. public static void hat() { eggTop(); line(); }}