コード・サンプル・アイコン サンプル・コード

生成されたJavaクラスに次のメソッドがあります。

public CreditRating() {
    super();
}

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

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ソース・エディタに貼り付けることができます。