Applying STAIR to the Pig Latin Program

The STAIR concept is nice in an academic sort of way, but its real power comes when you try to solve a problem of any complexity. To illustrate this, imagine trying to write the Pig Latin program right now, before you finish the chapter. You have all the tools you need, but you don’t have a plan. I suspect that it would be a frustrating exercise. Use the STAIR process to set up a plan for your program. This makes the programming experience much more pleasant.

Stating the Problem

The following paragraph explains how I thought about this problem.

Create a program that demonstrates string manipulation and looping structures. The program should ask the user for a phrase and then translate that phrase into pig latin. The program should break the phrase into words and then look at the first character of each word. If the initial character is a vowel, the program should simply add way to the end of the word. If the first character is a consonant, the program should remove that consonant from the beginning of the word, add the consonant to the end of the word, and add ay after the consonant. The program should then ask for another phrase. When the user types quit as the phrase, the program should exit.

Notice how complete and well thought out this statement of the problem is. It could still be improved, but it’s a good starting place. Writing a program from a statement like this is much easier than from one that simply states, “Make a Pig Latin program.”

Identifying the Tools

This program is likely to need the following tools:

  • A while loop to control continuing the program until the user enters quit

  • Some type of input to get the phrase from the user

  • A foreach…split structure to break the phrase into words

  • A variable to hold each word (word)

  • The String.SubString() method to divide the word into first character and other characters

  • Variables for the first character (firstChar) and the rest of the word (restOfWord)

  • A condition to see whether the first character is a vowel

  • A WriteLine statement to send the completed pig latin phrase to the screen

Other tools might be necessary, but this list will do as a starting point. In essence, I built this list by looking again at the statement of the problem and looking at the kinds of tools I anticipate using to solve the various little problems along the way.

Creating the Algorithm

The algorithm works by restating the problem in terms of the tools. At this point, I usually do two things: I try to get the statements in the correct order, and I begin to flesh out some of the details. This results in a stylistic language that is neither English nor a programming language. It’s frequently called pseudocode by people who name such things. Although there are some formal definitions for pseudocode, I generally write short statements that I know I’ll be able to translate into the language I’m writing in. Here’s my first crack at pseudocode for the Pig Latin program: (Note that in this book, pseudocode will be in italics):

Pseudocode:

Create variables, word, firstChar, restOfWord, sentence, pigWord
						while user has not said "quit"
						  ask user for sentence
						  look at sentence one word at a time (foreach)
						     extract firstChar, restOfWord
						     if firstChar is a vowel,
						         pigWord = word + "way"
						     otherwise
						         pigWord = restOfWord + firstChar + "ay"
						     end if
						  end foreach loop
						end while loop

This pseudocode is extremely useful because it provides a blueprint for finishing the program. Implementing the program simply requires fleshing out the pseudocode with C# code. Notice that I still use English-like phrases, such as user has not said "quit". As long as you have an idea how to translate the pseudocode into actual code, using a form of English is fine. However, you often find that you don’t know exactly how to proceed on a particular piece of your code. For example, how do you write code to determine whether a certain letter is a vowel? You can do this a number of ways, for example, by using the switch structure and a series of else if clauses. Later, I’ll show you another strategy that’s more compact. When a part of your algorithm is not ready to implement, you can apply the STAIR process again to that smaller piece of the problem until you are satisfied that you will be able to implement the algorithm.

Implementing and Refining

The implementation and refinement steps aren’t really a part of the program-planning process. The implementation is the actual code, which you will see in the next section. Refinement is an ongoing process, so I’ll show you how I had to refine the plan as I wrote the actual code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.17