Skip to main content

Section 9.9 Worked Example: Writing Classes - Overrides and Other

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 9.9.1

You can watch this video or read through the content below it.
Problem: We will be writing a class to represent an instance of time, like a specific time in the day.
Now write toString, equals, and any other methods we need.

Subsection 9.9.2 SG8: Write toString method

  1. public
  2. Returns String
  3. No parameters
  4. Logic - convert needed attributes to a format that can be printed
public String toString() {
   String holder= "";
   if (hour < 10)
      holder = "0";
   holder += hour + ":";
   if (minute < 10)
      holder += '0';
   holder += (minute + ":");
   if (second < 10)
      holder += '0';
   holder += second;
   return holder;
}

Subsection 9.9.3 SG9: Write equals method

  1. public
  2. Returns boolean
  3. Parameter - instance of the class
  4. Logic - compare attributes for equity
public boolean equals (TimeType other) {

return (hour == other.hour &&
   minute == other.minute &&
   second == other.second);

// alternate logic
    // return  toString().equals(other.toString());

}

Subsection 9.9.4 SG10: Write other methods

For additional functionality, we will implement two more instance methods.
public void increment() {
   second++;
   if (second > 59) {
      second = 0;
      minute++;
   }
   if (minute > 59) {
      minute = 0;
      hour++;
   }
   if (hour > 23) {
      hour = 0;
   }
}

public boolean lessThan (TimeType other) {
   boolean result = false;
   if (hour < other.hour)
      result = true;
   else if (hour > other.hour)
      result = false;
   else {
      if (minute < other.minute)
         result = true;
      else if (minute > other.minute)
         result = false;
      else  {
         if (second < other.second)
            result = true;
         else
            result = false;
      }
   }
   return result;
}

Subsection 9.9.5

After writing methods, this is a good time to review the structure of the class and test with a main driver program.
Figure 9.9.1.
public static void main (String [] args) {
   TimeType noon = new TimeType(12, 0, 0);
   System.out.println("noon: " + noon); //concat auto-calls toString
   TimeType dueTime = new TimeType(23, 59, 59);
   System.out.println("The due time is " + dueTime);
   for (int i = 0; i < 362; i++) {
      dueTime.increment();
      System.out.println("The due time is " + dueTime);
   }
   if (dueTime.lessThan(noon))
      System.out.println("dueTime less");
   else
      System.out.println("noon less");
}

Practice Pages.

You have attempted of activities on this page.