Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code in the following Java program is redundant, basically doing the same th

ID: 3871154 • Letter: T

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. 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");

    }

}

Explanation / Answer

import java.util.*;

public class Bills

{

public static int getBills(double amount)

{

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

if (numBills * 20.0 < amount) {

numBills++;

}

return numBills;

}

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();

int numBills1 = getBills(amount);

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

amount = console.nextDouble();

int numBills2 = getBills(amount);

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

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

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote