Developing Java Applications
Peter Komisar © Conestoga College  version 1.0  Fall / 2007

Reference:  Third Draft Document for Review, Sept. 17,2007,
SG24-7501-00 'Rational Application Developer V7 Programming
Guide'.

This is a synopsis of information found in the 8th chapter,
''Developing Java Applications' found in the third draft
Document for Review, dated September 17, SG24-7501-00
Rational Application Developer V7 Programming Guide. It
has been prepared for private academic purposes and is not
an effort to publish the contained material.

Any anonymous quotes are from the above noted reference.        


The draft in Chapter 7 proceeds to demonstrate
the use of the Java Perspective through the banking
application described earlier. The sample can be
created by following the procedure used below which
we should follow. If need be the sample can also be
imported as noted in the Chapter contents.

It first reviews in more detail the different views
in each perspective with added detail.

The chapter reviews the common views used in the
Java perspective

Common Views of the Java Perspective
The layout diagrammed in the pdf is a custom layout.
The authors recommend saving a custom layout that
suits your needs as is quoted below from the RAD 7
Programmer's Guide.
  
"We recommend that you also customize the
perspectives so that they fit your requirements."


Saving a Customized Perspective


Window    --»  Save Perspective As.

Reseting to a Predefined or Saved Perspective:

Window  --» Reset Perspective.

Java Perspective

Java perspective are typically used to develop standalone
Java applications or utility Java projects which are packaged
as  utility JARs so they can be shared.

Adding Views

Select Window → Show View.

Summary Review of Views

// "For some problems Application Developer provides a quick fix.
// How to process a quick fix is described in “Quick fix” on page 310.

// "Java builder is responsible for all Java resource files. However,
// in an
enterprise application project other builders may be used."



Java Browsing perspective


Used to browse and manipulate your code, the Browsing
Perspective uses 'distinct' views to present the same info
that is supplied in the Package Explorer.


Java Type Hierarchy perspective


The whole perspective is dedicated to the Hierarchy view

The Bank Application

The Rational draft describes the 'ITSO Bank Application'.
It has the following classes and interfaces.

Interfaces and Classes Overview

// UML: Customer * Account
"A transaction - is a single operation that will be
performed on an account. In the example only
two transaction types exist: Debit and credit."

ITSOBankException

ITSOBankException is the supertype of all exceptions
of the application.
Exception classes are in the package
itso.rad7.bank.exception.


Packages & Description


UML Class diagram // see UML overview in text

A UML class diagram is provided overview application
architecture diagramming the interfaces and classes and
their relationships.


Creating a new Java Project


To create a new Java project:

Select File New Project

or

Click Toolbar use the New Java Project wizard
Select Java Project or Java  Java Project click Next

Enter info at the dialog:
// see pdf for changing JREs or Directories

Click Finish  // before you do note the dialog does the following


Build Path Dialog
// wasn't clear in text where this is located



Creating and Editing Classpath Variables


Preference pageSelect Window Preferences
Java Build Path Classpath Variables.

// The source folder itself is always exported

Creating a UML class diagram

Application Developer supports the creation of UML
class diagrams. Class diagrams are static visual
representations of packages, interfaces, classes, and
their relationships.

RAD  automatically calls the related wizard to create
parallel Java code in the process of adding elements
to the diagram. Additionally existing elements may be
added to the class diagram.

Adding an Existing Element to a Class Diagram

Cardinality/Optionality
reference:The Unified Modeling Language (UML) http://www.essentialstrategies.com/publications/modeling/uml.htm


Specifying the numerical relationship between entities
in UML takes the form:

<lower limit> . . <upper limit>

 where

<lower limit> usually denotes 0 or 1 // optionality
<upper limit> number denoting the upper limit // cardinality.

The <upper limit> may be:

  • * , an asterisk, // more than one
  • an explicit number
  • a set of numbers, or a range.

 Examples

 ' 0..* '  - zero, one, or more  // abbreviates to:  *
 ' 1..1 ' - exactly one // abbreviates to:  1



Steps for Creating a UML class diagram

The file ITSOBank-ClassDiagram.dnx should appear in the
diagram folder and open in the Visualizer Class Diagram
editor. The 'Java Drawer' is open in the Palette. RAD
opens the Java Drawer by default for a Java project.

Creating Java packages

Here is where a duality of approaches begins.
One can build from a UML diagram, what Rational
brings to the Eclipse platform, or by using standard
wizards, the classic Eclipse way. We will see both
approaches are good.

Inside the Java project Java packages need first be
created. The Visualizer Class Diagram editor or the
New Java Package wizard both can do this.

The Visualizer Class Diagram editor actually calls the
New Java Package wizard as well, so the mechanisms
are the same.

The following Java packages are created.


ITSO Java Packages


Creating Java Packages in the Visualizer Class Diagram Editor

// repeat for three of the needed packages

Creating Java packages Using the New Java Package Wizard


// you can also right-click the package in the Package Explorer
// and select Visualize → Add to Current Diagram.


 Creating the ITSO Java Interfaces

Creating Java Interfaces with the Visualizer Class Diagram Editor
//  a line appears between the package and the Bank interface.

Creating Java Interfaces Using the New Java Package Wizard
// "To add an interface to the class diagram, you can just
// drag&drop the interface to the class diagram editor, or
// select the interface in the Package Explorer and
// Visualize → Add to Current Diagram.



ITSO Java Classes

itso.rad7.bank.impl  // package
    |                        
    |__ITSOBank      // classes      
 


itso.rad7.bank.model
  // package

    |
    |__Account
        // classes    
    |__Customer
    |__Transaction   // abstract
           |__Credit
           |__Debit



itso.rad7.bank.client
   
|                        
    |__BankClient
    

itso.rad7.bank.exception
    |                        
    |__ITSOBankException
                      |__AccountAlreadyExistException
                      |__InvalidAccountException
                      |__InvalidAmountException
                      |__InvalidCustomerException
                      |__InvalidTransactionException


Creating Java Interfaces with the Visualizer Class Diagram Editor
// UML Note: a line appears between the package and the Bank interface.

Creating Java Interfaces Using the New Java Package Wizard

To Specify Superclass

Example

Superclass: java.lang.Object (default).

To change the superclass:


To Specify Interfaces

Example

interface:java.io.Serializable

To add an interface:

Dialog Provided Method Stubs

//  In the example selections are useful for the classes

// ITSOBank, Credit, and Debit.

// UML Note: A line is drawn from the package to the new class.


Creating Java Classes Using the New Java Package Wizard


// Alternative,  drag&drop the class to the class diagram
// select Visualize → Add to Current Diagram.

The Rational pdf continues with just the following classes.

Creating Java Attributes (fields) and Getter / Setter Methods

See the pdf for a complete summary of the class
attributes in table form. The Transactions interface,
the ITSOBank class and the Account class are
roughed in below to provide a different view.



import java.util.*;
import java.math.BigDecimal;

interface Transactions{
                     String CREDIT="CREDIT";
                     String DEBIT="DEBIT";
  // Constants in interfaces are public final static
 // methods ?                
                    }

class ITSOBank {

private HashMap<String, Account> accounts;
private HashMap<String, Customer> customers;
private HashMap <String,ArrayList<Account>> customerAccounts;
// all above have private setter, public getter method
private static ITSOBank bank;
// static public getter method

   ITSOBank( ){

   accounts = new HashMap<String, Account>( );
   customers=new HashMap<String, Customer>( );
   customerAccounts = new HashMap
                         <String, ArrayList<Account>>( );
   bank = new ITSOBank( );
                   }
   }

class Account{
private String accountNumber=null;
private BigDecimal balance=null;
private ArrayList<Transactions> transactions =null;
// all above have private setter, public getter method

     Account(){
     transactions =new ArrayList<Transactions>();
     }
}

// etc to include the TransAction Customer, and Exception classes
// stub classes to allow compilation
       
class TransAction{}
class Customer{}
 
Creating Attributes in the Visualizer Class Diagram Editor

There are two ways to add a field to an interface or a
class.

1 ) Write the field declaration directly into the interface
or class body from inside a Java editor

2) Add the field in the Visualizer Class Diagram editor.
// the 'Visualizer' calls the 'Create Java Field wizard'

The first way is self-explanatory and is the classic
approach.

To create a field in the Visualizer Class Diagram editor:
// change standard Java types by clicking inverted chevron symbol
// if the attribute is not visible it may be in a collapsed
// or filtered view


Creating Get/Set Methods using the Refactor Feature

// "It is good programming style when you use the getter and
//  setter method also internally in the class to access member
// variables."


Create Get/Set Methods using the Source Feature


Simple Standard Eclipse Alternative Method


Following are methods that need to be implemented.
The pdf has them in an easy to read table form. Here
they are composed as Java methods.

// this is like putting the horse before the carriage
// in providing what we are attempting to build, but it
// is fine for the sake of the study


Bank Interface Java Methods that Need to be Generated

  void addCustomer(Customer customer)
  throws CustomerAlreadyExistException

  void updateCustomer(String ssn, String title,
  String firstName, String lastName)
  throws InvalidCustomerException

  void removeCustomer(Customer customer)
  throws InvalidCustomerException

  void openAccountForCustomer(Customer customer,
  Account account) throws InvalidCustomerException,
  AccountAlreadyExistException

  void closeAccountOfCustomer(Customer customer,
  Account account) throws InvalidAccountException,
  InvalidCustomerException

  Customer searchCustomerBySsn(String ssn)
  throws InvalidCustomerException

  Account searchAccountByAccountNumber
  (String accountNumber) throws InvalidAccountException

  Map<String,Customer> getCustomers( )
 
  ArrayList<Account> getAccountsForCustomer
  (String customerSsn) throws InvalidCustomerException

  ArrayList<Transaction> getTransactionsForAccount
  (String accountNumber) InvalidAccountException

  void deposit(String accountNumber, BigDecimal amount)
  throws InvalidAccountException, InvalidTransactionException

  void withdraw(String accountNumber, BigDecimal amount)
  throws InvalidAccountException, InvalidTransactionException

  void transfer(String debitAccountNumber,
  String creditAccountNumber, BigDecimal amount)
  throws InvalidAccountException, InvalidTransactionException

// if tempted to cut and paste the above into the
// interface, it is a good idea to first paste into a
// standard text editor, to filter out any
editor artifacts


Adding Method Declarations to an Interface


You can either add method declarations to an interface,
the 'old fashion' way, by writing the method declaration
directly into the interface body in the Java editor, or they
can be added using the Visualizer Class Diagram editor.



Declaring methods in an Interface using the Visualizer Class Diagram Editor

// visibility may be obscured by method compartment
// being collapsed or filtered out


Adding Constructors and Java Methods to a class


Constructors and methods are added class in the
way as method declarations are added to an interface
however there are no restrictions that applied to
interfaces.

Following are the Constructors and Methods Needed
for the first few classes listed in the IBM Redbook draft.
As in the code sample shown later in this chapter, the
get / set methods are omitted. The pdf has all the method
elements listed in table form. Here we view them in
Java in stub form. This is a partial preview of the code
that RAD or Eclipse will generate.

ITSOBank Constructor

private ITSOBank() {
this.setCustomers(new HashMap<String, Customer>());
this.setAccounts(new HashMap<String, Account>());
this.setCustomerAccounts(new HashMap<String, ArrayList<Account>>());
this.initializeBank();

}

ITSOBank Methods

Most of ITSOBank's methods are implementations
of the methods associated with the Bank interface.

Account Class Constructor

public Account(String accountNumber, BigDecimal balance) {
this.setAccountNumber(accountNumber);
this.setBalance(balance);
this.setTransactions(new ArrayList<Transaction>());

}


Account Class Methods

public void processTransaction(BigDecimal amount,
String transactionType) throws InvalidTransactionException
{ /*...*/ }


public String toString() {
// . . .
return accountInfo.toString();

}

Customer Class Constructor

public Customer(String ssn, String title, String firstName,
String lastName) {

this.setSsn(ssn);
this.setTitle(title);
this.setFirstName(firstName);
this.setLastName(lastName);
this.setAccounts(new ArrayList<Account>());

}

Customer Class Methods

public void addAccount(Account account)
throws AccountAlreadyExistException
{
}


public void removeAccount(Account account)
throws InvalidAccountException
{
}

public void updateCustomer(String title, String firstName, String lastName)
{
}
public String toString() {
return this.getSsn() + " " + this.getTitle() + " " + this.getLastName()
+ " " + this.getFirstName();

}
}


Account Class Constructor

public Account(String accountNumber, BigDecimal balance) {
this.setAccountNumber(accountNumber);
this.setBalance(balance);
this.setTransactions(new ArrayList<Transaction>());

}

Account Class Methods

public void processTransaction
(BigDecimal amount, String transactionType)
throws InvalidTransactionException
{
}

public String toString() {
return accountInfo.toString();
//
...
}

See the IBM Redbook for data used to create the
constructors and methods for the classes Transaction,
Credit, Debit and BankClient.

Overriding or Implementing Methods

  • right-click the class in the diagram editor
  • or in the Package Explorer
  • Select Source → Override/Implement Methods.
  • the dialog listes eligible methods
  • Select  one
  • Click OK to add stubs to selected class


Curve Ball!

You might have hoped that the application would
carry us through developing the details of the method
implementations. Not so! At least not at this point.
Instead the logic is imported.

Features used to help develop code are described later
in this chapter. Specifically noted are 'Editor features
on page 282 and  'Rapid Application Development'
features on page 300.

// Is it a 'cop out'? We must say no!. If you are a Java developer
// you know how to implement the methods. If you are not a Java
// Developer this would be a time consuming exercise.
 
//  The code content here is standard Java, not particular to J2EE
// and so belongs to the domain of a standard, if advanced, Java
// course.

// When the code is specific to the J2EE APIs we will try to cover it
// carefully

Import the Classes

The classes can be added to the diagram manually,
or the diagram may be imported into the diagram folder.

Diagram File

C:\7501code\java\diagram\ITSOBank-Diagram.dnx

The diagrams appearance can be changed by:

// As an example, the many <<use>>
relationships
// may be hidden.




Creating Relationships between Java Types

"In Application Developer it is possible to model the
relationships between Java types in the Visualizer
Class Diagram editor. This section includes the
following topics:"
Extends relationship

To create an extends relationship between existing
classes:
// extends is sub-classing and is symbolized in UML
// as a solid line ending in a triangular arrow pointing
// to the superclass, like a 'widening definition'


Example


Credit and Debit each extend the Transaction class


Implements relationship

To create an implements relationship between
an existing class and an interface:

Example

The ITSOBank class implements the Bank interface.

// The implements relationship in UML uses a dashed line
// with a triangular arrow pointing to the interface.



Association relationship


ITSO Bank application classes have the following relationships:
To create an association relationship between to classes:
// All the associations are already defined for the imported classes.

Showing associations as Arrows or Attributes
// associations are represented in UML with a solid
// ending in a open arrow , 


The ITSOBank Implementation Code

As time permits we can investigate the code that was
imported. Notice that not all the Exception classes are
present. Also many of the get set  methods are omitted.
Also the Bank interface is not defined. A TransActionType
class is missing which extends the TransAction interface.

Running the Java Application

Once the ITSO Bank application has been completed and
freed and all errors have been resolved, the application can
be tested.

To run the ITSO Bank application:

// the class selected has to have the main( ) method.

// Note: The following sections of the chapter will be given
// cursory treatment up to page 300, where the topic "the
// Java editor and rapid application development" starts


Assignment


Question 1.

In the following exercise, we begin to build the
bank application. Because we don't complete
the process, there will be many unsatisfied
references.

a) Use the features of RAD 7 or Eclipse to generate
the packages associated with the bank application.

b) Use the new --> interface and class functions to build
the Bank interface inside the appropriate package as well
as the ITSOBank and Customer classes.

c) Populate the interface the appropriate methods.

// while they can be written or pasted in go through the
// RAD/Eclipse builder moves for a least some of these.


d) Use the constructor and method builder to add
a constructor and described methods to the ITSOBank
class.

e) In the Customer class use the get / set builder function
to provide get / set methods.

Provide printouts of the interface and class skeleton
generated. Screenshot the package view showing all
the packages created, expanded to show the classes
or interfaces they contain.

Question 2

Import the complete application and run it as described
in Chapter 7. Provide a screen shot of the console view of
the output.