|
An Introduction to Developing Classes
with Examples in Java |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
access class class-name
{
this can be used to disambiguate between
local variables/formal parameters and attributes with the
same namethis Revisited.
operator (i.e., the membership operator) so must refer to
an objectnew operatorA 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.
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 encapsulation of a picture frame.
*
* Note: The attributes are public in this version because we
* have not yet discussed information hiding.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class PictureFrame
{
public boolean stand;
public double height, matte, width;
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.
/**
* Return the cost of this PictureFrame (which is a function
* of the perimeter and the area) in dollars.
*
* @return The cost
*/
public double calculateCost()
{
double frame, glass;
frame = (2.0*width + 2.0*height) * 0.15;
glass = (width * height) * 0.05;
return frame+glass;
}
/**
* Return the visible area (in square inches) of the content
* contained in this PictureFrame.
*
* @return The visible area
*/
public double calculateVisibleArea()
{
return (width - 2.0*matte) * (height - 2.0*matte);
}
/**
* 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;
}
toString() that returns a
human-readable String representation of all
of the (important) attributestoString() Method /**
* Return a human-readable String representation of this PictureFrame.
*
* @return The String representation
*/
public String toString()
{
String result;
result = String.format("%5.2f in. x %5.2f in.", width, height);
if (matte > 0.0)
result += String.format(" with a %5.2f in. matte", matte);
if (stand)
result += " (w/ stand)";
return result;
}
toString() methodequals() method
that returns true if the attributes are the
samecompareTo() method of objects
can be orderedequals() Method /**
* 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);
}
toString() methodequals() methodPictureFrame Class/**
* A Driver that can be used to demonstrate the PictureFrame class.
*/
public class PictureFrameDriver
{
/**
* The entry point.
*
* @param args The command line arguments
*/
public static void main(String[] args)
{
double visible;
PictureFrame dog, snake;
dog = new PictureFrame(8.0, 10.0, 0.0, true);
System.out.printf("Description: %s\n", dog.toString());
System.out.printf("Visible Area: %5.2f sq. in.\n",
dog.calculateVisibleArea());
System.out.printf("Cost: $%5.2f\n", dog.calculateCost());
System.out.printf("\n");
snake = new PictureFrame(3.0, 5.0, 1.0, true);
System.out.printf("Description: %s\n", snake.toString());
System.out.printf("Visible Area: %5.2f sq. in.\n",
snake.calculateVisibleArea());
System.out.printf("Cost: $%5.2f\n", snake.calculateCost());
}
}