8.5.1. WrClasses-WE4-P1¶
Subgoals for Writing a Class 4/4
Name it
Differentiate class-level (static) vs. instance/object-level variables
Differentiate class-level (static) vs. instance/object behaviors/methods
Define class variables (static) as needed ‘
Name
Data Type
public / private / final
Define instance variables (that you want to be interrelated)
Name
Data Type
private
Create constructor (behavior) that creates initial state of object
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
Classes-WE4-P1
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.
public ___A___ toString () {
return ___B___ + " by " + ___C___ + "(" + ___D___ + ")";
- String
- int
- double
- void
- return
Q36: Fill in Blank A.
- void
- String
- title
- artist
- length
Q37: Fill in Blank B.
- void
- String
- title
- artist
- length
Q38: Fill in Blank C.
- void
- String
- title
- artist
- length
Q39: Fill in Blank D.
For questions 40 - 45, fill in the code blanks to implement the equals method, where two songs are equal if
public ___E___ equals (___F___ other) {
return artist.___G___(other.___H___) &&
title.___I___(other.___J___);
}
- void
- String
- boolean
- SongType
- other
Q40: Fill in Blank E.
- void
- String
- boolean
- SongType
- other
Q41: Fill in Blank F.
- artist
- title
- length
- other
- equals
Q42: Fill in Blank G.
- artist
- title
- length
- other
- equals
Q43: Fill in Blank H.
- artist
- title
- length
- other
- equals
Q44: Fill in Blank I.
- artist
- title
- length
- other
- equals
Q45: Fill in Blank J.
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 ___K___ isLonger (___L___ other) {
return ___M___ > ___N___.___O___;
}
- void
- String
- boolean
- SongType
- other
Q46: Fill in Blank K.
- void
- String
- boolean
- SongType
- other
Q47: Fill in Blank L.
- artist
- title
- length
- other
- equals
Q48: Fill in Blank M.
- artist
- title
- length
- other
- equals
Q49: Fill in Blank N.
- artist
- title
- length
- other
- equals
Q50: Fill in Blank O.