0
How method overloading can be achieved by taking input form the user in java?
import java.util.*; class A{ public static int add(int a,int b){return a+b;} public static double add(double a, double b){return a+b;} } class M{ public static void main(String[] arg){ System.out.println(A.add(4,5)); //9 System.out.println(A.add(4.2,5.3)); //9.5 }} This code is ok but if I want to take two no. from the user these no. can be either int or double type so i've to store these no. in two variables and pass them in add() method then how I'll decide the type of variables that type will int/double?
3 odpowiedzi
+ 3
Ok sarthak thanks...but I want that user can input either
4.2 and 5.3 or 4 and 5 and you want that i should have to receive value in double type variable, if I'm receiving the value in double i.e.
let sc is the object of Scanner class
double a=sc.nextDouble();
double b=sc.nextDouble();
then user can only input 4.2 and 5.3 but if user will input 4 and 5 then it will become an InputMismatchException
so the my proper question is that
How will I know that user is going to input int or double so that I'll declare the variable of type int or double and receive the value in it?
+ 1
Overloading Operator.....
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
***************************************************************************
Function overloading in C++:
You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.
Following is the example where same function print() is being used to print different data types:
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i)
{
cout << "Printing int: " << i << endl;
}
void print(double f)
{
cout << "Printing float: " << f << endl;
}
void print(char* c)
{
cout << "Printing character: " << c << endl;
}
};
int main(void)
{
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
When the above code is compiled and executed, it produces the following result:
Printing int: 5
Printing float: 500.263
Prin
0
take input in double.
use math.round() for removing decimal values.. if rounded value is equal to original value.. then explicitly convert it to int. else it leave it as it is.
for user input there will be only one print statement.