WritingRecipes-Ch4

Download Report

Transcript WritingRecipes-Ch4

Workshop for Programming And
Systems Management Teachers
Chapter 4
Writing Recipes (Methods)
Georgia Institute of Technology
Learning Goals
• Understand at a conceptual and practical
level
– How to invoke methods in Java
• Both class and object
– How to write methods in Java
– How to return a value from a method
– How to specify that a method doesn’t return a
value
– How to pass variables to a method
Georgia Institute of Technology
Invoking Methods in Java
• Use .methodName(parameters) to send a
message which will map to a method
• Examples
– FileChooser.pickAFile()
• pickAFile is a message which maps to the
pickAFile class method in the class FileChooser
– “Hi”.toLowerCase()
• toLowerCase is a message which maps to the
toLowerCase object method in the class String
Georgia Institute of Technology
Naming a Recipe
• Writing a method is giving a name to a
recipe (a series of Java statements) in a
class file
• When you want to execute the series of
statements again
– You use the method name
• As the name of the message
• You can also pass parameters
– To be used as variables in the method
Georgia Institute of Technology
Naming a Method
• When we name variables we use
– Type name; or Type name = expression;
• When we name methods we use
– visibility Type name (parameterList) {body}
• The order is important
– We call this the syntax of the declaration
• Example method declarations
– public void show()
– private double computeTax(double total)
Georgia Institute of Technology
Writing a Method
There are several visibility keywords
– The keyword “public” means it can be invoked
from inside any class definition
– The keyword “private” means it can be
invoked from within the current class definition
• The “Type” is the type of value returned
• Or the keyword “void” if no value is returned
• The “parameterList” is a list of type and
name pairs separated by commas
Georgia Institute of Technology
Declaring a Class Method
• A class method adds the keyword “static”
– public static String pickAFile()
• Class methods can be invoked using the
class name
– FileChooser.pickAFile
• Use a class method when you are not
operating on data in the current object
– The method may create an object
Georgia Institute of Technology
Method Body
• The statements that make up the method
are placed in a block
– between opening and closing curly braces
public Type name (parameterList)
{
statement 1;
statement 2;
.
.
.
}
Georgia Institute of Technology
Returning from a Method
• Use the keyword “return” to return a value
or object from a method
return x;
return person;
• The type of the thing being returned must
agree with the declared type
public int getNum()
{
return “3”; // would cause a compile error
}
Georgia Institute of Technology
Class Definitions
• In Object-oriented
programming we
break the tasks to be
done up and assign
them to classes
– The class that is
responsible for the
task
public class Name
{
field declarations
constructors
method declarations
}
• Method definitions go
inside a class
definition
Georgia Institute of Technology
Java Class Definitions
• In Java each class is defined in its own file
– With the same name as the class
– With .java as the extension
• The FileChooser class definition would be
defined in the file
– FileChooser.java
• The Picture class definition would be in
– Picture.java
Georgia Institute of Technology
Writing your First Method
• How about a method that lets you pick the
file name, creates the picture object, and
shows it? It could also return the picture
object for later use.
public static Picture pickAndShow()
{
String fileName = FileChooser.pickAFile();
Picture picture = new Picture(fileName);
picture.show();
return picture;
}
Georgia Institute of Technology
Method Exercise
• Open DrJava
• Open the file
Picture.java
• Type the method
before the last ‘}’ in
the file
• Compile the file
Definitions Pane
Files Pane
– Fix any errors and
compile again
• Invoke the method in
the interactions pane
Interactions Pane
Georgia Institute of Technology
Method to Pick And Play a Sound
• Write a method to pick and play a sound
– First pick a file name
– Create the sound object
– Tell the sound object to play
– Return the sound object
• How do you do each of these things?
• What class does this method need to be
declared in?
Georgia Institute of Technology
Pick and Play Sound Method Exercise
• Write the following method
–
–
–
–
In the Sound.java file
Before the last curly brace
Then compile it
And invoke it by
• Sound.pickAndPlay();
public static Sound pickAndPlay()
{
String fileName = FileChooser.pickAFile();
Sound sound = new Sound(fileName);
sound.play();
return sound;
}
Georgia Institute of Technology
Names are Important to People
• Go back and change the names you use
for variables in the method
– Does it change what the method does?
• Use names that are understandable
– To you
– To others
• Use names that aren’t too long
thisIsAVeryLongVariableName is a pain to type
Georgia Institute of Technology
Method to Show A Picture
• Write a method to show a particular
picture
– specify the file name
– create the picture object
• using the file name
– ask the picture object to show itself
– return the picture object
• How do you do each of those things?
• What class should the method be in?
Georgia Institute of Technology
Showing A Particular Picture
public static Picture showPicture()
{
String myFile = "FILENAME";
Picture myPicture = new Picture(myFile);
myPicture.show();
return myPicture;
}
Georgia Institute of Technology
Slashes and Backslashes
• We have seen file names with
backslashes ‘\’ in them
– As a result of FileChooser.pickAFile()
• However we can’t just use backslashes in
string objects
– They have a special meaning “\t” is tab “\n” is
newline
• There are two ways to fix this problem
– Use a slash instead ‘/’
– Use double backslashes ‘\\’
Georgia Institute of Technology
Using the Interaction Pane
• If you aren’t sure about how to do
something you want to do in a method
– Try it out in the Interactions pane
– Use System.out.println(expression) to see the
result
• Copy it to the Definitions pane
– Select what you want to copy
– Control-C to copy it
– Control-V to paste it
Georgia Institute of Technology
Passing a Parameter to a Method
• We wouldn’t want to create a new method
for every picture we want to show
– That would be a huge number of methods
• Instead let’s take the file name as a
parameter (input variable)
– Making the method more general (reusable)
• Parameters specify their type and name
– Just like a variable declaration
– They are used like variables in the method
Georgia Institute of Technology
Showing a Picture Given the Name
• Write a method to show a picture when the
method is passed the file name to use.
– Need to pass in a file name
– Need to create the picture object using the file
name
– Need to tell the picture object to show itself
– Need to return the picture object
• How do we do those things?
• What class should this method be defined
in?
Georgia Institute of Technology
showNamed Method
• Add the method to the Picture.java file
• Compile the Picture.java file
• Try the method in the interactions pane
Picture.showNamed(“c:/intro-prog-java/mediasources/katie.jpg”);
public static Picture showNamed(String fileName)
{
Picture myPicture = new Picture(fileName);
myPicture.show();
return myPicture;
}
Georgia Institute of Technology
How Does showNamed Work?
• When you type
– Picture.showNamed(“c:/intro-progjava/mediasources/katie.jpg”);
• The compiler checks that the Picture class has a
class method showNamed that takes a string
object.
• At run time the method is executed and the input
variable fileName refers to the input string object
during execution of the method
– The name is only in use during the method
Georgia Institute of Technology
Scope
• Parameters or variables declared in a
method have method scope
– The names are known inside the method
• They are not known outside the method
– Can use the same variable names in the
interactions pane
– Or in other methods
Georgia Institute of Technology
playNamed Exercise
• Try to do a similar method in the
Sound.java class to play a sound
– It should take a file name as input
– It should create the sound object
– It should tell the sound object to play
– It should return the created sound object
Georgia Institute of Technology
Show a Passed Picture
• How would you write a method that takes
a picture object as an input variable?
• What type would the input variable be?
• What should you name it?
• How would you get the picture object to
show?
• Does it need to return anything?
Georgia Institute of Technology
showPicture Method
public static void showPicture(Picture myPicture)
{
myPicture.show();
}
• How is this different from just calling
picture.show()?
• Which one is a class method?
• Which one is an object method?
• Which way is better?
Georgia Institute of Technology
Summary
• Methods are declared inside of class
definitions
• Methods declarations have the following
syntax
– visibility Type name (parameterList) { code}
• Class methods have the keyword static
• Methods that do not return any value use
the keyword void
Georgia Institute of Technology