• 0

[Java] Method overloading while accepting input from a user


Question

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:

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);

}
}

5 answers to this question

Recommended Posts

  • 0

The purpose of method overloading is not to duplicate code for each possible type; in fact, anytime your design implies duplicating code, it should make you raise an eyebrow. Here, both void rect(int, int) and void rect(double, double) use the exact same code. Also, since integers are implictly convertible to doubles, when you pass an integer to the method, the compiler could flag the call as ambiguous (but in Java I'm not sure).

Since integer and double multiplication are equivalent (well, for your purposes I guess), and that doubles include integers, you could just keep the double version and drop the integer version.

If you need a version of the method unique to different argument types but that does the same thing for each type, this calls for a generic method. Something like

void <T> rect(T a, T b) {
   // use T instead of int or double
}

  • 0

Also, I think the idea of accepting input from the user without knowing what is the type of input makes no sense, unless you want to go through the trouble of analysing the raw string to identify the type (if it's digits and there's a dot somewhere, it's a double, if it's alphanumerical characters, it's a string, etc.).

  • 0

I guess this is some kind of school project that you have been set? So debating how useful it is won't help?

Anyway, I think you are right, there's no way to input a number without committing to its type.

Is it worth showing us the original problem statement?

  • 0
  Dr_Asik said:
The purpose of method overloading is not to duplicate code for each possible type; in fact, anytime your design implies duplicating code, it should make you raise an eyebrow. Here, both void rect(int, int) and void rect(double, double) use the exact same code.

If you need a version of the method unique to different argument types but that does the same thing for each type, this calls for a generic method. Something like

void <T> rect(T a, T b) {
	// use T instead of int or double
 }

I was just trying to learn the basics of method overloading. So I just duplicated the code so that I can get a double result if the input is of the type double and int result if the input type is int.

I appreciate your help. But I didn't understand the code; we have to replace T with a data type right?

  JamesCherrill said:
I guess this is some kind of school project that you have been set? So debating how useful it is won't help?

Anyway, I think you are right, there's no way to input a number without committing to its type.

Is it worth showing us the original problem statement?

The question [came up with it myself] : Compile a program in the Java using method overloading to find the area of a rectangle based the data type of the input (int or double, by the user) and print the result with the same data type.

  • 0
  Unto Darkness said:
I appreciate your help. But I didn't understand the code; we have to replace T with a data type right?
No, you really write T. Or AnyType. Or any unused type identifier. At compile-time, T will be (in theory) replaced by the type of the arguments you provide. I say in theory because how Java does it is superficial and stupid, but you don't need to know about that right now. You can consider T as being a placeholder for the real type. The compiler will generate as many versions of your method as the number of types you call it with.

In other words, instead of duplicating the code yourself, like you did in your sample code, you let the compiler do it by replacing the type by a placeholder.

  Quote
I was just trying to learn the basics of method overloading. So I just duplicated the code so that I can get a double result if the input is of the type double and int result if the input type is int.
I understand, but that's not really the purpose of method overloading, hence you have trouble finding a test case that makes sense. If you need a method that does exactly the same thing ( = uses the exact same code) for different types, write a generic one as I proposed. Method overloading is mainly useful if you need to do something slightly different depending on the parameters provided. For instance, System.out.print() is overloaded for many different types because although the overall result is the same (the object gets displayed on the terminal), it's done somewhat differently for each type. When you do System.out.print(1.345), Java recognizes through string analysis, at compilation, that 1.345 is a float and by overloading, selects System.out.print(float f).
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.