vib321
vib321

Reputation: 71

Where I'm getting wrong with the code? I'm trying to implement the method overloading

When I run the code, I get prompt to enter the number and I enter the number but nothing happens after that! I'm trying to implement method overloading here. Any help would be appreciated.

import java.util.Scanner;
public class MethodOverload {
 public static void main(String [] args){
     Scanner input = new Scanner(System.in);
     Scanner inputDoub = new Scanner (System.in);
     System.out.println ("Enter the int or double number");
     int x = input.nextInt();
     double y = inputDoub.nextDouble();
     //int x;
     //double y;
     System.out.printf("Square of integer value %d", x, "is", square(x));
     System.out.printf("Square of double value %f", y, "is", square(y));
      }

   public static int square(int intValue){
       System.out.printf ("\nCalled square method with int argument: %d", intValue);

       return intValue*intValue;
   }

   public static double square (double doubleValue){
       System.out.printf ("\nCalled sqauer method with double argument: %d", doubleValue);
       return doubleValue*doubleValue;
   }

}

Upvotes: 0

Views: 71

Answers (2)

Rajesh
Rajesh

Reputation: 2155

Your attempted formatting double with %d which is not correct. PFB change needed :

public static double square (double doubleValue){
    System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
    return doubleValue*doubleValue;
}

One observation: It doesn't make sense to use 2 separate instances of Scanner. No use. Correct your code like this:

Scanner input = new Scanner(System.in);
//Scanner inputDoub = new Scanner (System.in);
System.out.println ("Enter the int or double number");
int x = input.nextInt();
double y = input.nextDouble();

Upvotes: 0

The Coding Wombat
The Coding Wombat

Reputation: 815

import java.util.Scanner;
public class MethodOverload {
public static void main(String [] args){
    Scanner input = new Scanner(System.in);
    System.out.println ("Enter the int or double number");
    double y = input.nextDouble();

    if(y % 1 == 0) {
        int x = (int) y;
        System.out.printf("Square of integer value %d is %d", x, square(x));
    }else{
        System.out.printf("Square of double value %f is %f", y, square(y));
    }

}

public static int square(int intValue){
    System.out.printf ("\nCalled square method with int argument: %d", intValue);

    return intValue*intValue;
}

public static double square (double doubleValue){
    System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
    return doubleValue*doubleValue;
}

}

If I understand correctly you simply want to get the users input, and if the users enters a double use one overloaded method, if he enters an integer use the other. The code above does this.

It simply stores the user input as a double, if the user input modulo 1 = 0 (meaning it's an integer) cast it to an integer and call the overloaded method passing an integer argument. Also, in the last overloaded square method, you used %d in your printf function instead of %f, use %f when you want to have a double.

Your first two printf statements were also wrong, the syntax only allows one String to be displayed, and the other arguments are for replacing all the % signs.

Upvotes: 1

Related Questions