Question: The code in the following Java program is redundant, basically doing the same thing twice. Write a static method named getBills that eliminates this redundancy.

The code in the following Java program is redundant, basically doing the same thing twice. Write a static method named getBills that eliminates this redundancy. Rewrite method main to use your new method.

import java.util.*;

public class Bills {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

System.out.print("How much will John be spending? ");

double amount = console.nextDouble();

System.out.println();

int numBills1 = (int) (amount / 20.0);

if (numBills1 * 20.0 < amount) {

numBills1++;

}

System.out.print("How much will Jane be spending? ");

amount = console.nextDouble();

System.out.println();

int numBills2 = (int) (amount / 20.0);

if (numBills2 * 20.0 < amount) {

numBills2++;

}

System.out.println("John needs " + numBills1 + " bills");

System.out.println("Jane needs " + numBills2 + " bills");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!