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

Suppose we can buy apple juice from the vending machine for $2 each. On every ap

ID: 646025 • Letter: S

Question

Suppose we can buy apple juice from the vending machine for $2 each. On every apple juice is a coupon. We can redeem 4 coupons for one apple juice from the machine. We would like to know how many apple juice cans can be consumed, including those redeemed via coupons, if we have n dollars. For example, if we have 20 dollars then we can initially buy 10 apple juices. This gives us 10 coupons. We can redeem 8 coupons for 2 additional apple juices. These two additional apple juices have 2 more coupons, so we now have a total of 4 coupons when added to the 2 leftover from the original purchase. This gives us enough to redeem for one final apple juice. As a result we now have 13 apple juices and 1 leftover coupon. Write a program that inputs the number of dollars and outputs how many apple juice cans you can collect after spending all your money and redeeming as many coupons as possible. Also output the number of leftover coupons. The easiest way to solve this problem is to use a loop. Hint: Use a while loop that redeems coupons for juice and calculates the new number of coupons. Continue the loop as long as you have enough coupons to redeem for apple juice.

sample run:

Enter the number of dollars you have to spend: 20

you can get 13 juice cans with 1 coupon leftover.

process returned 0(0x0);

Explanation / Answer

import java.util.Scanner;


public class VendingMachine {
   public static void main(String args[]){
       Scanner in = new Scanner(System.in);
       System.out.print("Enter the number of dollars you have to spend: ");
       int m = in.nextInt();
       int coupons = 0;
       int num = m / 2;
       coupons += num;
       while(coupons >= 4){
           num += coupons / 4;
           coupons = (coupons / 4) + (coupons % 4);
       }
       System.out.printf("you can get %d juice cans with %d coupon leftover.", num, coupons);
   }
}

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