JMU
An Introduction to Developing Classes
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Simplified Syntax of a Class

access class class-name
{

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


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

Simplified Syntax (cont.)
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: 0)
public class Book
{
    // Attributes
    public String    author, isbn, title, publisher;
    public double    price;
    // A Method
    public double academicPrice()
    {
        return price * 0.90;        
    }
}
        
Typical Programming/Software Engineering Sequences
Creating an Initial Encapsulation
Using Variables in Methods
Using Variables in Methods (cont.)
Using Variables in Methods (cont.)
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: LocalVariable)
    public double volumePrice(double volume) 
    {
        double   discount;
     
        discount = 0.0;
        if      (volume > 1000.0)     discount = 0.40;
        else if (volume >  100.0)     discount = 0.25;

        return (1.0 - discount) * price;
    }
        
Using Variables in Methods (cont.)
An Example of Disambiguation
javaexamples/oopbasics/classes/Book.java (Fragment: Disambiguation)
    public void increasePriceTo(double price)
    {
        if (price > this.price) this.price = price;
    }
        
this Revisited
Constructors - A Special Kind of Method
Constructors (cont.)
An Example
javaexamples/oopbasics/classes/Book.java (Fragment: Constructor)
      // Constructor
      //
      public Book()
      {
          price = 0.0;
          publisher = "JMU Press";
      }
        
The Process for Creating an Initial Encapsulation
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
An Example We'll Come Back To
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
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.)
Attributes
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Attributes)
/**
 * 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;
        
An Example We'll Come Back To
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.)
Methods
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Methods)
    /**
     * 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);
    }
        
An Example We'll Come Back To (cont.)

Adding a Constructor

javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: Constructor)
    /**
     * 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;
    }
        
Facilitating Debugging
An Example We'll Come Back To (cont.)
Adding a toString() Method
javaexamples/oopbasics/pictureframe/start/PictureFrame.java (Fragment: toString)
    /**
     * 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;
    }
        
The Process Thus Far
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
  4. Add a toString() method
Facilitating Comparisons
An Example We'll Come Back To (cont.)
Adding an equals() Method
javaexamples/oopbasics/pictureframe/start/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);
    }
        
The Process Thus Far
  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.)
Using the PictureFrame Class
javaexamples/oopbasics/pictureframe/start/PictureFrameDriver.java
/**
 * 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());
    }
}