Right now I am passing hard coded arguments to the constructors. I want to pass user input as argument to the constructors. Below is my code:
public class AddTwoNumbers {
AddTwoNumbers(int num1, int num2){
int sum = num1+num2;
System.out.println("Sum = "+sum);
}
AddTwoNumbers(int num1, float num2){
float sum = num1+num2;
System.out.println("Sum = "+sum);
}
AddTwoNumbers(float num1, int num2){
float sum = num1+num2;
System.out.println("Sum = "+sum);
}
AddTwoNumbers(float num1, float num2){
float sum = num1+num2;
System.out.println("Sum = "+sum);
}
public static void main(String[] args) {
AddTwoNumbers addNumObj1 = new AddTwoNumbers(4,5);
AddTwoNumbers addNumObj2 = new AddTwoNumbers(4.5f, 5.5f);
}
}
So how can I take take user input. If I use Scanner class then I am already restricting the user's choice of input that is, if I ask the user to input both integer numbers but user want one integer number and another float number. Also doing so lefts no advantage of constructor overloading. So I am looking for a solution in which I don't want to show user multiple options to select from instead handle it by code and want to make use of constructor overloading specifically.