Skip to main content

Section 10.6 Worked Example: Writing Classes - Getters and Setters

Subgoals for Writing a Class.

  1. Name it
  2. Differentiate class-level static vs. instance/object-level variables
  3. Differentiate class-level static vs. instance/object behaviors/methods
  4. Define instance variables (that you want to be interrelated)
    1. Name
    2. Data Type
    3. private
  5. Define class variables static as needed
    1. Name
    2. Data Type
    3. public / private / final
  6. Create constructor (behavior) that creates initial state of object
    1. Overloaded constructor (with as many parameters)
    2. public
    3. Same name as class
    4. No return type
    5. Default - no parameters
    6. Logic - initialize all variables
    7. Repeat as needed, adding parameters
  7. Create 1 accessor and 1 mutator behaviors per attribute
    1. Accessors
      1. Name is get_<attr_name>
      2. Public
      3. Return type same data type as attribute
      4. No parameters
      5. Logic - return value
    2. Mutators
      1. Name is set_<attr_name>
      2. Public
      3. Return type is void
      4. Parameter is same data type as attribute
      5. Logic validates input parameter and sets attribute value
  8. Write toString method
    1. public
    2. Returns String
    3. No parameters
    4. Logic - convert needed attributes to a format that can be printed
  9. Write equals method
    1. public
    2. Returns boolean
    3. Parameter - instance of the class
    4. Logic - compare attributes for equity
  10. Create additional methods as needed

Subsection 10.6.1

You can watch this video or read through the content below it.
Problem: Write a class that will represent an instance of time, like a specific time in the day (i.e., 5:03:26) where all instances are printed in the same format which is either a 24 hour clock format or uses am and pm.
Now write the appropriate accessors (getters) and mutators (setters).

Subsection 10.6.2 SG7: Create 1 accessor and 1 mutator behaviors per attribute

There are 3 instance attributes, so we will need 3 getters and 3 setters
7A. Accessors
  1. Name is get_<attr_name>
  2. Public
  3. Return type same data type as attribute
  4. No parameters
  5. Logic - return value
The purpose of accessors is to return a copy of the value of the attribute. So each accessor will be public and have the name get_ followed by the name of the attribute. Accessors generally do not have any parameters and the logic consists of a single statement which is to return the value stored in the attribute.
public int getHr() {
       return hour;
}

public int getMin() {
   return minute;
}

public int getSec() {
   return second;
}
7B. Mutators
  1. Name is set_<attr_name>
  2. Public
  3. Return type is void
  4. Parameter is same data type as attribute
  5. Logic validates input parameter and sets attribute value
The purpose of mutators is to allow code from outside the class to update the value stored in an attribute. Because attributes are private, no code outside the class can directly change the value, so we add mutators. Each mutator will be public and have the name set_ followed by the name of the attribute. Mutators generally have one parameter which represents the new value to be stored in the attribute, if the value is valid. The logic of the mutator consists of verifying that the parameter value is valid before assigning it to the attribute. Mutators generally do not return anything, making them void methods.
public void setHr(int hr) {
   if (hr >=0 && hr <= 23)
      hour = hr;
}

public void setMin(int min) {
   if (min >= 0 && min <= 59)
      minute = min;
}

public void setSec(int sec) {
   if (sec >= 0 && sec <= 59)
      second = sec;
}

Subsection 10.6.3

Here is the updated UML diagram.
After writing getters and setters, this is a good time to review the structure of the class and refactor the overloaded constructor(s) to eliminate duplicate validation logic.
Figure 10.6.1.
public TimeType (int hr, int min, int sec) {
   setHr(hr);
   setMin(min);
   setSec(sec);
}
After working on the class definition, it is a good practice to test with a main driver program.
public static void main (String [] args) {
   TimeType now = new TimeType();
   now.setHr(14);
   now.setMin(30);
   now.setSec(45);
}

Practice Pages.

You have attempted of activities on this page.