Lesson 9 - Activity 1 - Can you create your own methods?

Time: 15 minutes

Purpose: Create your own methods

Introduction: You’ve watched me declare some methods, now it’s time for you to have a go

Task: Create the following methods,

  1. addInteger, which takes two integers, and returns the sum
  2. addDouble, which takes two double numbers, and returns the sum
  3. intMatch, which takes two integers and returns true if they match
public static int add(int num1, int num2) {
    int answer= (num1 + num2);
    return answer;
}

public static double add (double double1, double double2) {
    double answer = (double1 + double2);
    return answer;
}

public static Boolean intMatch (int num1, int num2) {
    Boolean answer = (num1 == num2);
    return answer;
}

Good work - I’d say you don’t need the () for + or - operations.

It’s useful in

Boolean answer = (num1 == num2);

Because

Boolean answer = num1 == num2;

Doesn’t read as easily - it gets confusing with all the =. But this is purely stylistic.