Methods
Write something once, use it anywhere.
A method is a machine
Something goes in, something comes back. Drag it.
public static int square(int n) {
return n * n;
}
The same lines get copied twice, once for each fighter. A method lets them be written once and called twice. When the formula is wrong, fix it in one place.
The shape
Methods go inside the class, next to main, never inside it.
public class Squares {
public static void main(String[] args) {
System.out.println(square(7));
}
public static int square(int n) {
return n * n;
}
}
Java finds square whether written above or below main. Place methods after main so the reader sees the program first.
Parameters
A parameter is a variable the method is handed when it is called.
public static void greet(String who) {
System.out.println("Hello, " + who + "!");
}
| Code | who becomes | It prints |
|---|---|---|
greet("Sam") | "Sam" | Hello, Sam! |
greet("Adi") | "Adi" | Hello, Adi! |
The name who only exists inside the method. Nothing outside can see it.
Returning a value
Two kinds of method. The difference is what sits before the name.
| Does | Call it | |
|---|---|---|
void | Acts, hands nothing back | on its own line |
int, double, String | Works something out and hands it back | where a value would go |
greet("Sam"); // void, a whole statement
int answer = square(7); // returns, so it goes on the right of =
System.out.println(square(7)); // or straight into a println
return ends the method immediately. Anything written after it never runs.
Problems
1. Say it three times
Hello.javaWrite sayHello(). It takes nothing and returns nothing. Call it three times from main.
Hello there!
Hello there!
Hello there!
Hint
No value goes in and none comes back, so it is public static void sayHello() with empty brackets.
2. Greet by name
Greet.javaAsk for a name, then pass it to greet.
Your name: Sam
Hello, Sam!
Hint 1
The method already has its parameter. Only the call is missing: greet(name);
Hint 2
The variable in main and the parameter need not share a name. greet(name) passes the value, and inside the method it is called who.
3. Squares
Squares.javaFinish square so it hands back n * n. Print the square of 3, 7 and 12.
9
49
144
Hint
Replace return 0; with return n * n;
Add cube next to it, and print square(3) + cube(2). Two method calls in one expression work fine.
4. Battle Sim, without the copy and paste
BattleScore.javaIn Battle Sim the combat formula is typed twice, once per fighter. Put it in a method and call it twice.
ExampleFighter 1 strength, armor, stamina? 80 60 55
Fighter 2 strength, armor, stamina? 70 90 40
Fighter 1 combat = 68.8
Fighter 2 combat = 67.5
Hint 1
The method takes three int values and hands back a double, so it starts public static double warriorScore(int strength, int armor, int stamina).
Hint 2
Keep the 4.0. All three parameters are whole numbers, so dividing by 4 would throw the decimal away. Same trap as week 2.
Hint 3
Now change the formula to divide by 5.0 and run it. Notice only one line needed editing for both fighters. That is the whole point of the week.
Rules
- Methods go inside the class, beside
main, never inside it. - Every method that promises a type must
returnthat type on every path. - Keep a method short. If it spans more than one screen, it is doing too much.
- Name it for what it does.
square, notdoIt. - Name, date and one sentence at the top of every file.
- Stuck fifteen minutes on one thing? Stop and ask.
Every assignment from here on can use methods, and the rule that banned them is gone. Going back and rewriting Battle Sim with them is worth an hour.