Here's an example of a method that takes two integers as parameters and returns their sum:
public int sum(int a, int b) {
return a + b;
}
To use this method in an Android app, you can create a new class (let's call it MathUtils
) and add the method to it:
public class MathUtils {
public int sum(int a, int b) {
return a + b;
}
}
Then, in your Android activity or fragment, you can create an instance of the MathUtils
class and call the sum
method:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MathUtils mathUtils = new MathUtils();
int result = mathUtils.sum(2, 3);
Log.d("TAG", "Result: " + result);
}
}
In this example, we create an instance of the MathUtils
class and call the sum
method with the integers 2
and 3
. The method returns 5
, which we then log to the console using Log.d
.