Section 10.10 WrClasses-WE4-P1
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)
-
Define class variables
static
as needed -
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.10.1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
For questions 36 - 39, fill in the code blanks to implement the toString method to print the song in the following format:
SONG by ARTIST (X.XX)
where X.XX is the length in minutes.
A toString method is automatically invoked whenever an object reference is concatenated to a String or otherwise needs to be converted to a String. The toString method is public and returns a String that contains the values of the attributes (or some subset). The method takes no parameters.
In this worked example, we need to put all the values of the attributes into a String format. Remember that we are having all instances print in a 24 hour clock format. Any attribute value that has a value < 10 will only print a single digit, and that might look funny (i.e., "0:0:0" for midnight instead of "00:00:00"). So we will add an additional "0" for any value that is < 10.
public _______ toString () {
A
return _______ + " by " + _______ + "(" + _______ + ")";
B C D
}
To abbreviate this example, we have not explicitly checked the value of the class level attribute, FORMAT24. To be complete, the toString method should check this attribute and have this logic if the attribute is true, but have different logic if the attribute is false and print a different string with "am" and "pm" as appropriate.
Exercises Exercises
2.
3.
4.
For questions 40 - 45, fill in the code blanks to implement the equals method, where two songs are equal if they have the same name and artist, regardless of length.
The equals method is used to determine if two object references are equal, or have the same values for their attributes. The equals method should always be public and return a boolean which is true if the objects are considered equal and false otherwise. There is a single parameter, which will represent the second object reference which you will be comparing the instance to. This can be defined as another instance of the class. The logic of the equals method is to determine if the instance and the parameter have equal attribute values.
public _______ equals (_______ other) {
E F
return artist._______(other._______) && title._______(other._______);
G H I J
}
5.
6.
7.
8.
9.
10.
For questions 46 - 50, fill in the code blanks to implement a method called isLonger, which returns true if the song is longer than the song in the parameter.
public _______ isLonger (_______ other) {
K L
return _______ > _______._______;
M N O
}
11.
12.
13.
14.
15.
You have attempted 1 of 3 activities on this page.