Skip to main content

Section 9.10 WrClasses-WE4-P1

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.10.1

Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
Figure 9.10.1.
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___ + ")";

Exercises Exercises

1.
    Q36: Fill in Blank A.
  • String
  • int
  • double
  • void
  • return
2.
    Q37: Fill in Blank B.
  • void
  • String
  • title
  • artist
  • length
3.
    Q38: Fill in Blank C.
  • void
  • String
  • title
  • artist
  • length
4.
    Q39: Fill in Blank D.
  • 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.
public ___E___ equals (___F___ other) {
   return artist.___G___(other.___H___) &&
      title.___I___(other.___J___);
}
5.
    Q40: Fill in Blank E.
  • void
  • String
  • boolean
  • SongType
  • other
6.
    Q41: Fill in Blank F.
  • void
  • String
  • boolean
  • SongType
  • other
7.
    Q42: Fill in Blank G.
  • artist
  • title
  • length
  • other
  • equals
8.
    Q43: Fill in Blank H.
  • artist
  • title
  • length
  • other
  • equals
9.
    Q44: Fill in Blank I.
  • artist
  • title
  • length
  • other
  • equals
10.
    Q45: Fill in Blank J.
  • 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.
public ___K___ isLonger (___L___ other) {
   return ___M___ > ___N___.___O___;
}
11.
    Q46: Fill in Blank K.
  • void
  • String
  • boolean
  • SongType
  • other
12.
    Q47: Fill in Blank L.
  • void
  • String
  • boolean
  • SongType
  • other
13.
    Q48: Fill in Blank M.
  • artist
  • title
  • length
  • other
  • equals
14.
    Q49: Fill in Blank N.
  • artist
  • title
  • length
  • other
  • equals
15.
    Q50: Fill in Blank O.
  • artist
  • title
  • length
  • other
  • equals
You have attempted of activities on this page.