Example: Yield on a Security

The yield on a 10 year bond which pays interest semiannually is returned in this example.
import com.imsl.finance.*;
import java.text.*;
import java.util.*;

public class yieldEx1 {
    static final DateFormat dateFormat =
    DateFormat.getDateInstance(DateFormat.SHORT,  Locale.US);
    
    static private GregorianCalendar parse(String s) throws ParseException {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(dateFormat.parse(s));
        return cal;
    }
    
    public static void main(String args[]) throws ParseException {
        GregorianCalendar settlement = parse("7/1/85");
        GregorianCalendar maturity = parse("7/1/95");
        double rate = .06;
        double price = 95.40663;
        double redemption = 105.;
        int frequency = Bond.SEMIANNUAL;
        DayCountBasis dcb = DayCountBasis.BasisNASD;
        double yield = Bond.yield(settlement, maturity, rate, price,
        redemption, frequency, dcb);
        System.out.println("The yield of the bond is " + yield);
    }
}

Output

The yield of the bond is 0.06999999682842899
Link to Java source.