Download presentation
Presentation is loading. Please wait.
Published byMuriel Hampton Modified over 5 years ago
1
Workshop for Programming And Systems Management Teachers
Chapter 4 Writing Recipes (Methods) Georgia Institute of Technology
2
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
3
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
4
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
5
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
6
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 There are other visibility keywords but these are the two you should be using. Georgia Institute of Technology
7
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
8
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
9
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
10
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 Method definitions go inside a class definition public class Name { field declarations constructors method declarations } The order of things declared in a class definition doesn’t matter. Some people declare the methods first and then the constructors and fields. Some prefer the fields first, then the constructors, and then the methods. You an actually mix definitions up like declare a field then a method and then another field but it is good practice to group the fields, constructors, and methods. Georgia Institute of Technology
11
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 The compiler won’t compile a file with a class name that differs from the file name it is defined in. Georgia Institute of Technology
12
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
13
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 Fix any errors and compile again Invoke the method in the interactions pane Files Pane Definitions Pane The files are in c:\intro-prog-java\bookClasses\ Interactions Pane Georgia Institute of Technology
14
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
15
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
16
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 It doesn’t matter to the computer what names you use. Good names make your code easier for people to understand what you are doing. Georgia Institute of Technology
17
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
18
Showing A Particular Picture
public static Picture showPicture() { String myFile = "FILENAME"; Picture myPicture = new Picture(myFile); myPicture.show(); return myPicture; } Georgia Institute of Technology
19
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
20
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 You can also copy text from the definitions pane to the interactions pane. Georgia Institute of Technology
21
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 We also don’t want to have to edit the program and recompile to change the file name for each file we want to create a picture from. Georgia Institute of Technology
22
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
23
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; } Try showNamed() with different file names. You can use FileChooser.pickAFile() to get the file names to use. Georgia Institute of Technology
24
How Does showNamed Work?
When you type Picture.showNamed(“c:/intro-prog-java/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
25
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
26
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
27
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
28
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
29
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.