Write a program that can perform addition with these overloaded methods:
int add(int x, int y) – returns the sum of two integer values
double add(int x, double y) – returns the sum of an integer value and a double value
double add(int x, int y, double z) – returns the sum of two integer values and a double value
Use the methods in the program. (How would you use them?)
class App {
/**
* returns the sum of two integer values
*
* @param x
* @param y
* @return
*/
static int add(int x, int y) {
return x + y;
}
/***
* returns the sum of an integer value and a double value
*
* @param x
* @param y
* @return
*/
static double add(int x, double y) {
return x + y;
}
/**
* returns the sum of two integer values and a double value
*
* @param args
*/
static double add(int x, int y, double z) {
return x + y + z;
}
public static void main(String[] args) {
int x = 5;
int y = 5;
double z = 1.2;
System.out.println("int add(int x, int y): " + add(x, y));
System.out.println("double add(int x, double y): " + add(x, z));
System.out.println("double add(int x, int y, double z): " + add(x, y, z));
}
}
Comments
Leave a comment