Section 10.9 Worked Example: Writing Classes - Other Methods
Subgoals for Writing a Class.
- Name it
- Differentiate class-level
static
vs. instance/object-level variables - Differentiate class-level
static
vs. instance/object behaviors/methods -
Define instance variables (that you want to be interrelated)
- Name
- Data Type
- private
-
Define class variables
static
as needed- Name
- Data Type
- public / private / final
-
Create constructor (behavior) that creates initial state of object
- Overloaded constructor (with as many parameters)
- public
- Same name as class
- No return type
- Default - no parameters
- Logic - initialize all variables
- Repeat as needed, adding parameters
-
Create 1 accessor and 1 mutator behaviors per attribute
-
Accessors
- Name is get_<attr_name>
- Public
- Return type same data type as attribute
- No parameters
- Logic - return value
-
Mutators
- Name is set_<attr_name>
- Public
- Return type is void
- Parameter is same data type as attribute
- Logic validates input parameter and sets attribute value
-
-
Write toString method
- public
- Returns String
- No parameters
- Logic - convert needed attributes to a format that can be printed
-
Write equals method
- public
- Returns boolean
- Parameter - instance of the class
- Logic - compare attributes for equity
- Create additional methods as needed
Subsection 10.9.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.
Whenever you define a new class, other than defining attributes, and writing constructors, accessors and mutators, there are two additional methods that should always be included. They are the toString and equals methods. We will now write those.
Subsection 10.9.2 SG8: Write toString method
- public
- Returns String
- No parameters
- 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 10.9.3 SG9: Write equals method
- public
- Returns boolean
- Parameter - instance of the class
- 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 10.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 10.9.5
After writing methods, this is a good time to review the structure of the class and test with a main driver program.
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.