5.6. Writing Methods¶
Up until this unit, you wrote all 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 away the details of how it works. This 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. Here are some of the main reasons to use multiple methods in your programs:
Organization and Reducing Complexity: organize your program into small sections of code by function to reduce its complexity. Divide a problem into subproblems to solve it a piece at a time.
Reusing Code: avoid repetition of code. Reuse code by putting it in a method and calling it whenever needed.
Maintainability and Debugging: smaller methods are easier to debug and understand than searching through a large main method.
Let’s look at an example with lots of repetition of code and create methods to reduce the repetition of code. You can sing along here https://www.youtube.com/watch?v=Di23O5cN4ZU&ab_channel=Rock%27NLearn .
public static void main(String args[]) { System.out.println("This old man, he played one."); System.out.println("He played knick knack on my thumb. "); System.out.println("With a knick knack paddy whack, give a dog a bone."); System.out.println("This old man came rolling home."); System.out.println("This old man, he played two."); System.out.println("He played knick knack on my shoe. "); System.out.println("With a knick knack paddy whack, give a dog a bone."); System.out.println("This old man came rolling home."); }
Did you find some repeated lines of the This Old Man song? You may have noticed that the chorus is repeated “With a knick knack paddy whack, give a dog a bone. This old man came rolling home.” When you see repeated code, that is a signal for you to make a new method!
There are three steps to creating and calling a method:
Object of the Class: Declare an object of your class in the main method or from outside the class.
// Step 1: declare an object in main or from outside the class Classname objectName = new Classname();
Method Call: whenever you want to use the method, call objectName.methodName();
// Step 2: call the object's method objectName.methodName(); //Step 2
Method Definition: write the method’s header and body code like below:
// Step 3: Define the method in the class // method header public void methodName() { // method body for the code }
For example, here is a chorus() method definition that we could write for the “This Old Man Song”:
public void chorus()
{
System.out.println("With a knick knack paddy whack, give a dog a bone.");
System.out.println("This old man came rolling home.");
}
Run the following code to see the song This Old Man print out. Can you replace the last two lines in the second verse in the main method with a call the chorus() method instead? You can also see this code run in the Java visualizer by clicking on the Code Lens button.
5.6.1. Parameters¶
You may have noticed more repetition in the song above. What about the lines of each verse? Notice that every word is repeated except the last ones that include a number and a rhyme such as one/thumb and two/shoe.
System.out.println("This old man, he played one.");
System.out.println("He played knick knack on my thumb.");
...
System.out.println("This old man, he played two.");
System.out.println("He played knick knack on my shoe.");
We can make methods even more powerful and more abstract by giving them parameters for data that they need to do their job. We can make a method called verse that takes the number and the rhyme to print out any verse!
public void verse(String number, String rhyme)
{
System.out.println("This old man, he played " + number);
System.out.println("He played knick knack on my " + rhyme);
}
Run the following code to see the song This Old Man print out using the verse and chorus methods. You can also see this code run in the Java visualizer by clicking on the Show Code Lens button below. Can you add verse three with the rhyme “knee”? Can you add verse four with the rhyme “door”? How many verses do you know?
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 give or 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.
Here’s what that looks like with the 2 method calls above. Notice how the parameter variables get new values with every method call.
Figure 1: Matching Arguments to Parameters¶
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 changes its value inside the method, the original value outside the method is not changed.
If you pass in an argument that holds a reference to an object, like a String or Person or Turtle 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 refering 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.
Figure 2: Turtle Reference Equality¶
(Advanced topics warning): Although String objects are not mutable, the classes 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 assigning it to a variable. Try the problems below to practice with a String method that takes a parameter and returns a boolean value.
Run the following program which contains a method called findLetter that takes a letter and a text as parameters and uses a loop to see if that letter is in the text and returns true if it is, false otherwise. Set the variables letter
and message
to new values in the main method and run it again to try finding a different letter. Then, change the code of the findLetter method to return how many times it finds letter in text, using a new variable called count
. How would the return type change?
5.6.2.
Programming Challenge : Song with Parameters¶
Here’s another song, The Ants Go Marching, that is very similar to the This Old Man song in its repetitive structure. Notice that the verses below have a lot of repeated words and phrases. Click on the words or phrases that are different in each verse. These will be the arguments that you will pass to the methods in your song code.
The ants go marching one by one, hurrah, hurrah The ants go marching one by one, hurrah, hurrah The ants go marching one by one The little one stops to suck a thumb And they all go marching down to the ground To get out of the rain, BOOM! BOOM! BOOM! BOOM! The ants go marching two by two, hurrah, hurrah The ants go marching two by two, hurrah, hurrah The ants go marching two by two The little one stops to tie a shoe And they all go marching down to the ground To get out of the rain, BOOM! BOOM! BOOM! BOOM! The ants go marching three by three, hurrah, hurrah The ants go marching three by three, hurrah, hurrah The ants go marching three by three The little one stops to climb a tree And they all go marching down to the ground To get out of the rain, BOOM! BOOM! BOOM! BOOM!
In the active code window below, create a method or methods that takes parameters to print out a verse. The method(s) should be abstract enough to work for all 3 verses. Use good commenting for your methods that describe the @param. For the autograder, make sure you create a method called verse that takes 2 parameters.
In the main method, create an object of the class and call the method(s) you created in the last step to print out 3 verses of the song. Can you add more verses?
Create method(s) with parameters to print out verses of the song The Ants Go Marching. https://youtu.be/QPwEZ8Vv2YQ/The+Ants+Go+Marching
5.6.3.
Design a Class for your Community¶
In lessons 5.1 and 5.2, you came up with a class of your own choice relevant to your community.
Copy your class with its 3 instance variables, constructor, and its print() and main methods from lesson 5.2 into the active code exercise below.
Create accessor (get) methods and mutator (set) methods for each of the instance variables.
Create a
toString
method that returns all the information in the instance variables.Write an additional method for your class that takes a parameter. For example, there could be a print method with arguments that indicate how you want to print out the information, e.g.
print(format)
could print the data according to an argument that is “plain” or “table” where the data is printed in a table drawn with dashes and lines (|
). Or come up with another creative method for your class.Use these methods in the main method to test them. Make sure you use good commenting.
Copy your class from lesson 5.2. Add get, set, toString, and a method that takes a parameter. For example, there could be a print method with arguments that indicate how you want to print out the information, print(format) where format is “plain” or “table”.
5.6.4. 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 methods, write a method definition with a method signature like “public void chorus()” and a method body in {} and method calls using an object.the method name 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 refering 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 not modify mutable objects that are passed as parameters unless required in the specification.
5.6.5. AP Practice¶
amount += dollars; return dollars;
-
dollars should be incremented by amount.
dollars = amount; return amount;
-
dollars should be incremented by amount.
dollars += amount; return dollars;
-
Correct.
dollars = dollars + amount; return amount;
-
amount is returned instead of dollars.
amount = dollars + amount; return dollars;
-
dollars should be incremented by amount.
5-6-8: Consider the following class, which uses the instance variable dollars to represent the money in a wallet in dollars.
public class Wallet
{
private double dollars;
public double putMoneyInWallet(int amount)
{
/* missing code */
}
}
The putMoneyInWallet method is intended to increase the dollars in the wallet by the parameter amount and then return the updated dollars in the wallet. Which of the following code segments should replace missing code so that the putMoneyInWallet method will work as intended?
I only
-
I would work but it is not the only code that would work.
II only
-
II does not check against the boilingPoint and does not return only boolean values.
III only
-
III would work but it is not the only code that would work.
I and III only.
-
Correct!
I, II, III
-
II does not check against the boilingPoint and does not return only boolean values.
5-6-9: Consider the Liquid class below.
public class Liquid
{
private int currentTemp;
private int boilingPoint;
public Liquid(int ct, int bp)
{
currentTemp = ct;
boilingPoint = bp;
}
public boolean isBoiling(int amount)
{
/* missing code */
}
}
The isBoiling method is intended to return true if increasing the currentTemp by the parameter amount is greater than or equal to the boilingPoint, or otherwise return false. Which of the following code segments can replace missing code to ensure that the isBoiling method works as intended?
I. if (currentTemp + amount < boilingPoint)
{
return false;
}
else
{
return true;
}
II. if (amount > currentTemp)
{
return false;
}
else
{
return currentTemp;
}
III. if (amount + currentTemp >= boilingPoint)
{
return true;
}
else
{
return false;
}