3. Problem-solving refers to a
systematic approach to analyzing the
problem, developing a solution
(algorithm), and implementing that
solution in code.
5. Problem-Solving Approaches
When tackling coding problems,
there are two main approaches you
can take to break them down and
develop a solution.
1. Top-Down Approach
2. Bottom-Up Approach
6. Top-Down Approach
This approach starts with the big
picture and works its way down.
1. Understand the overall
problem and break it down
into smaller subproblems.
1. Solve each subproblem
independently, and then
combine the solutions to
form the final solution to
the larger problem.
7. Top-Down Approach:
Calculating Area
1. Identify the whole shape.
2. Divide it into simpler
shapes.
3. Calculate the area of
each simple shape.
4. Combine the areas to find
the total area.
8. Top-Down Approach:
Calculating Area
1. Identify the whole shape.
2. Divide it into simpler
shapes.
3. Calculate the area of
each simple shape.
4. Combine the areas to find
the total area.
10. Bottom-Up Approach
This approach starts with the
building blocks and works its way
up.
1. Start with the most basic
operations needed to solve
the problem.
1. Combine these operations
into larger functions to
create the overall
solution.
11. Bottom-Up Approach:
Counting Vowels
1. Implement a function to
identify vowels.
1. Implement a function to
iterate through the list.
Vowels : (a, e, i, o, u)
List: (b, d, c, a, z, x, i, l, f)
12. Identify vowels
// Basic Operation
private static boolean isVowel(char x) {
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < vowels.length; i++)
if (vowels[i] == x)
return true;
return false;
}
13. Iterate through list
// Larger Function
private static int countVowels(char[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++)
if (isVowel(arr[i]))
count++;
return count;
}