Skip to main content

Section 10.1 Worked Example: Writing Classes - Defining Attributes

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 10.1.1

You can watch this video or read through the content below it.
Problem: Write a class that will represent an instance of time, like a specific time in the day (i.e., 5:03:26) where all instances are printed in the same format which is either a 24 hour clock format or uses am and pm.
For this worked example, we will only be determining the appropriate attributes (data) to be stored and declaring them.

Subsection 10.1.2 SG1: Name it

We will call the class TimeType.

Subsection 10.1.3 SG2: Differentiate class-level (static) vs. instance/object-level variables

class-level (static) data: one value shared between ALL instances
instance-level data: each instance has its own copy
In this example, we want all time instances to be in the same format so static data for format, all remaining attributes are instance level attributes.

Subsection 10.1.4 SG3: Differentiate class-level (static) vs. instance/object behaviors/methods

All methods will be instance level. There is one class level attribute which would indicate that you might need an accessor and/or mutator for that attribute. However, in this example the attribute is final (i.e., cannot be changed after the first instance of the class is created) and is public, so no class level methods are needed.

Subsection 10.1.5 SG4: Define class variables (static) as needed

public class TimeType {

   public static final boolean FORMAT24 = true;

}
Note the use of the final keyword to define FORMAT24 as a constant, which makes it safe to expose as public. An alternate implementation might choose to make this value mutable, but private, using static methods to access and alter it.

Subsection 10.1.6 SG5: Define instance variables (that you want to be interrelated)

Unified Modeling Language (UML) is often used to represent the structure of a class. A class UML diagram consists of a box broken into three segments:
  1. Top segment is the name of the class
  2. Second segment are the attributes of the class
  3. Third segment are the methods of the class
For all attributes and methods, a + indicates public, a - indicates private. For attributes, they are listed by name followed by a colon followed by their datatype. All final variables are listed in all capital letters.
Figure 10.1.1.
public class TimeType {

   //static var
   public static final boolean FORMAT24 = true;

   //instance vars
   private int hour;
   private int minute;
   private int second;

}

Practice Pages.

You have attempted of activities on this page.