Skip to main content

Section 8.11 WrClasses-WE4-P2

Subsection 8.11.1 Subgoals for Writing a Class 4/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

  1. Write toString method

    1. public

    2. Returns String

    3. No parameters

    4. Logic - convert needed attributes to a format that can be printed

  2. Write equals method

    1. public

    2. Returns boolean

    3. Parameter - instance of the class

    4. Logic - compare attributes for equity

  3. Create additional methods as needed

Subsection 8.11.2 Classes-WE4-P2

Exercises Exercises

1.
2.
3.
4.
5.
You have attempted of activities on this page.