Section 1c

Turning a Sequence into a Program


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Programming

A computer program works the same way as any other sequential process. One command is implemented after the other. For example, supposed a programmer wanted to create a picture with three asterisks, and he has a printAsterisk command. The program in its simplest form would look like the following in Java:

    

public static void main( String[] args )
{ // print first asterisk
// method: printAsterisk
printAsterisk();

// print next asterisk
// method: printAsterisk
printAsterisk();

// print third asterisk
// method: printAsterisk
printAsterisk();
}

As one might might expect, there is much more required of a complete program including that it resides inside a class, however this is a good example of the code that makes a program work. Take a look at the components of this program:

  1. The program starts with a public static void main( String[] args). All programs will have one and only one main method, and this is the actual program from beginning to end; as mentioned, there is more to writing the complete programs, but this is the core component of a program. To expand on this code, the word public means that programmers have access to this method and can use it, the word static, as mentioned previously, means that there will be one and only one of this method, the void means it doesn't return any calculated or process results when it is done; results don't need to be returned since this method is actually called by the operating system. Finally, the name main is an identifier naming the method, and in some cases, if a user calls this class and the main method, she could put other arguments or identifiers on the same line (called a command line) as the class name and they would be brought into the program. The explanation of these items will help extend information about Java but at this point, none of this is important; the goal is to figure out how a program works.

  2. The program code is enclosed in curly braces that are indented as shown; this indicates all of the operations related to the main method; some tips for using the curly braces:

    1. indent them three spaces

    2. never type one curly brace without the other; when creating the main method now, and other methods later, always type the header line which is public static void main( String[] args) in this case, then press ENTER, type three spaces, type the open curly brace, then press ENTER two or three times to make some vertical spaces, then type three spaces and a close curly brace; getting into this simple habit now will prevent hours chasing missing curly braces later

  3. After the main method and curly braces are typed in, type the planned actions in as comments. Comments that go to the end of only one line are created by typing two forward slashes; so in this case, type // print first asterisk, start a new line and type // print second asterisk, start a new line and type // print third asterisk; at this point, the program has been written, but it has been written in English; the development process that will be used later on will include primarily typing the actions in English first, and then later translating them to code; note that comments are used for developing and clarifying what a program does, but they are ignored by the compiler so they do not have anything to do with the executable program

  4. Once the program is written in English, type the code for each operation with proper syntax; type the command exactly as spelled, then type an empty set of parentheses, and then add a semicolon to the end; some notes:

    1. the command is an identifier that conducts a specified operation; in this case it is a printAsterisk method that was written in advance; this is assumed to be a command, but from now on it will be called a member function, or more commonly, a method

    2. the open and closed parentheses are required for any method used in a program; to repeat, methods conduct many operations in programming, so they are treated as commands; and to repeat again, all methods require open and closed parentheses; as will be presented soon, there will be times when the parentheses will have things between them

    3. the semicolon announces to the Java compiler that the program statement is complete; this is a syntactical requirement of the Java programming language

    4. the code that takes care of printing an asterisk is a little more complicated than this one command; however, the printAsterisk method is an abstraction that covers up the details of conducting the task; the program is broken into contributing components called modules using this method, so the program is considered to be modular; this means the program is easier to develop by using modules, it is easier to understand and use since it is easy to see what each part does; and it will always be easier to diagnose and repair if anything goes wrong because if there is a failure, it is usually easy to see which module is causing the problem

  5. This discussion is simply showing how a program looks; however, in order to check programming code during the programming process, it is wise to compile the program after every statement; no more than one or two statements should be written at a time without compiling; the compiler checks the code syntax and indicates if something was missed or incorrectly implemented related to one or more of the rules of the programming language. This means the code can be fixed quickly and easy while it is still being worked on; programmers who write a bunch of code first and then compile should not be surprised at how many errors will pop up, and they will waste a lot of time trying to figure out where they are, and what they are

  6. Finally, notice that the program is simple and easy to read; the operations are clear and understandable; and white space is used to help keep the code readable; white space simply means that the code is not all jammed together and is easier to read; no programmer can guarantee a perfectly running program, however any programmer can guarantee easily readable, easily understandable, and well-developed programs

Again with consideration for the fact that there is a little more to the program than just the main method, the program written here simply outputs three asterisks to what is called the console window.

The results of the program previously discussed would be (as it would be found in the console window):

Image of output display

The above graphic shows the display in the console window which is commonly located at the bottom of the Eclipse screen. However, for reference, note that the coding area is above it, and in this case, it shows the program that was discussed.

Don't just watch this video, try it yourself. You will need the WorkingTestClass file so you can download it here as a zipped file.


The only other trick to writing these methods (aka commands) is that the parentheses can be helpful. When the three printAsterisk() methods were written, three asterisks were displayed. But there is sometimes an easier way. There might be a method that can print more than one asterisk; if there was, it might be plural, as in printAsterisks, and it would actually have a parameter (i.e., some data in between the parentheses that can be used to give the method the information it needs to do its job). Now, one method can do the job of several, as shown.

Image shows one printAsterisks function representing three printAsterisk functions

Don't just watch this video, try it yourself.


There will be much more discussion about methods and parameters as you progress through this reference. However, by looking at what you can do right now with the tools you are given, it means you can start writing programs immediately. Getting started with the programming will get you moving toward the more advanced activities.