code sample iconサンプル・コード

生成されたJavaクラスで、次の行を探します。

                           
public CreditRating() {
}
                        

この行のあとに、次のメソッドを追加します。このメソッドは、入力文字列の値を受け入れ、処理してから、整数値を返します。

                           
public int processRating (String ssn) {
    int id;
    try
    {
        // Parses integer value of first 3 numbers form SSN 
        id = Integer.parseInt(ssn.substring(0, 3));
    }
    catch (NumberFormatException e)
    {
        // if SSN is invalid returns -1 
        return -1;
    }
    if (id < 300)
    {
        // If value of the first 3 numbers from customer SSN is less 
        // than 300, rating is 1. 
        return 1;
    }
    else if (id < 600)
    {
        // If value less than 600, rating is 2. 
        return 2;
    }
    else if (id < 900)
    {
        // If value less than 900, rating is 3. 
        return 3;
    }
    else
    {
       // Otherwise, rating is 0. 
        return 0;
    }
}
                        

サンプル・コードを入力する代わりに、このウィンドウのコードをコピーしてから、Javaソース・エディタに貼り付けることもできます。

Copyright © 1997, 2008, Oracle. All rights reserved.