- Forward


An Introduction to Developing Classes
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Class:
    • A definition of a set that is written in terms of the characteristics of the elements (rather than by listing the elements)
  • Characteristics (a.k.a., Members):
    • Both attributes and behaviors
  • Encapsulation:
    • (v) The process of defining an entity in terms of its characteristics
    • (n) A result of the encapsulation process
Simplified Syntax of a Class
Back SMYC Forward

access class class-name
{

[
access [static] type attribute [, attribute]... ;
]...


[
access [static] type method([type param [, type param]...])
{
}
]...
}

Simplified Syntax (cont.)
Back SMYC Forward
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: 0)
 
Typical Programming/Software Engineering Sequences
Back SMYC Forward
  • Getting Started:
    • Students are only expected to go from a textual description of a class to a final encapsulation (using a refinement process)
  • By the End:
    • Students are expected to understand and use several software processes to go from needs and desires to deployment
Creating an Initial Encapsulation
Back SMYC Forward
  • Attributes/Fields:
    • Identify the noun phrases in the description
    • Think about the data types to use for each noun phrase
  • Behaviors/Methods:
    • Identify the verb phrases in the description
    • Think about the parameters for each verb phrase
Using Variables in Methods
Back SMYC Forward
  • An Observation:
    • A method will probably need to use some or all of the attributes but may also need to use other values that are not part of the definition of the class
  • An Obvious Solution:
    • Use variables (in addition to the attributes)
Using Variables in Methods (cont.)
Back SMYC Forward
  • Local Variables:
    • Have subprogram scope
    • Can only be used in the method that declares them
  • Attributes:
    • Can be used in all methods in the class
    • this can be used to disambiguate between local variables/formal parameters and attributes with the same name
Using Variables in Methods (cont.)
Back SMYC Forward
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: LocalVariable)
 
Using Variables in Methods (cont.)
Back SMYC Forward
An Example of Disambiguation
javaexamples/oopbasics/classes/Book.java (Fragment: Disambiguation)
 
this Revisited
Back SMYC Forward
  • How We Described it Earlier:
    • It's used to disambiguate attributes and local variables/parameters that have the same name
  • An Observation:
    • It is used as the left-side operand of the . operator (i.e., the membership operator) so must refer to an object
  • Internally:
    • A reference of the calling object
Constructors - A Special Kind of Method
Back SMYC Forward
  • Purpose:
    • Constructors perform initializations required when an object is created
  • Syntax:
    • A constructor has no return type and the same name as the class
  • Use:
    • A constructor is called indirectly using the new operator
Constructors (cont.)
Back SMYC Forward
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: Constructor)
 
The Process for Creating an Initial Encapsulation
Back SMYC Forward
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
An Example We'll Come Back To
Back SMYC Forward
The Textual Description

A picture frame is used to display a picture. It always has a width and height (measured in inches). It may or may not have a matte/border (also measured in inches). If it has a matte, it is the same size on all four sides. One can calculate the visible area of a picture frame from its area and the area of the matte.

A picture frame may or may not have a stand.

One can calculate the cost of a picture frame from the amount of material used in the perimeter of the frame itself and the area of the glass.

An Example We'll Come Back To
Back SMYC Forward
Identifying Attributes

A picture frame is used to display a picture. It always has a width and height (measured in inches). It may or may not have a matte/border (also measured in inches). If it has a matte, it is the same size on all four sides. One can calculate the visible area of a picture frame from its area and the area of the matte.

A picture frame may or may not have a stand.

One can calculate the cost of a picture frame from the amount of material used in the perimeter of the frame itself and the area of the glass.

An Example We'll Come Back To (cont.)
Back SMYC Forward
Attributes
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Attributes)
 
An Example We'll Come Back To
Back SMYC Forward
Identifying Methods

A picture frame is used to display a picture. It always has a width and height (measured in inches). It may or may not have a matte/border (also measured in inches). If it has a matte, it is the same size on all four sides. One can calculate the visible area of a picture frame from its area and the area of the matte.

A picture frame may or may not have a stand.

One can calculate the cost of a picture frame from the amount of material used in the perimeter of the frame itself and the area of the glass.

An Example We'll Come Back To (cont.)
Back SMYC Forward
Methods
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Methods)
 
An Example We'll Come Back To (cont.)
Back SMYC Forward

Adding a Constructor

javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Constructor)
 
Facilitating Debugging
Back SMYC Forward
  • An Observation:
    • When debugging, we often want to see all of an object's attributes
  • The Implication:
    • Always include a method (commonly named toString() that returns a human-readable String representation of all of the (important) attributes
An Example We'll Come Back To (cont.)
Back SMYC Forward
Adding a toString() Method
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: toString)
 
The Process Thus Far
Back SMYC Forward
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
  4. Add a toString() method
Facilitating Comparisons
Back SMYC Forward
  • An Observation:
    • We often want to compare the attributes of two objects (of the same class)
  • The Implication:
    • Always include an equals() method that returns true if the attributes are the same
    • Include a compareTo() method of objects can be ordered
An Example We'll Come Back To (cont.)
Back SMYC Forward
Adding an equals() Method
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: equals)
 
The Process Thus Far
Back SMYC Forward
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
  4. Add a toString() method
  5. Add an equals() method
An Example We'll Come Back To (cont.)
Back SMYC Forward
Using the PictureFrame Class
javaexamples/oopbasics/pictureframe/start/PictureFrameDriver.java
 
There's Always More to Learn
Back -