Create a Java method called calculateSum
that takes in two integers as parameters and returns the sum of the two integers.
calculateSum
method.calculateSum
method and print the result.public static int calculateSum(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
public static void main(String[] args) {
int result = calculateSum(5, 10);
System.out.println("The sum is: " + result);
}
Explanation:
Method Declaration:
calculateSum
.int
, indicating that this method will return an integer value.num1
and num2
, both of type int
.num1
and num2
is calculated and stored in a variable called sum
.sum
value is returned as the output of the method.Method Invocation and Print Result:
main
method, we invoke the calculateSum
method by passing two integers, 5
and 10
, as arguments.calculateSum
method is assigned to an int
variable called result
.System.out.println()
, concatenating the string "The sum is: " with the value of result
.