Developing Classes
With Examples in Java
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
What's the Starting Point? (cont.)
An Example of a 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.
Creating the Initial Encapsulation (cont.)
An Example
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.
Creating the Initial Encapsulation (cont.)
An Example
public class PictureFrame
{
private double width;
private double height;
private double matte;
private boolean stand;
private boolean hasMatte;
public double calculateVisibleArea(){return 0.0;}
public double calculateCost(){return 0.0;}
}
Refining the Encapsulation: Attributes (cont.)
Overuse of Instance Attributes
javaexamples/oopbasics/Screen.java
Refining the Encapsulation: Attributes (cont.)
Eliminating a Redundancy
public class PictureFrame
{
private double width;
private double height;
private double matte; // 0.0 when there is no matte
private boolean stand;
public double calculateCost(){return 0.0;}
public double calculateVisibleArea(){return 0.0;}
}
Refining the Encapsulation
Adding equals()
and toString()
Methods
public class PictureFrame
{
private double width;
private double height;
private double matte; // 0.0 when there is no matte
private boolean stand;
public double calculateCost(){return 0.0;}
public double calculateVisibleArea(){return 0.0;}
public boolean equals(PictureFrame other) {return false);
public String toString(){return "";}
}
Identifying Constructors (cont.)
An Example
javaexamples/oopbasics/pictureframe/start/PictureFrame.java
Overloaded Constructors (cont.)
Adding Copy, Default, and Explicit Value Constructors
javaexamples/oopbasics/pictureframe/overloadedconstructors/PictureFrame.java
(Fragment: Constructors)
Overloaded Constructors (cont.)
A Common Mistake
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;
}
public PictureFrame(double width, double height)
{
PictureFrame frame;
frame = new PictureFrame(width, height, 0.0, false);
}
More on Copy Constructors (cont.)
In this example, since both of the attributes are primitive types,
the distinction between shallow and deep copies does not arise.
javaexamples/basics/Pair.java
(Fragment: CopyConstructor)
If, for example, the Pair
class included a String
attribute named id
, then a shallow copy would include the
statement:
this.id = other.id; // this.id is an alias for other.id
whereas a deep copy would include the statement:
this.id = new String(other.id); // Use the copy constructor in String
Private Methods (cont.)
javaexamples/oopbasics/pictureframe/privatemethods/PictureFrame.java
Overloaded Methods: An Example
javaexamples/oopbasics/pictureframe/overloadedmethods/PictureFrame.java
(Fragment: overloadedmethods)
Overloaded Methods: Default Handling (cont.)
An Example
public class TaxStand
{
.
.
.
/**
* Calculates the tax payment based on income and
* filing status
*/
public double payment(int income, int status)
{
...
}
/**
* Calculates the tax payment assuming the
* filing status is 0
*/
public double payment(int income)
{
return payment(income, 0);
}
}
Overloaded Methods: Similar Types (cont.)
An Example
public class Weight
{
.
.
.
/**
* Change this Weight by a given amount
*
* @param amount The amount of the change
*/
public void changeBy(Weight amount)
{
...
}
/**
* Change this Weight by a given amount
*
* @param pounds Number of pounds in the change
* @param ounces Number of ounces in the change
* @param positive true to increase, false to decrease
*/
public void changeBy(int pounds, int ounces, boolean positive)
{
...
}
}
Overloaded Methods: Similar Types (cont.)
An Example
public class TaxStand
{
.
.
.
/**
* Calculates the tax payment based on income and
* filing status
*/
public double payment(int income, int status)
{
...
}
/**
* Calculates the tax payment based on income and
* filing status
*/
public double payment(double income, int status)
{
...
}
}
Overloaded Methods: Cardinality (cont.)
An Example
public int minimum(int a, int b)
{
int min;
min = a;
if (b < a) min = b;
return b;
}
public int minimum(int[] values)
{
int min;
min = values[0];
for (i=1; i < values.length; i++)
{
if (values[i] < min) min = values[i];
}
return min;
}
Class Attributes: Constants etc... (cont.)
javaexamples/oopbasics/pictureframe/staticattributes/PictureFrame.java
(Fragment: staticattributes)
Class Attributes: Common Values
An Example
public class ProgressiveSlotMachine
{
// Attributes of the class
private static double jackpot;
// Attributes of objects
private double currentBet;
.
.
.
private void handleLosingSpin()
{
jackpot = jackpot + 0.5 * currentBet;
}
}
Class Attributes: Object Counters
An Example
/**
* A user account
*/
public class UserAccount
{
// Attributes of the class
private static int nextAccountNumber;
// Attributes of objects
protected final int accountNumber;
protected String name;
/**
* Explicit Value Constructor
*
* @param name The name of the account holder
*/
public UserAccount(String name)
{
// Store the name
this.name = name;
// Store the account number
accountNumber = nextAccountNumber;
// Setup the account number for the next account
nextAccountNumber++;
}
Class Methods: "Factory" Methods (cont.)
A parsePictureFrame()
Method in the PictureFrame
Class
javaexamples/oopbasics/pictureframe/factories/PictureFrame.java
(Fragment: parse)
There's Always More to Learn