SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Getting Started
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

The video game machines at your local arcade output coupons according to how well you play the game. You can redeem 10 coupons for a candy bar or 3 coupons for a gumball. You prefer candy bars to gumballs. Write a program that defines a variable initially assigned to the number of coupons you win. Next, the program should output how many candy bars and gumballs you can get if you spend all of your coupons on candy bars first, and any remaining coupons on gumballs.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// CouponsDistribution.java
public class CouponsDistribution
{
	public static final int COUPONS_PER_CANDYBAR = 10;
	public static final int COUPONS_PER_GUMBALL = 3;

	public static void main(String[] args)
	{
		int numbeOfCouponsWin = 37;

		int numberOfCandybars = numbeOfCouponsWin / COUPONS_PER_CANDYBAR;

		int remainingCoupons = numbeOfCouponsWin % COUPONS_PER_CANDYBAR;

		int numberOfGumballs = remainingCoupons / COUPONS_PER_GUMBALL;

		remainingCoupons = remainingCoupons % COUPONS_PER_GUMBALL;

		System.out.println("Number of coupons win:       " + numbeOfCouponsWin);
		System.out.println("Number of candy bars get:    " + numberOfCandybars);
		System.out.println("Number of gumballs get:      " + numberOfGumballs);
		System.out.println("Number of remaining coupons: " + remainingCoupons);
	}
}

Output:

Number of coupons win:       37
Number of candy bars get:    3
Number of gumballs get:      2
Number of remaining coupons: 1
1 0

Discussions

Post the discussion to improve the above solution.