//Create an abstract class, which has not
objects, but specifies the
//attributes and methods for the members of
the class.
public abstract class Assets {
//Define the private variables to “hide” the
real values.
private String sAccountNumber;
private String sAccountName;
private double dOriginalValue;
private double dDepreciatedValue;
public Assets( String sNumber, String sName,
double dValue )
//Define the public variables to share
values.
{
sAccountNumber = sNumber;
sAccountName = sName;
dOriginalValue = dValue;
dDepreciatedValue = dValue;
}
//Define the methods that all assets will
inherit.
public abstract double
computeDepreciation();
public String getAccountNumber() { return
sAccountNumber; }
public String getAccountName() { return
sAccountName; }
public double getOriginalValue() { return
dOriginalValue; }
public double getDepreciatedValue() { return
dDepreciatedValue; }
public void setDepreciatedValue() { dDepreciatedValue
= computeDepreciation();
}
}
//Define
how depreciation is to be computed for computers
public
class Computer extends Assets {
private double dNewValue;
public Computer(String sAccount, String
sName, double dOriginalVal )
{ super(sAccount, sName, dOriginalVal); }
//Over-ride the computeDepreciation method
in the abstract class.
public double computeDepreciation()
{
dNewValue = super.getDepreciatedValue() *
0.667;
return dNewValue;
}
}
//Define
how depreciation is to be computed for buildings
public
class Building extends Assets {
private double newDeprValue;
public Building (String sAccount, String
sName, double dBalance )
{ super(sAccount, sName, dBalance); }
public double computeDepreciation()
{
newDeprValue = 0.95 * super.getDepreciatedValue();
return newDeprValue;
}
}
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
//Define
the class to create some accounts (i.e., account objects)
public
class CreateAccounts extends Applet {
public void paint (Graphics g)
{
//Reference the assets class.
//The Assets.class file must be in the
same directory.
Assets ref;
// Create two new computers, a1 and a2
Assets a1 = new Computer ( "1",
"Computer a1", 1500.00 );
Assets a2 = new Computer ( "2",
"Computer a2", 2500.00 );
// Create a new building, b
Assets b = new Building ( "456",
"Building 1", 100000.00 );
//Refer to asset a1.
//Since a1 is a computer the depreciation
method for comuters will be used.
ref = a1;
g.drawString("Depreciated value of
computer a1 is " + a1.computeDepreciation(), 25, 25 );
//Refer to asset a2.
//Since a2 is a computer the depreciation
method for comuters will be used.
ref = a2;
g.drawString("Depreciated value of
computer a2 is " + a2.computeDepreciation(),
25, 50 );
//Refer to asset b.
//Since b is a computer the depreciation
method for comuters will be used.
ref = b;
g.drawString("Depreciated value of
building 1 is " + b.computeDepreciation(),
25, 75 );
}
}