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

Suppose amount is an int variable that contains a number between 1 and 99 (repre

ID: 3915201 • Letter: S

Question

Suppose amount is an int variable that contains a number between 1 and 99 (representing a number of cents for which you need to calculate how to make change). Write a fragment of Java which is a loop that runs the correct number of times for how many ways quarters (worth 25 cents) can be used to make change from coins to equal value in amount. Use the variable numQuarters in the loop someway. The loop will need to run between 3 and 0 times depending on amount. Make sure the first time the loop runs it uses the most quarters possible. For example, if amount contains 53, the loop should in order give numQuarters the values 2, 1, 0. You don’t need to do anything in the loop except be certain numQuarters goes through the correct set of values in the correct order.

Write your answer here (as a fragment of Java using the variables amount and numQuarters):

Explanation / Answer

Here is the required code fragment which allows you to perform the required operation using a do while loop.

// code fragment starts here

/**

* finding the maximum number of quarters possible (here 25 is used

* because 1 quarter=25 cent)

*/

int numQuarters = amount / 25;

// looping until the value of numQuarters is zero

do {

                // do whatever you have to do here.

                numQuarters--; // decerementing numQuarters to get the next value

} while (numQuarters >= 0);

// code fragment ends here

Below is the complete program for testing this code snippet

//Change.java

public class Change {

                public static void main(String[] args) {

                                int amount = 53;

                                // code fragment starts here

                                /**

                                * finding the maximum number of quarters possible (here 25 is used

                                * because 1 quarter=25 cent)

                                */

                                int numQuarters = amount / 25;

                                // looping until the value of numQuarters is zero

                                do {

                                                // do whatever you have to do here.

                                                System.out.println("Current value of numQuarter: " + numQuarters);

                                                numQuarters--; // decerementing numQuarters to get the next value

                                } while (numQuarters >= 0);

                                // code fragment ends here

                }

}

/*OUTPUT*/

Current value of numQuarter: 2

Current value of numQuarter: 1

Current value of numQuarter: 0

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