Podcast
Questions and Answers
What is the purpose of a method in programming?
What is the purpose of a method in programming?
What does the 'void' keyword signify in a Java method declaration?
What does the 'void' keyword signify in a Java method declaration?
Which component of a method indicates the method's name?
Which component of a method indicates the method's name?
What is the main advantage of using a method like printVerse() to display song lyrics?
What is the main advantage of using a method like printVerse() to display song lyrics?
Signup and view all the answers
In the method declaration 'public static void printVerse(String bodyPart)', what does 'public' represent?
In the method declaration 'public static void printVerse(String bodyPart)', what does 'public' represent?
Signup and view all the answers
Study Notes
Methods Part 2
- Methods are used to perform actions
- A method sends a message to an object or class
- To build a method in Alice, click "create new method"
- Then drag statements into the method
- Students have likely created main methods and other methods before
- Example: The Hokey Pokey Song
- Brute force: store song lyrics in a single string, display in one action, 60 lines of code (excessive)
- Better approach: leverage song structure, single method ("printVerse") takes a "bodyPart" argument
- This example shows how a method can streamline code
Method Example: The Hokey Pokey Song Lyrics
- Detailed Hokey Pokey lyrics provided, including all verses.
Introductory Example: The Hokey Pokey Song (Continued)
- HokeyPokey.java to display Hokey Pokey lyrics properly.
- Code example demonstrating a
printVerse
method, taking a "body part" parameter. This correctly illustrates efficiency.
Methods (Continued)
- Analyzing
printVerse
method-
public
: accessible by other classes -
static
: a class method, not specific to an object -
void
: does not return a value (unlike a function which does return a value). -
printVerse
: the methods' name -
()
: parameter list,String bodyPart
-
{}
: containing the method's statements/actions.
-
- Shows a simplified Java method structure
-
[AccessMode] [static] ReturnType MethodName (Params) {Statements}
-
Non-void vs. void Methods
- Alice methods:
-
Methods
: just run statements -
Functions
: return a value
-
- Java considers both
methods
-
void
method in Java:- Corresponds to an Alice method (no return type)
-
non-void
method in Java:- Corresponds to an Alice function (requires a return type)
Einstein's Formula
-
e = m x c²
: energy = mass x speed of light²- Formula itself could be a user story
-
massToEnergy
method example- Method returns a
double
- Parameter list includes a double
mass
-
SPEED_OF_LIGHT
is a constant outside the method - Calculation is within the return statement
- Method returns a
Einstein's Formula (Continued)
- Code example of the
massToEnergy
method, using the formula - Shows how a method can encapsulate a complex calculation.
- Returns a computed
double
value
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore the concept of methods in programming with a focus on the Hokey Pokey song example. Learn how to create efficient methods to streamline code and analyze the structure of the printVerse
method. This quiz will test your understanding of method creation and implementation.