5.6 Writing Methods
The last two sections looked at creating two specific types of class method focused on accessing and updating instance variables. What if you want to do something else, like perform a calculation or print out a result? Using class methods, you can write your own behaviors for a class.
Recall from Unit 2 where you first methods, a method header is the first line that is used to define a method. For example:
In the above example, since the method is a non-void method with a parameter, the method would take the formal parameter (parameter in the method) value in, use it, and then return a computed value. The return value needs to match the return type.
When a primitive value is passed to the method, the formal parameter is initialized with a copy of the value. In other words, any changes to the formal parameter in the method will not change the actual parameter in the calling method.
Here is an example. Given a Numbers
class:
If the following code is executed to call the addFive
method:
The output will be 17 however, the value of a
will remain unchanged at 12.
In contrast, when an object is passed to a function as a parameter, the formal parameter is initialized with a copy of the reference, not a copy of the object. What does this mean?
As a copy of the reference, both the original reference and the copy point back to the same object. This makes the actual parameter and formal parameter aliases that refer to the same object. Assuming the object is mutable, updates in the method will be reflected in the original value also getting updated.
As a note, it is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
If the formal parameter is a reference object, you can actually access the private data associated with that reference if the method has been written in the object’s class file. In the example below, because MyProgram is not the file that the Rectangle class was written in, you have to use rect.setWidth()
and rect.setHeight()
to access the instance variables.
MyProgram.java
If the method was written inside the Rectangle class file, then you could access the object’s data by using the .variable notation. Notice that instead of using setWidth
and setHeight
, you can just change the value of width and height by writing rect.width
and rect.height
. This works when the parameter reference object is the same as the class file.
Rectangle.java
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Each of the methods below can be found in the
Student
class. Which of the following methods would have access to the parameter object’s private data and methods?
Implement the class Distance
.
This class has one instance variable, a double
called feet
. The class has methods that convert the feet into different units.
It should have the following methods:
public Distance(double startFeet)
- the constructor; initializesfeet
public double toYards()
- converts feet to yards. To convert to yards, divide feet by 3.public double toInches()
- converts feet to inches. To convert to inches, multiply feet by 12.public double getFeet()
- returns the value offeet
Main Method
To test your class, create three Distance
objects in main
. One represents the distance between Karel and the gymnasium, Karel and the cafeteria, and Karel and his best friend.
Karel is 213 feet from the gymnasium. Karel is 128 feet from the cafeteria. Karel is 10.5 feet from his best friend.
Your program should use the methods from Distance to print the number of:
- yards Karel is from the gymnasium
- feet Karel is from the cafeteria
- inches Karel is from his best friend