Functions (methods) in Java

1. Write a function “larger” that takes two integers x and y and returns the larger of the two.

     int larger(int x, int y){
        if (x > y) {
            return x;
        } else {
            return y;
        }
    }

     
2. Write a function isEven that takes an int argument x and returns true if x is even and false otherwise.
    
3. Write a function “chineseZodiac”, that takes and integer parameter “year” and returns the name of the animal that corresponds to that birth year. Clue: you will need to use the modulo operator.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s