Section 1f

Making Programs (Even More) Easier


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Easier, Faster, Funner

Simplify, Simplify, Simplify. You have now seen the creation of a couple of programs that follow your commands. You have seen simple text pictures drawn by a program, and this was done with only three methods. Things look pretty good so far. However, one thing that was kind of a problem was the fact that in order to accomplish these tasks you would need to copy and paste all those methods like printAsterisk and printSpace repeatedly in order to add more asterisks or spaces to your program. Wouldn't it be easier if you could reduce the number of lines of code and the amount of text manipulation that you have to implement?

Answer: Of course. Especially if someone wants you to start making other shapes likes diamonds, rectangles, and octagons.

What are we improving? To create more complicated shapes, you could continue to use the tools you have. Using the tools you have seen up to this point would allow you to implement all the tasks with which you might be challenged. However, in Computer Science, the goal is not always just to solve a problem or create a program. In fact, there are cases where just solving the problem or creating the solution is simply not good enough. As you have read before, program code that is not easy to read and/or easy to maintain and improve, is never good enough. You heard it here again.

You want to create programs that are easy to develop, easy to read, and easy to maintain and improve. Using parameters in the kinds of programs you have been working on is once again going to reduce the number of required methods for each action, and it is going to make the code easier to understand. For example, you could type printAsterisk(); and then copy and paste it several times to accomplish the task of printing several asterisks. But wouldn't it be easier to just tell the computer "Print three asterisks", or "Print seven asterisks", etc? Wouldn't it be easier to use only one method that prints a number of asterisks specified by you, the programmer? This concept, discussed briefly in the previous chapter, is what you will look at next. Two changes will be required: 1) you will need a new method since the previous methods conduct a specific activity, and this cannot be changed without changing the method itself, and 2) you will be using parameters you observed previously in the new method.

What's a parameter? A parameter provides information to a method that is needed for the method to do its work. For example, if you have hired a car painter, you will need to tell him which car to paint, and which color it needs to be painted. A method is just like a car painter, or any other service person for that matter, in that it will consistently and competently take care of its dedicated task, but there are times it will need to know some things to accomplish that.

In the case of the printAsterisk method, its task was to print an asterisk and it did not need any further information to conduct its task. So, to print five asterisks, the printAsterisk method would be used as shown here.

printAsterisk();
printAsterisk();
printAsterisk();
printAsterisk();
printAsterisk();

However, if another method called printAsterisks (notice the plural word) was called, you would expect it to print more than one asterisk, simply by reading the name of the method. Side note: this is the definition of good self-documentation. However, you have to ask the question, "How can it know how many asterisks to print?", and you will have asked a great question. The answer is that you will pass it a parameter that lets it know, as shown here.

printAsterisks( 5 );

That was easy. Any time you need to use -- and later on to create -- a method, you will need to think about what the method will do if left to its own devices, and what information you will need to provide to it. Providing information to a method is called "passing" the information to it, and since the values within the parentheses are called parameters, it is said that you are "passing one or more parameters".

First side note. Since a copy of the value is passed in the parameter list, we say that this is a copy parameter, or that the action was "pass by copy". This is the only way Java passes parameters and we will need to further discuss this later.

Second side note. For this and most educational examples, it was more clear to use a constant value (i.e., "5"), also called a literal value. However, even though literals may be used initially, virtually all methods should be called with variable parameters for clarity and readability of the code. The following would be the appropriate way to call this method:

// Given that "numberOfAsterisks" represents the value 5,
// We can call the method as follows:
printAsterisks( numberOfAsterisks );

Watch this video and then write your own rectangle-making method with the methods that use parameters.

Watch this video and then write your own triangle-making method with the methods that use parameters.

Now write your own program that creates a diamond using these new methods. You can use numbers as parameters if you wish, or you can use variables as you saw in the videos; remember that you will have to change the number in the variable for each different length line of spaces or asterisks. Either way, this is not difficult but your practice with writing these small programs is very important.

Image of diamond

Finally write your own program that creates an octagon using these new methods. Again, it is up to you whether you use numbers or variables in your method parameters. The important thing is to keep practicing with program writing.

Image of octagon