JMU
Using Members in Classes
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
The Owning Object
An Example - Attributes and a Constructor
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: 0)
/**
 * An encapsulation of a picture frame.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version PF
 */
public class PictureFrame
{
    private static final double DOLLARS_PER_IN_OF_FRAME    = 0.15;
    private static final double DOLLARS_PER_SQ_IN_OF_GLASS = 0.05;    

    private boolean    stand;    
    private double     height, matte, width;

    /**
     * Construct a PictureFrame object.
     *
     * Note: Negative values are converted to positive values 
     * and the width and height are put in canonical form 
     * (i.e., a portrait orientation).
     *
     * @param width   The width (in inches)
     * @param height  The height (in inches)
     * @param matte   The size of the matte (in inches) on all 4 sides
     * @param stand   true if there is a built-in stand
     */
    public PictureFrame(double width, double height, double matte, 
                        boolean stand)
    {
        double     h, w;
        
        h = Math.abs(height);
        w = Math.abs(width);

        this.width  = Math.min(w, h);
        this.height = Math.max(w, h);
        this.matte  = Math.abs(matte);
        this.stand  = stand;
    }
}
        

Why are some attributes static and some not?
What are the formal parameters of the constructor?
What local variables are used in the constructor?
What methods are invoked in the constructor and are they static or not?

An Example (cont.) - Getters
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: Getters)
    /**
     * Get the height of this PictureFrame.
     *
     * @return   The height
     */
    public double getHeight()
    {
        return height;
    }

    /**
     * Get the size of the matte.
     *
     * @return   The size of the matte
     */
    public double getMatte()
    {
        return matte;
    }

    /**
     * Get the width of this PictureFrame.
     *
     * @return   The width
     */
    public double getWidth()
    {
        return width;
    }
        

How do you refer to the owning object?
Why isn't it necessary to do so?
Could you do so anyway?

An Example (cont.) - Private Methods
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: PrivateMethods)
    
    /**
     * Calculate the area of this PictureFrame.
     *
     * @return  The area (in square inches)
     */
    private double area()
    {
        return width * height;
    }

    /**
     * Calculate the perimeter of this PictureFrame.
     *
     * @return  The area (in inches)
     */
    private double perimeter()
    {
        return 2.0*width + 2.0*height;
    }
    
    /**
     * Return the cost of this PictureFrame (which is a function
     * of the perimeter and the area) in dollars.
     *
     * @return   The cost
     */
    public double getCost()
    {
        double     frameCost, glassCost;
        
        frameCost = this.perimeter() * DOLLARS_PER_IN_OF_FRAME;
        glassCost = this.area() * DOLLARS_PER_SQ_IN_OF_GLASS;

        return frameCost + glassCost;
    }
        

Which new methods are private?
What is true of private members?
Why include private methods?
Is it necessary to use this. when invoking private methods?
Which members are static and which are not?

An Example (cont.) - Comparing Objects
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: Equals)
    /**
     * Return true if the owning PictureFrame and the given PictureFrame
     * have the same attributes.
     *
     * @return  true if the attributes are the same; false otherwise
     */
    public boolean equals(PictureFrame other)
    {
        return (this.width == other.width) && (this.height == other.height)
            && (this.matte == other.matte) && (this.stand == other.stand);
    }
        

How many objects are being compared?
How are they referred to?
Is it necessary to use this.?
Why are the attributes of other accessible?

An Example (cont.) - Getting a String Representation
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: ToString)
    /**
     * Return a human-readable String representation of this PictureFrame.
     *
     * @return         The String representation
     */
    public String toString()
    {
        String      result;
    
        result = "" + width + "x" + height;
        return result;
    }
}
        

When might this method be useful?
What operators are being used?

Using a Non-Static Member in a Static Context
Using a Non-Static Member in a Static Context(cont.)
Using a Static Member with an Object on the Left
Java vs. Python - Important Differences