I am trying to use method overloading to find the area of a rectangle. Only thing is the values have to be entered by the user. But if it has to accepted from the user, shouldn't we know the datatype of his input? And if we do, then the purpose of overloading becomes useless, because I already know the datatype.
Can you guys help me out?
You can add onto this code:
import java.io.*;
import java.lang.*;
import java.util.*;
class mtdovrld
{
void rect(int a,int b)
{
int result = a*b;
System.out.println(result);
}
void rect(double a,double b)
{
double result = a*b;
System.out.println(result);
}
}
class rectarea
{
public static void main(String[] args)throws IOException
{
mtdovrld zo = new mtdovrld();
Scanner input= new Scanner(System.in);
System.out.println("Pleaser values:");
// Here is the problem, how can I accept values from user where I do not have to specify datatype and will still be accepted by method?
double a = input.nextDouble();
double b = input.nextDouble();
zo.rect(a,b);
}
}
Question
Unto Darkness
I have got a problem.
I am trying to use method overloading to find the area of a rectangle. Only thing is the values have to be entered by the user. But if it has to accepted from the user, shouldn't we know the datatype of his input? And if we do, then the purpose of overloading becomes useless, because I already know the datatype.
Can you guys help me out?
You can add onto this code:
Link to comment
https://www.neowin.net/forum/topic/722390-java-method-overloading-while-accepting-input-from-a-user/Share on other sites
5 answers to this question
Recommended Posts