8.3.1. WrClasses-WE3-P1

Subgoals for Writing a Class 3/4

  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 class variables (static) as needed ‘

    1. Name

    2. Data Type

    3. public / private / final

  5. Define instance variables (that you want to be interrelated)

    1. Name

    2. Data Type

    3. private

  6. Create constructor (behavior) that creates initial state of object

    1. public

    2. Same name as class

    3. No return type

    4. Default - no parameters

    5. Logic - initialize all variables

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


Classes-WE3-P1

Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.

UML

Fill in the blanks for the followig code:

public class SongType {
   //attributes declared here
   //...

   //getters/accessors
   public ___A___ getTitle () {
      ___B___ ___C___;
   }
   ___D___ String ___E___ () {
      ___F___ artist;
   }
   public ___G___ getLength () {
      ___H___ ___I___;
   }

   //setters/mutators
   public ___J___ setTitle (String n) {
      title = ___K___;
   }
   ___L___ void setArtist (___M___ n) {
      artist = ___N___;
   }
   public ___O___ setLength (___P___ n) {
      if (n > 0)
         ___Q___ = n;
   }
}
You have attempted of activities on this page