Introduction To Programming When our programs are very small, we can usually just write them. We don't really need to plan much. However, modern software can have hundreds of thousands of lines of code. Maybe some planning is appropriate? During this semester, as our programs get more and more complex, we're going to explore ways to think and plan out what to do.
We will start working on our programs with a problem statement. These are a few paragraphs that describe the program that you will be writing. This is usually written by non-programmers and is intended to communicate to you, as best they can, what they want. Here are some examples.
Write a program that asks a user for 10 numbers. The program will then determine the smallest number, the largest number, and calculate the average of the number. The program will display the three calculated values.
Write a program that asks a user to enter the height of a child for each year of their life. The program will then determine which year the child grew the fastest and which year the child grew the slowest and output the calculated values.
Write a program that asks a user to enter a temperature in Fahrenheit and convert it to Celsius. The program will display the Fahrenheit temperature and the calculated Celsius temperature. A Celsius temperature is calculated by the formula 5 / 9 times Fahrenheit temperature minus 32.
The first step in our planning is to look at the important words in problem statement. To which nouns and verbs do I have to pay attention? Here's how do to this with some of our examples. Here's the first one:
The important nouns have been highlighted:
The Nouns (and their adjectives)
The next thing we do is look at the important verbs.
The Verbs
The next step is to take these nouns and verbs and make some checklists. This is a quick and fairly informal process. Don't get too hung up with being perfect or complete. We are trying to capture the high-level lists of key items in the program. The three lists we want to make are:
Look at the nouns, which ones are inputs? Write those down as JavaScript variables.
Next, look at the nouns for things that the program will need to output or display. Sometimes you will need to display the inputs. Sometimes you will need to display only the outputs. And sometimes a combination of both.
Here's where we start in on developing what our program actually does. This list will be very high-level, you don't need to have any detail! Look at the verbs. Do you see a pattern?
Those verbs are candidates for being on our list of steps. The first verb is "ask". What you are doing is getting input. That will be your first item on your list of steps you program is going to do. We're just going to convert it a little so it sounds more like we are programming it.
Then next two verbs are "determine" and "calculate". This is the heart of the program. It's what the program is all about. The process step just has to say a little more.
The last verb is "display". This is the output step. Be brief!
Now put them altogether.
Read this to yourself, does it sound complete? Does is sound like a program? Now let's make it into a program!