ch09 - Alice in Action with Java Chapter 9 Methods...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Alice in Action with Java Chapter 9 Methods
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 2 Objectives Build your own Java methods Define parameters and pass arguments to them Distinguish between class and instance methods Build a method library
Background image of page 2
Alice in Action with Java 3 Methods How to perform a method Send a message to an object or class Building a method in Alice Click the create new method button Drag statements into the method Focus of Chapter 9 Learning how to build methods in Java
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 4 Introductory Example: The Hokey Pokey Song Problem: write a Java program to display song lyrics Brute force approach One String object stores the song lyrics One action displays those lyrics Implement program using one println() message Issue: program is about 60 lines long (excessive) A better approach takes advantage of song structure Each verse only differs by the body part that is moved Implement program with a single method to print verse printVerse() takes one argument for the bodyPart
Background image of page 4
Alice in Action with Java 5 Introductory Example: The Hokey Pokey Song (continued)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 6 Introductory Example: The Hokey Pokey Song (continued)
Background image of page 6
Alice in Action with Java 7 Introductory Example: The Hokey Pokey Song (continued)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 8 Methods (continued) Analyzing the first line of printVerse() public : allows another class access to the method static : indicates that the message is a class method void : indicates that the method does not return a value printVerse : the method’s name () : contains parameters, such as String bodyPart { : indicates the beginning of the method statements Simplified pattern for a Java method [ AccessMode ] [static] ReturnType MethodName ( Params ) { Statements }
Background image of page 8
Alice in Action with Java 9 Non- void vs. void Methods Alice messages Methods: messages sent where a statement occurs Functions: messages sent where an expression occurs All messages in Java are called methods void method in Java Corresponds to an Alice method Example: printVerse() non- void method in Java Corresponds to an Alice function Must have a return type
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 10 Non- void Methods How getVerse() revises the HokeyPokey program Takes a String argument representing a bodyPart Returns a verse that includes the bodyPart Is called multiple times in one println() statement getVerse() method vs. printVerse() method getVerse() is a value-returning method getVerse() can be used for more than display Non- void methods use Java’s return statement return statement pattern: return Expression Any Java type can be used as a return type
Background image of page 10
Alice in Action with Java 11 Non- void Methods (continued)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alice in Action with Java 12 Non- void Methods (continued)
Background image of page 12
Alice in Action with Java 13 Method Design Procedure for developing a method
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 57

ch09 - Alice in Action with Java Chapter 9 Methods...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online