Chapter Overview
Learn about the mixed fraction case study, which we will solve using different approaches.
Imperative programming forms the foundation of various programming paradigms, emphasizing a step-by-step sequence of operations for the computer to execute a specific task. In this paradigm, programmers provide a series of statements that outline the program’s execution flow, guiding the computer through the instructions. As one of the most widely used and fundamental programming approaches, imperative programming remains at the core of many programming languages.
Within the domain of imperative programming, there are several subparadigms that have evolved over time. Three of the most prominent subparadigms are procedural programming, structured programming, and object-oriented programming (OOP). Each of these subparadigms has its own unique characteristics and ways of organizing code, but they are all built upon the foundation of imperative programming.
Throughout this chapter, we will delve into the intricacies of procedural programming (PP), structured programming, and object-oriented programming (OOP). We will conduct a thorough examination of these approaches, grasping their appropriate applications and considering their respective advantages and disadvantages. Additionally, we will explore three primary data handling approaches, starting with primitive variables and advancing to the benefits of using structs and classes.
To facilitate our study, we will utilize a captivating case study titled “Mixed Fraction Calculator.” Brace yourself for an engaging exploration of programming paradigms and their nuances.
Before we dive in, let’s quickly review some mathematical concepts relevant to our case study. We will be translating these concepts into both procedural (using primitive variables and structs) and object-oriented paradigms (using classes).
Mixed Fraction: A case study
Before we start writing code for our case study, we need to understand the problem statement to develop a clear thought process. Our task is to create a mixed fraction arithmetic calculator that performs the following operations:
Addition
Subtraction
Multiplication
Division
What is a mixed fraction?
A mixed fraction has three components: whole number, numerator, and denominator, as shown in the diagram below:
Generally, to perform any arithmetic operation on mixed fractions, they must first be converted into improper fractions. In an improper fraction, we only have numerators and denominators. Here are the required mathematical steps for its conversion:
Multiply the denominator of the mixed fraction with its whole number.
Add the resultant of the product obtained in Step 1 to the numerator value of the mixed fraction to obtain the new numerator for the improper fraction.
The denominator for both fractions will remain the same.
Reduce the obtained fraction (convert to simplest form) by dividing the improper fraction’s numerator and denominator by their greatest common divisor (GCD).
Note: If the mixed fraction is a negative value, we take absolute values for each component while computing the numerator of the improper fraction. Sign is associated with the whole fraction and not on individual components.
Now that we know how to convert our term into an improper fraction, the next step is to learn the logic behind each arithmetic operation.
Addition and subtraction
Addition and subtraction are the two faces of the same coin. They follow the same mathematical sequence except at the last step, where we add or subtract the two values. To perform these operations, we need to have the same denominator for each fraction. To understand the method, let’s calculate the sum (addition) of two mixed fractions:
The first step, as you learned earlier, is to convert these mixed fractions to improper fractions:
Now, to add them, we can follow the following approach:
Making the same denominators for both fractions.
Multiply the fraction
's numerator and denominator with the denominator of the fraction —this will be our new fraction . Multiply the fraction
's numerator and denominator with the denominator of the fraction —this will be our new fraction .
Note: We can simply compute the resulting fraction’s denominator by multiplying the denominators of
and .
Add the numerator of both fractions—this will be the resultant numerator, and the denominator will be the same as calculated in Step 2 (we can pick anyone’s fraction denominator).
Reduce the fraction by dividing the final numerator and denominator with their GCD.
Convert it back to a mixed fraction.
Notes: Steps 3 and 4 can be performed in either order.
These steps are demonstrated below:
For subtraction, in Step 2, we will subtract the numerator values for both fractions to get the resultant numerator. The rest of the method remains the same.
Multiplication and division
Multiplication is very straightforward for fractions. Let’s talk about our previous example of fractions
In division, we take the reciprocal of the second fraction and then perform the multiplication operation.
Note: Here, we get a proper fraction when division is performed. Therefore, we don’t need to convert it to mixed fraction form.
Now, we know the process to perform each of these arithmetic operation on fractions. This will help us understand and formulate the coding logic for our case study.
In upcoming lessons, we will discuss different programming approaches and how to integrate their style into our mixed calculator problem.