Intro to Java week 4
METHODS.JAVA

Methods

Write something once, use it anywhere.

Download week 4 Four files, one per problem.

A method is a machine

Something goes in, something comes back. Drag it.

5
goes in5
public static int square(int n) {
    return n * n;
}
comes back25
The trade

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

public staticalways this
intgives back
squarename
(int n)takes in

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;
    }
}
Order does not matter

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 + "!");
}
Codewho becomesIt 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.

DoesCall it
voidActs, hands nothing backon its own line
int, double, StringWorks something out and hands it backwhere 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
The one that catches everyone

return ends the method immediately. Anything written after it never runs.

Problems

1. Say it three times

Hello.java

Write sayHello(). It takes nothing and returns nothing. Call it three times from main.

Example
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.java

Ask for a name, then pass it to greet.

Example
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.java

Finish square so it hands back n * n. Print the square of 3, 7 and 12.

Example
9
49
144
Hint

Replace return 0; with return n * n;

Then try this

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.java

In Battle Sim the combat formula is typed twice, once per fighter. Put it in a method and call it twice.

Example
Fighter 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

  1. Methods go inside the class, beside main, never inside it.
  2. Every method that promises a type must return that type on every path.
  3. Keep a method short. If it spans more than one screen, it is doing too much.
  4. Name it for what it does. square, not doIt.
  5. Name, date and one sentence at the top of every file.
  6. Stuck fifteen minutes on one thing? Stop and ask.
What this unlocks

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.