Please enable JavaScript to use CodeHS

Chapter 5

Writing Classes

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.

Recap: The Method Header

Recall from Unit 2 where you first methods, a method header is the first line that is used to define a method. For example:

public int addFive(int num)
Plain text

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.

Passing a Primitive Value

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:

public class Numbers {
  public int addFive(int num) {
    num += 5;
    return num;
  }
}
Plain text

If the following code is executed to call the addFive method:

Numbers myNum = new Numbers();
int a = 12;
System.out.println(myNum.addFive(a));
Plain text

The output will be 17 however, the value of a will remain unchanged at 12.

Passing an Object Value

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.

Access to Private Data and Methods

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

public void resetRect(Rectangle rect) {
  rect.setWidth(0);
  rect.setHeight(0);
}
Plain text

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

public void resetRect(Rectangle rect) {
  rect.setWidth(0);
  rect.setHeight(0);
}
Plain text

Triangle Class

Baseball Player Class

Check Your Understanding

  1. 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?

Exercise: Distance Conversions

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; initializes feet
  • 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 of feet

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