Using Members in Classes
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
new
operator).
operator with the object identifier as the right-side operand).
operator with the class identifier as
the right-side operand).
operator requires an object or class
identifier as the left-side operandthis
(which is a reference to
the owning object)/** * 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?
/** * 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?
/** * 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?
/** * 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?
String
Representation/** * 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?
public class Interval { private double left, right; public Interval(double left, double right) { this.left = left; this.right = right; } // What's happens here, why, and when? public static double getLength() { return this.right - this.left; } }
main()
method of the main class
public class Game { private int lives; // What's happens here and why? (And when?) public static void main(String[] args) { lives = Integer.parseInt(args[0]); } }
self
(though needn't be) and is the first
parameter passed to a method