The following question was taken from Absolute Java 5th ed. by Walter Savitch:
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.
This is the code that I have written:
public class Question2 {
private static final int NUMBER_OF_COUPONS_FOR_A_CANDYBAR = 10;
private static final int NUMBER_OF_COUPONS_FOR_A_GUMBALL = 3;
private static int numberOfCoupons = 13;
public static void main(String[] args) {
System.out.println(numberOfCandybars(numberOfCoupons)
+ " candybar(s) and " + numberOfGumballs(numberOfCoupons)
+ " gumball(s).");
}
private static int numberOfCandybars(int initialCoupons) {
return initialCoupons / NUMBER_OF_COUPONS_FOR_A_CANDYBAR;
}
private static int numberOfGumballs(int remainingCoupons) {
remainingCoupons = numberOfCoupons % NUMBER_OF_COUPONS_FOR_A_CANDYBAR;
return remainingCoupons / NUMBER_OF_COUPONS_FOR_A_GUMBALL;
}
}