Methods With Parameters

So far we have identified how to create methods and return values, however we have not yet explored how to create methods which can accept values as parameters. As an example, the System.out.println method which we have used extensively can take a parameter which is printed out

Consider the code from the earlier methods tutorial:

package com.tinyappco;
public class Main {

    public static void main(String[] args) {
        System.out.println("Please type a phrase");
        int count = countOfVowelsInUserInput();
        System.out.println("There are " + count + " vowels in your phrase");
    }

    public static int countOfVowelsInUserInput(){
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        String text = scanner.nextLine();
        text = text.toLowerCase();
        int count = 0;
        for (int i = 0; i<text.length(); i++){
            char letter = text.charAt(i);
            if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u'){
                count++;
            }
        }
        return count; 
    } 
}
The countOfVowelsInUserInput() method, is currently limited to evaluating text typed in by the user. Ideally we would split this method into two - one which retrieves a line of text from the user (which we could reuse) and another which counts vowels in any string (which we could use without the command line).

You should have already discerned the code for the first part in the previous task, and it may look similar to this:

public static String lineOfUserInput(){
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        String userText = scanner.nextLine();
        return userText;
    }

Ideally the next stage is to re-write the main method, so it makes calls get the line of code, then to process it. This might look something like this:

public static void main(String[] args) {
    System.out.println("Please type a phrase");
    String userInput = lineOfUserInput();
    int vowelCount = countOfVowelsInString(userInput);
    System.out.println("There are " + vowelCount + " vowels in your phrase");
} 
However to do this we need to modify the old countOfVowelsInUserInput() method, so that it's name accuratly reflects it's now reduced functionality, and so it can take a parameter. The old method declaration looks like this:
public static int countOfVowelsInUserInput(){ //code follows
and the new like this
public static int countOfVowelsInString(String text) { //code follows
Notice how the brackets now contain two words, the first String declares the type of variable that the method can accept, the second text is the name that the variable will be refered to by within the body of the method. The complete modified method now appears as shown:
public static int countOfVowelsInString(String text) {
    text = text.toLowerCase();
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
        char letter = text.charAt(i);
        if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
            count++;
        }
    }
    return count;
}

The entire programme is now more clearly structured, and the user defined methods each have a clear single purpose.

Task - user boolean with prompt

Create a method named userBooleanWithPrompt which takes a String as a parameter and returns a boolean. This string should be displayed as a question to the user, and the method should read the user's response as a String and process it. You should be able to use the method you wrote in the Methods tasks as part of the solution for this.

video walkthough for the first part of this task is available

Task - Ten green bottles

Using the ten green bottles code from earlier tutorials, write a method which will take an integer as a parameter and return the verse starting with that many green bottles

Methods with multiple parameters

Sometimes we need to pass multiple parameters into a method. The format used to do this follows the same structure as above, however we simply add a comma before each second and subsequent parameter. Consider the following (rather pointless) example which checks if a string is a certain length and returns a boolean value:

public static boolean stringIsExactLength(String text, int guessLength){
    return text.length() == guessLength;
}
We could call this method as follows:
stringIsExactLength("Hello",5); //would return true

Task - multiple parameters

Further adapt the ten green bottles method so that as well as passing the number of bottles, you can pass a string to set the colour of the bottles

Advanced Task

Consider the following code:

public static void main(String[] args) {
        String greeting = "Hello";
        changeGreeting(greeting);
        System.out.println(greeting);
    }
    public static void changeGreeting(String greetingPhrase){
        greetingPhrase = "Bonjour";
    }
What will be printed? Are you sure? Why?