Sample: MortgageAmortizor

Objectives

This sample illustrates how to build an application using visual composition.
 

Description

This sample illustrates using features of visual composition for the following tasks:

Running the MortgageAmortizor sample

This sample amortizes a loan, showing payment amount, effective annual interest rate, and total interest cost. The Amortization bean was created by subclassing java.applet.Applet.  It uses a BorderLayout as its layout manager. Its Center component contains a Panel. This nested Panel uses a GridBagLayout to manage its components.

To run the sample:

  1. Select the Amortization class from the Projects page.
  2. Select the Run button on the tool bar.
DISCLAIMER: This sample is provided only for demonstration purposes. The calculations are not intended for actual use as there is no warranty as to their accuracy.
 

To build the MortgageAmortizor sample

The classes that are part of the MortgageAmortizor sample should be built in the following order:
  1. CompoundInterest
  2. Amortization

Building the CompoundInterest class

  1. Create the CompoundInterest class as follows:
  2. Create properties:
  3. Add a field:
  4. Add a method:
  5. Add some code:
    1. You can add code to some of the set and get methods to prevent unnecessary recalculation.
 
Select this method
    Add this code...
setPrincipalAmount /* If an input value has changed, remember to recalculate */ 
if (fieldPrincipalAmount != oldValue) recalculationNeeded = true;
setAmortizationPeriod /* If an input value has changed, remember to recalculate */ 
if (fieldAmortizationPeriod != oldValue) recalculationNeeded = true;
setInterestRate /* If an input value has changed, remember to recalculate */ 
if (fieldInterestRate != oldValue) recalculationNeeded = true; 
 
/* Scale double to percentage, if necessary.  */ 
/* This allows the interest rate to be specified as .10 to represent 10%,  */ 
/* or as 10 to represent 10%.*/ 
if (fieldInterestRate > 1.0d) fieldInterestRate = fieldInterestRate / 100d;
setPaymentsPerYear /* If an input value has changed, remember to recalculate */ 
if (fieldPaymentsPerYear != oldValue) recalculationNeeded = true;
setTimesPerYear /* If an input value has changed, remember to recalculate */ 
if (fieldTimesPerYear != oldValue) recalculationNeeded = true;
   
Select this method Add this code...
getPaymentAmount /* If any input value has changed since last recalc, then recalc */ 
if (recalculationNeeded) recalculate();
getEffectiveAnnualRate /* If any input value has changed since last recalc, then recalc */ 
if (recalculationNeeded) recalculate();
getTotalInterestCost /* If any input value has changed since last recalc, then recalc */ 
if (recalculationNeeded) recalculate();
getInterestRatePerPayment /* If any input value has changed since last recalc, then recalc */ 
if (recalculationNeeded) recalculate();
 

Close the class browser for CompoundInterest, and you're done!
 

Building the Amortization class

  1. Create the Amortization class as follows:
  2. The Visual Composition page of the class browser will be displayed, where you can build the user interface as follows:
  3. Add some nonvisual beans:
  4. Save the bean and generate code for it:
  5. Add some methods:
  6. Make connections to set properties and invoke methods as follows (see Connection Syntax).
You've now finished building the applet, and you're ready to run!