Exercise: Free Coffee Cups


For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.

Knowing that I paid for cups cups, compute and print the total number of cups I would get (number of cups I paid for + number of cups I got for free).


Example:

For example, if cups was 12, the answer would be 14.

Why? Because I paid for 12 cups and I got 2 for free, which is a total of 14 cups.

Introduction

In this lesson, we will explore a common promotional scenario where you get a free item after purchasing a certain number of items. Specifically, for every 6 coffee cups you buy, you get an additional cup for free. This type of problem is significant in programming as it involves basic arithmetic operations and conditional logic, which are fundamental in many real-world applications.

Such scenarios are common in retail promotions, loyalty programs, and bulk purchasing discounts. Understanding how to compute the total number of items received, including free items, is essential for both consumers and businesses to evaluate the benefits of such promotions.

Understanding the Basics

Before diving into the solution, let's break down the problem:

For example, if you buy 12 cups, you get 2 free cups (one for each set of 6 cups), making a total of 14 cups.

Main Concepts

To solve this problem, we need to use basic arithmetic operations:

The logical flow is straightforward:

  1. Calculate the number of free cups by dividing the number of paid cups by 6.
  2. Add the number of free cups to the number of paid cups to get the total number of cups.

Examples and Use Cases

Let's look at a few examples to understand the concept better:

Common Pitfalls and Best Practices

Common mistakes to avoid:

Best practices:

Advanced Techniques

For more advanced scenarios, consider cases where the promotion rules change, such as getting a free cup for every 5 cups bought. The logic remains similar, but the divisor changes.

Code Implementation

Here is a Java implementation of the solution:

public class FreeCoffeeCups {
    public static void main(String[] args) {
        int cups = 12; // Example input
        int totalCups = calculateTotalCups(cups);
        System.out.println("Total cups: " + totalCups);
    }

    /**
     * This method calculates the total number of cups including free cups.
     * @param cups The number of cups paid for.
     * @return The total number of cups including free cups.
     */
    public static int calculateTotalCups(int cups) {
        // Calculate the number of free cups
        int freeCups = cups / 6;
        // Calculate the total number of cups
        return cups + freeCups;
    }
}

Debugging and Testing

To debug and test the code:

Example test cases:

Thinking and Problem-Solving Tips

When approaching similar problems:

Practice with different scenarios to improve your problem-solving skills.

Conclusion

In this lesson, we covered how to calculate the total number of coffee cups received, including free cups, based on a promotional offer. We discussed the basic concepts, provided examples, and implemented a solution in Java. Understanding such problems is essential for handling real-world scenarios involving promotions and discounts.

Keep practicing and exploring more complex scenarios to enhance your programming skills.

Additional Resources

For further reading and practice: