Skip to main content

Section 10.3 Worked Example: Writing Classes - Constructors

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.3.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.
The attributes have been declared, now write a default and overloaded constructor.

Subsection 10.3.2 SG6: Create constructor (behavior) that creates initial state of object

Subgoals for writing constructors are:
  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
We will start with the default constructor, which has no parameters. The default constructor has a header/signature of:
public TimeType() {

}
The logic of any constructor is to initialize all the attributes:
public TimeType () {
   hour = 0;
   minute = 0;
   second = 0;
}
Now we need to create additional constructors by adding parameters as needed. We will create one additional constructor that takes in 3 parameters, one for each instance attribute. Note that the parameters are checked for validity before assigning them to the attributes.
public TimeType (int hr, int min, int sec) {

   if (hr >=0 && hr <= 23)
      hour = hr;
   if (min >= 0 && min <= 59)
      minute = min;
   if (sec >= 0 && sec <= 59)
      second = sec;
}

Subsection 10.3.3

We are now able to construct TimeType objects from the main method in two ways
public static void main (String [] args) {
   TimeType midnight = new TimeType(); //call the default constructor
   TimeType noon = new TimeType(12, 0, 0); //call an overloaded constructor
}
The midnight object reference will refer to an instance of TimeType where the hour, minute, and second attributes all have the value 0 because that is the default value given to all integer attributes upon instantiation.
Here are the constructors added to the UML diagram within the method segment. Constructors are public, and the same name as the class, and have no return type (not even void) and we generally only list the data types of the parameters and not the parameter names.
Figure 10.3.1.

Practice Pages.

You have attempted of activities on this page.