Join Peggy Fisher for an in-depth discussion in this video Understanding algorithms, part of Learning Java (2015).
- An algorithm is a set of steps needed to solve a problem. It is written in our native tongue, and not in any particular programming language. It helps the programmer to think through the problem and describe a possible solution without worrying about the implementation. Let's start by using an example that we might be familiar with. When we want to make cookies, we follow a recipe. The recipe starts by identifying all the ingredients we need, then it provides a list of steps that need to be executed in sequence.
For example, if the recipe is for chocolate chip cookies, we have to measure the ingredients, preheat the oven, mix the ingredients scoop the cookie dough onto a cookie sheet. After the oven is preheated, we can place the raw cookie dough in the oven for a specified amount of time. Then we remove the cookies and let them cool. Finally, the best part, we get to eat the cookies, with milk of course. What would happen if we didn't provide any measurements for the ingredients? Or maybe we forgot to preheat the oven.
Or what if we tried to place the ingredients in the oven without mixing them first? Just like a recipe, we need to follow a series of steps to complete our program. Let's try this approach on another problem. We want to write a program that will help us to determine how much gas we need to go from Philadelphia to Boston, and how much it will cost. We need to know the distance of the trip, we need to know what is the miles per gallon of your vehicle, and the current price of gasoline. Now that we have described our problem, let's figure out our algorithm for this solution.
First, we'll ask the user for the miles to be traveled. In our case, how many miles from Philadelphia to Boston? Now that we have the information we need, we can calculate the amount of gas needed for the trip, using the formula total miles to be driven divided by miles per gallon. Next, using the total gallons from the previous step, find out the expected cost by multiplying the number of gallons times the price of one gallon of gas. And finally, print the results to the user.
In this example, instead of ingredients, our variables are distance, MPG or miles per gallon, current cost of one gallon of gasoline, total gallons needed, and finally, we need a variable to hold the total gas cost for the trip. When our algorithm is finished, we can use this information to write our program. We start with the variables and define them with a specific data type. We'll review some of the most common data types when we start programming in a little bit.
Similar to the measurement units in our recipe example, such as teaspoon or cup, when programming, we need to use data types such as integer or double. From here we can take our algorithm and write code in any programming language.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs