Reading a UML Class Diagram

UML class overview

Where:

  • scope is indicated as + public, - private, (# protected, ~ package - these are not focused on in CS149).
  • The arg list is a list of parameter types (e.g., int, double, String) it can also include type after a :
  • Class (i.e. static) methods and fields are indicated by underlining
  • Constant (i.e. final) fields are indicated via naming convention: constants should be in ALL_CAPS

Lines between classes indicate relationships as follows:

UML relationships

and can have numbers indicating instances of or 'multiplicity' as follows:

0 No instances (rare)
0..1 No instances, or one instance
1 Exactly one instance
1..1 Exactly one instance
0..* Zero or more instances
* Zero or more instances
1..* One or more instances

For example:

Credit Card UML Diagram

Where the Credit card Java class looks like this:

    
/**
 * Credit Card Class. 
 * This class will represent a basic credit card.
 * @author chaoaj
 * @version 12-1-16
 */

public class CreditCard {
    // Constants
    public static final double APR = 0.2999;
    
    public static final double CREDIT_LIMIT = 2000.00;
    // Attributes
    
    private String cardNumber;

    private int ownerNumber;
    
    private double balance;
    
    private int securityPin;
    
    //Constructors
    
    /**
     * Default Constructor.
     */
    
    public CreditCard() {
        this.cardNumber = "9999-9999-9999-9999";
        this.ownerNumber = 999;
        this.balance = 0.0;
        this.securityPin = 123;
    }
    
/**
 * CreditCard Constructor.
 * @param cardNumber card number.
 * @param ownerNumber owner number.
 * @return
 */
    
    public CreditCard(String cardNumber, int ownerNumber) {
        this.cardNumber = cardNumber;
        this.ownerNumber = ownerNumber;
        this.balance = 0.0;
        this.securityPin = 123;
    }
    
    /**
     * getBalance - gets the card balance for a particular owner.
     * @param ownerNumber - number of the owner for the card.
     * @return double value of balance on card.
     */
    public double getBalance(int ownerNumber) {
        return this.balance;
    }
    /**
     * adjustBalance - sets the card balance to the input double.
     * @param adjustment - amount to adjust balance. 
     */
    
    public void adjustBalance(double adjustment) {
        if ((this.balance + adjustment) < CREDIT_LIMIT) {
            this.balance += adjustment;
        } else {
            System.out.println("Over the limit");
        }
    }

    /**
     * toString method.
     * @param card - creditcard.
     * @return String - string of card info.
     */
    
    public String toString(CreditCard card) {
        String s = "Card Number: " + card.cardNumber + ", "
                + "for Owner: " + card.ownerNumber + ", "
                + "has Balance: " + card.balance + ".";
        return s;        
    }
    
/**
 * equals - check if all values are equal for cards.    
 * @param card - card to check against.
 * @return boolean - true if equal, false if not.
 */
    
    public boolean equals(CreditCard card) {
        boolean returnValue = false;
        if (this.cardNumber.equals(card.cardNumber) 
                && this.ownerNumber == card.ownerNumber
                && this.balance == card.balance
                && this.securityPin == card.securityPin) {
            returnValue = true;
        }
        return returnValue;
    }
}
    

Back to Top