SLIDE 1
Week 8 -Wednesday What did we talk about last time? StdDraw - - PowerPoint PPT Presentation
Week 8 -Wednesday What did we talk about last time? StdDraw - - PowerPoint PPT Presentation
Week 8 -Wednesday What did we talk about last time? StdDraw practice We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and
SLIDE 2
SLIDE 3
SLIDE 4
SLIDE 5
SLIDE 6
We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to
read
Sometimes you need to do similar things several times:
- Read some input and check that it falls within certain values
- Draw a green hexagon at several different locations
- Find the square root of several different numbers
SLIDE 7
Methods allow you to package up some code to run over and
- ver
Methods usually take some input (like numbers or Strings)
so that they can be customized
Methods often give back an answer (like the square root of a
number)
Also called functions in some other languages
SLIDE 8
More modular programming
- Break a program into separate tasks
- Each task could be assigned to a different programmer
Code reusability
- Use code over and over
- Even from other programs (like Math.sqrt())
- Less code duplication
Improved readability
- Each method can do a few, clear tasks
SLIDE 9
SLIDE 10
public static type name(type arg1,…,type argn) { statement1; statement2; … statementm; } Required syntax Type of answer Name of last argument Name of 1st argument Name of method Type of 1st argument Type of last argument Code done by method
SLIDE 11
Given two integers, find the smaller: public static int min(int a, int b) { if( a < b ) { return a; } else { return b; } }
SLIDE 12
static methods are methods that are not connected to a
particular object
They are just supposed to perform some simple task We are going to focus on static methods until next week For now, just taken it as a given that the definition of every
method will include the static keyword
SLIDE 13
It is possible to divide methods into two types:
- Value returning methods
- Void methods
Value returning methods give an answer:
int small = min(x, y);
SLIDE 14
Void methods are declared with void as their return type Void methods just do something If you try to save the value they give back, there will be a compiler
error
public static void help(int times) { for(int i = 0; i < times; ++i){ System.out.println("Help!"); } }
SLIDE 15
Like most code in Java, the code inside of a method executes
line by line
Of course, you are allowed to put if statements and loops
inside methods
You can also put in return statements A method will stop executing and jump back to wherever it
was called from when it hits a return
The return statement is where you put the value that will be
given back to the caller
SLIDE 16
SLIDE 17
Defining a method is only half the game You have to call methods to use them Calling a method means giving a method the parameters (or
arguments) it needs and then waiting for its answer
By now, you have done many method calls
- System.out.println()
- Math.sqrt()
You can call your own methods the same way
SLIDE 18
Proper syntax for calling a static method gives first the name
- f the class that the method is in, a dot, the name of the
method, then the arguments
If the method is in the same class as the code calling it, you
can leave off the Class. part
If it is a value returning method, you can store that value into
a variable of the right type
Class.name(arg1, arg2, arg3);
SLIDE 19
If a static method is in another class, you have to put the name of
the class first, followed by a dot
We've seen a few of these already: By convention, class names in Java always start with a capital
letter
double length = Math.sin(angle); double value = Math.random(); double[] samples = StdAudio.read(file);
SLIDE 20
Variables from outside of the method don't exist unless
they've been passed in as parameters
No matter how complex a program is, inside this method,
- nly x, y, and z variables exist
public static int add(int x, int y){ int z = x + y; return z; }
SLIDE 21
A magical process called binding happens which copies the
values from the calling code into the parameters of the method
The calling code can use variables with the same names, with
different names, or even just literals
The method does not change the values of the variables in the
- riginal code
Remember, it only has copies of the values
SLIDE 22
No connection between the two different x's and y's
public static int add(int x, int y){ int z = x + y; //5 + 10 return z; } int a = 10; int x = 3;
int y = add( 5, a ); //y contains 15 now
SLIDE 23
- 1. Start a method with the keywords public static
- 2. Next comes the return type
- 3. Then the name of the method
- 4. Then, in parentheses, the arguments you want to give to the
method
- 5. Inside braces, put the body of the method
- 6. Include a return statement that gives back a value if
needed
SLIDE 24
A palindrome is a word or phrase spelled the same forwards
and backwards:
- "bob"
- "radar"
- "satanoscillatemymetallicsonatas"
Write a method with the following header that returns true
if a String is a palindrome and false otherwise
public static boolean isPalindrome(String phrase) {
SLIDE 25
SLIDE 26
SLIDE 27
Keep reading Chapter 8 More method practice
SLIDE 28
Keep reading Chapter 8 of the textbook Keep working on Project 3
- Due next Friday