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

Suppose you can buy a chocolate bar from the vending machinie for $1 each. insid

ID: 3623849 • Letter: S

Question

Suppose you can buy a chocolate bar from the vending machinie for $1 each. inside every chocolate bar is coupon.you can redeem seven coupons for one chocolate bar from the machine. then you wanted to know how many chocolate bars you can eat including those redeem via coupon,with n dollars. for example , if you have $20 dollars then you can initially buy 20 chocolate bars. these two additional chocolate bars give you two more coupons,so you now have a total of 8 coupons. this gives you enough to redeem for one final chocolate bar. as a result you now have 23 chocolate bars and two leftover coupons.

Explanation / Answer

Here's a method that that tells you how many bars you will eventually get for your money, assuming you redeem any coupons that you might get.

   public static int buyBars(int dollars) {
        int coupons = 0;
        int bars = 0;
        while (dollars > 0) {
            // buy a bar
            dollars -= 1;
            bars += 1;
            coupons += 1;
            if (coupons == 7) {
                // redeem coupons for a chocolate bar
                coupons = 0;
                bars += 1;
                coupons += 1;
            }
        }
        return bars;
    }

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