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.
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.
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
1.
String
int
double
void
return
2.
void
String
title
artist
length
3.
void
String
title
artist
length
4.
void
String
title
artist
length
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.
For this worked example, we should think about comparing two TimeType instances, which we will consider to be the same only if the hours, minutes, and seconds, are all identical. So the logic of the method returns true if the instance’s hour value is the same as the parameter’s hour value, etc. Another potential way of writing an equals method is to compare the toString values for each object.
public _______ equals (_______ other) {
E F
return artist._______(other._______) && title._______(other._______);
G H I J
}
5.
void
String
boolean
SongType
other
6.
void
String
boolean
SongType
other
7.
artist
title
length
other
equals
8.
artist
title
length
other
equals
9.
artist
title
length
other
equals
10.
artist
title
length
other
equals
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.
For additional functionality, we will implement two more instance methods. The first, increment, should be public, will not return any value and takes no parameters. The method should add one second to the current time. Remember that we have to consider the upper bound values for hours and minutes.
The second method to be written is lessThan, which will determine if the instance is less than (or occurs before) the parameter. This public method returns a boolean value which is true if the current instance is chronologically before the value in the parameter, and false otherwise. The logic for this method will first compare the hours of the two instances, and if the hours are equal then we compare the minutes, etc.
public _______ isLonger (_______ other) {
K L
return _______ > _______._______;
M N O
}
11.
void
String
boolean
SongType
other
12.
void
String
boolean
SongType
other
13.
artist
title
length
other
equals
14.
artist
title
length
other
equals
15.
artist
title
length
other
equals