Skip to main content

Section 9.2 WrClasses-WE1-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.2.1

Consider writing a class to represent a song on your phone or music player. You need to define the appropriate attributes (data) to store the name of the song, the artist, and length. All songs should be stored in the same format, which should be MP3.
Complete the following class skeleton.
public class SongType {
   ___A___ ___B___ title;
   ___C___ ___D___ artist;
   ___E___ ___F___ length;
   ___G___ ___H___ final String FORMAT = "MP3";
}

Exercises Exercises

1.
    Q1: Fill in Blank A.
  • public
  • private
  • String
  • int
  • double
2.
    Q2: Fill in Blank B.
  • public
  • private
  • String
  • int
  • double
3.
    Q3: Fill in Blank C.
  • public
  • private
  • String
  • int
  • double
4.
    Q4: Fill in Blank D.
  • public
  • private
  • String
  • int
  • double
5.
    Q5: Fill in Blank E.
  • public
  • private
  • String
  • int
  • double
6.
    Q6: Fill in Blank F.
  • public
  • private
  • String
  • int
  • double
7.
    Q7: Fill in Blank G.
  • public
  • private
  • static
  • void
  • const
8.
    Q8: Fill in Blank H.
  • public
  • private
  • static
  • void
  • const
You have attempted of activities on this page.