discuss changes to both primitive and reference formal parameters in methods
Up until this unit, you wrote all of your code in the main method, but now we are using lots of methods!
Why have multiple methods instead of just one?
Procedural abstraction allows us to name a block of code as a method and call it whenever we need it, abstracting (hiding) away the details of how it works.
Whenever I see that word "abstraction" I think to myself "hiding away the minor details so I can focus on the big ideas".
In other words, once you create a method and you know that it works, you can hide it away and use it without having to worry about how it works.
This serves to organize our code by function and reduce its complexity by reducing the need for the repetition of code.
In other words, when you find yourself copying and pasting a lot, maybe you could turn those lines of code into a method!
This also serves to organize our code by function and reduce its complexity and reduce the repetition of code.
In addition, it helps with debugging and maintenance since changes to that block of code only need to happen in one place.
Let's look at an example with lots of repetition of code and create methods to reduce the repetition of code:
Running the code above, this is the output in the console:
Do you see that there are some repeated lines of code in the This Old Man song?
The chorus is repeated "With a knick knack paddy whack, give a dog a bone. This old man came rolling home."
This repeated code is a signal to make a new method!
CREATING AND CALLING METHODS
We have been creating methods already, but I just want to clarify the THREE steps to creating and calling methods:
1) Object of the Class: Declare an object of your class in the main method or from outside the class.
2) Method call: whenever you want to use the method, call objectName.methodName();
3) Method Definition: write the method's header and body code like below:
For example, here is a chorus() method definition that we could write for the "This Old Man Song":
Notice that I created a Song class for this method called Song.java.
Also notice that there are no instance variables, or constructors in the Song class!
Now that I have a chorus() method, I can call it from the main method!
Running the code above, you would get the same output in the console:
PARAMETERS
We can make methods more powerful and more abstract by giving them parameters for data that they need to do their job.
When you create your own method, the variables you define for it in the method header are called formal parameters.Â
When you call the method to do its job, you pass in arguments or actual parameters to it that are then saved in these local parameter variables.
When a method is called, the right method definition is found by checking the method signature or header at the top of the method definition to match the method name, the number of arguments, the data types for the arguments and the return type.
Taking a look at the lines of each verse, we see there is more repetition in the song above.
Notice that every word is repeated except the last words that include a number and rhyming word such as one/thumb and two/shoe:
We could make a method called verse that takes the number and rhyming words as parameters to print out any verse!
Now that I have chorus() and verse() methods, I can call them from the main method!
Running the code above, you would get the same output in the console:
CALL BY VALUE & ALIASES
Java uses Call by Value when it passes arguments to methods.
This means that a copy of the value in the argument is saved in the parameter variable.
If the parameter variable chances its value inside the method, the original value outside the method is not changed.
If you pass an argument that holds a reference to an object, like a String or Person object, a copy of this reference is passed in and saved in the parameter variable.
The formal parameter and the actual parameter (argument) are then aliases, both referring to the same object.
Java was designed this way to avoid copying large objects from method to method.
Remember when we discussed reference aliases with Turtle objects who are set equal to one another:
Although String objects are not mutable, the class that you create will have mutable objects.
If the reference parameter is for a mutable object, the method could change the actual object.
However, it is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
Methods can even access the private data and methods of a parameter that is a reference to an object if the parameter is the same type as the method's enclosing class.
Note that Strings are immutable objects, so they cannot be changed by the method; only a new changed copy of them can be made.
Methods can also return values of any type back to the calling method.
The calling method should do something with this return value, like printing it out or saving it in a variable.
The following code shows a String method that takes a parameter and returns a boolean value:
In the main method, you can see that I created a new StringFind object named test in order to test the message "Apples and Oranges" for the letter "p":
Notice that I also called another method called findCount() on the test message in order to count how many times the letter was found:
This is the output when I searched for different letters:
SUMMARY
Procedural Abstraction (creating methods) reduces the complexity and repetition of code. We can name a block of code as a method and call it whenever we need it, abstracting away the details of how it works.
A programmer breaks down a large problem into smaller subproblems by creating methods to solve each individual subproblem.
To write method, write a method definition with a method signature like "public void chorus( )" and a method body in curly braces { } and method calls using object.method( ); and arguments whenever you need it to do its job.
To call an object's method, you must use the object name and the dot ( . ) operator followed by the method name, for example object.method( );
When you call a method, you can give or pass in arguments or actual parameters to it inside the parentheses object.method(arguments). The arguments are saved in local formal parameter variables that are declared in the method header, for example: public void method(type param1, type param2) ( { ... }.
Values provided in the arguments in a method call need to correspond to the order and type of the parameters in the method signature.
When an actual parameter is a primitive value, the formal parameter is initialized with a copy of that value. Changes to the formal parameter have no effect on the corresponding actual parameter.
When an actual parameter is a reference to an object, the formal parameter is initialized with a copy of that reference, not a copy of the object. The formal parameter and the actual parameter are then aliases, both referring to the same object.
When an actual parameter is a reference to an object, the method or constructor could use this reference to alter the state of the original object. However, it is good programming practice to no modify multiple objects that are passed as parameters unless required in the specification.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.
3) Here is another song that is very similar to the This Old Man song in its repetitive structure:
Your task is to create a method or methods that take parameters to print out the chorus and verses.
The method(s) should be abstract enough to work for all three verses.
In the main method, create an object of the class and call the method(s) you created to print out all 3 verses of the song.
Use your creativity to create and print out a 4th verse!
Make sure to show me your code when you've finished this program.