Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial.

Implement the Currency Converter Classes

The Converter class needs a method to perform the conversion computation. To add this method to Converter.java:

  1. In the Groups & Files list in the project window, select the Classes group.

  2. In the detail view, double-click Converter.java. Xcode opens the file in an editor window.

  3. Insert the tagged lines in Listing 2-1 into the Converter.java file.

Listing 2-1  Implementation of the Converter class

/* Converter */
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class Converter {
    public float convertDollars(float dollars, float rate) {       // 1
        return dollars * rate;                                     // 2
    }                                                              // 3
}

The convertDollars method multiplies the two arguments and returns the result.

Now, update the empty implementation of the convert method in ConverterController.java that Interface Builder generated for you by changing or adding the tagged lines in Listing 2-2 to the file.

Listing 2-2  Implementation of the ConverterController class

import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
 
public class ConverterController {
    public NSTextField amountField; /* IBOutlet */
    public Converter converter; /* IBOutlet */                     // 1
    public NSTextField dollarField; /* IBOutlet */
    public NSTextField rateField; /* IBOutlet */
 
    public void convert(Object sender) { /* IBAction */
        float dollars, rate, amount;                               // 2
        dollars = dollarField.floatValue();                        // 3
        rate = rateField.floatValue();                             // 4
 
        amount = converter.convertDollars(dollars, rate);          // 5
 
        amountField.setFloatValue(amount);                         // 6
        rateField.selectText(this);                                // 7
    }
}

Notice that line 1 changes the type of the converter instance variable from Object to Converter. This is possible here because the Converter class is now defined in the project (when the ConverterController class was defined in Interface Builder, the Converter class didn’t exist). You could have also done this by explicitly setting the type of the converter outlet to Converter within Interface Builder after defining the Converter class. This is an important step because leaving converter’s type as Object prevents the program from accessing the methods that the Converter class implements. (The Objective-C language doesn’t have this limitation.)

The convert method does the following:

You’ve now completed the implementation of Currency Converter. Are you surprised how little code you had to write, given that your application now has a fully functional converting system and a beautiful Aqua user interface? “Building Currency Converter” shows how to build and run the application.



< Previous PageNext Page > Hide TOC


Last updated: 2006-10-03




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice