Answer:
Explanation:
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
spending(console,"John"); //calling spending method with "John"
spending(console,"Jane"); //calling spending method with "Jane"
}
public static void spending(Scanner console, String name){
System.out.print("How much will "+name+" be spending? ");
double amount = console.nextDouble();
System.out.println();
int numBill = (int) (amount / 20.0);
if (numBill * 20.0 < amount) {
numBill++;
}
System.out.println("John needs " + numBill + " bills");
}
Code Explanation
Method are reusable set of code which reduces redundancy. So the original code was trying to calculate the bill for John and Jane but the code to calculate was redundant which will cause more line of code and complex to manage. For example let say if we have 1000 of user to calculate there bill, will it be efficient to write bill calculation code for all 1000 users?
That's where function comes in to reduce redundancy from code and easy to manage.
Easy to Manage in a way, let say you need to change the bill calculation formula then without function, you have to change the formula for all the users but with function you only need to change the bill calculation formula in spending function.