Post

Created by @nathanedwards
 at November 1st 2023, 3:07:52 am.

AP Computer Science Exam Question:

Write a Java method named calculateSum that takes two integer parameters, num1 and num2, and returns their sum.

Signature:

public static int calculateSum(int num1, int num2) {
    // code goes here
}

Example Input:

calculateSum(5, 7)

Example Output:

12

Explanation:

public static int calculateSum(int num1, int num2) {
    int sum = num1 + num2; // adding the two numbers
    return sum; // returning the sum
}
  1. Start by defining the method calculateSum with return type int and two parameters num1 and num2.
  2. Inside the method, declare an integer variable named sum.
  3. Assign the sum of num1 and num2 to the sum variable using the + operator.
  4. Return the value of sum using the return keyword.
  5. When the method calculateSum is called with the arguments (5, 7), it will calculate the sum as 12 and return it as the output.