I am trying to change my code to incorporate an int and double parameter for one method name. My end goal is to let the user pick two numbers and if they type one as int and the other as double, I want the code to still be able to account for those different types and print successfully. The code as follows is the basics I have come up with so far and I would like some help on how to change this code to use method overloading.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Select operation:");
System.out.println("1. Divide 2 numbers");
System.out.println("2. Exit");
System.out.print("Enter choice(1/2:");
int choice = input.nextInt();
if (choice == 1){
division();
}
else if (choice == 2){
Exit();
}
input.close();
}
public static void division(){
int nOne, nTwo;
Scanner input = new Scanner(System.in);
System.out.println("Division");
System.out.print("First Number: ");
nOne = input.nextInt();
System.out.print("Second Number: ");
nTwo = input.nextInt();
input.close();
System.out.println("Sum: " + nOne + " / " + nTwo + " = " + (nOne /
nTwo));
}
public static void Exit(){
Scanner input = new Scanner(System.in);
System.out.println("Goodbye");
System.exit(0);
}
}
You need to give the dataype via the parameters. So you then have two methods like this:
you can then call the method division both with int and double and the according method will be chosen.