

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Java の例
<a name="examples-java"></a>

次のJava例は、ユーザーがインラインコード提案の生成を開始する方法を示しています。これらの例に示しているのは、独自のプロジェクトのコーディングアシスタントをトリガーする方法です。以下の例の大部分は、他の言語に適用されます。
+ [クラスと関数を生成する](#classes-java)
+ [ドキュメントコード](#code-java)
+ [アルゴリズムの生成](#algorithms-java)
+ [ユニットテストを生成する](#tests-java)

## クラスと関数を生成する
<a name="classes-java"></a>

クラス全体または部分的な実装を生成するには、コードコメントを使用します。コメントは、 クラスの意図を記述します。シンプルに開始し、必要に応じて詳細を追加します。クラスを生成した後も、スケルトン内で関数を生成し続けることができます。

コメントを書き込むときは、言語のコメント構造を使用することをお勧めします。ではJava、これは です`/** */`*。*

入力 

```
/**
 * DocumentReader class. has a name, children and optional parent. 
 * Includes I/O functions to read and
 * write document
 */
```

出力: 

```
/**
 * DocumentReader class. has a name, children and optional parent. 
 * Includes I/O functions to read and
 * write document
 */
import java.util.ArrayList;
public class DocumentReader {
    private String name;
    private DocumentReader parent;
    private ArrayList<DocumentReader> children;

    /**
     * Constructor
     * @param name name of the document
     * @param parent parent of the document
     */
    public DocumentReader(String name, DocumentReader parent) {
        this.name = name;
        this.parent = parent;
        this.children = new ArrayList<>();
    }

    /**
     * Constructor
     * @param name name of the document
     */
    public DocumentReader(String name) {
        this(name, null);
    }
    /**
     * Getter for name
     * @return name of the document
     */
    public String getName() {
        return name;
    }

    /**
     * Getter for parent
     * @return parent of the document
     */
    public DocumentReader getParent() {
        return parent;
    }

    /**
     * Getter for children
     * @return children of the document
     */
    public ArrayList<DocumentReader> getChildren() {
        return children;
    }

    /**
     * Adds a child to the document
     * @param child child to be added
     */
    public void addChild(DocumentReader child) {
        children.add(child);
    }

    /**
     * Removes a child from the document
     * @param child child to be removed
     */
    public void removeChild(DocumentReader child) {
        children.remove(child);
    }
}
```

初期クラスができたので、追加のコメントを使用して追加の関数を生成できます。

入力

```
/**
* function to read document from local directory path
* and print first line of document
*/
```

出力:

```
/**
 * function to read document from local directory path
 * and print first line of document
 */
public void readDocument() {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(name));
        String line = reader.readLine();
        System.out.println(line);
        reader.close();
    } catch (IOException e) {
        System.out.println("Error reading document: " + e.getMessage());
    }
}
```

## ドキュメントコード
<a name="code-java"></a>

インラインドキュメントに依存することで、コードドキュメントの時間を短縮します。Java コメントブレーク を追加します`/* */`。

入力 

```
import java.util.function.Function;

public class S3Utils {
    /* 
     * <place cursor here>
    */
    public static Function<String, String> generateS3Function(String bucketName, String region) {
        return (String key) -> {
            String url = generateS3Url(bucketName, key, region);
            return url;
        };
    }
}
```

インライン生成が発生しない場合は、ドキュメントを作成する場所にカーソルを置いた後に Amazon Q Developer のインライン提案を実行します。

出力:

```
import java.util.function.Function;

public class S3Utils {
    /* 
     * Generate a function that returns the S3 URL for a given key
     * @param bucketName the name of the bucket
     * @param region the region of the bucket
     * @return the function that generates the S3 URL
    */
    public static Function<String, String> generateS3Function(String bucketName, String region) {
        return (String key) -> {
            String url = generateS3Url(bucketName, key, region);
            return url;
        };
    }
}
```

コード (クラス、関数、またはスクリプト要素) の簡単な説明には、ドキュメントを追加するコードセクションの前に戦略的に配置されたフォワード文字 (`//`) を使用します。

入力

```
// 
public class S3Utils {
    /* 
     * Generate a function that returns the S3 URL for a given key
     * @param bucketName the name of the bucket
     * @param region the region of the bucket
     * @return the function that generates the S3 URL
    */
    public static Function<String, String> generateS3Function(String bucketName, String region) {
        return (String key) -> {
            String url = generateS3Url(bucketName, key, region);
            return url;
        };
    }
}
```

出力:

```
// This class is used to generate a function that returns the S3 URL for a given key
public class S3Utils {
    /* 
     * Generate a function that returns the S3 URL for a given key
     * @param bucketName the name of the bucket
     * @param region the region of the bucket
     * @return the function that generates the S3 URL
    */
    public static Function<String, String> generateS3Function(String bucketName, String region) {
        return (String key) -> {
            String url = generateS3Url(bucketName, key, region);
            return url;
        };
    }
}
```

## アルゴリズムの生成
<a name="algorithms-java"></a>

Amazon Q Developer は、特に AWS SDKs に関連する一般的なアルゴリズムを生成できます。スクリプトで、必要な関数を指定するプロンプトを追加します。

この例では、名前値へのユーザー ID キーのハッシュマップを生成し、バブルソートアルゴリズムを使用してソート関数を生成します。

入力

```
import java.util.HashMap;
public class SortList {

    // generate key value pair hash map for 
    // user id and name
    public static HashMap<Integer, String> getUserIdNameMap() {
        HashMap<Integer, String> userIdNameMap = new HashMap<Integer, String>();
        userIdNameMap.put(1, "Amit");
        userIdNameMap.put(2, "Rahul");
        userIdNameMap.put(3, "Vijay");
        userIdNameMap.put(4, "Raj");
        userIdNameMap.put(5, "Kumar");
        return userIdNameMap;
    }

    /*
    * Create function to sort UserIdName map by name alphabetically using bubble sort
    */ 
    
    
}
```

出力:

```
/*
* Create function to sort UserIdName map by name alphabetically using bubble sort
*/ 
public static HashMap<Integer, String> sortUserIdNameMap(HashMap<Integer, String> userIdNameMap) {
    int size = userIdNameMap.size();
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (userIdNameMap.get(i).compareTo(userIdNameMap.get(j)) > 0) {
                String temp = userIdNameMap.get(i);
                userIdNameMap.put(i, userIdNameMap.get(j));
                userIdNameMap.put(j, temp);
            }
        }
    }
    return userIdNameMap;
}
```

さらに、ユーザーは標準手順のプロンプトを作成できます。この例では、ユーザーはハッシュマップを Amazon DynamoDB テーブルに保存するためのプロンプトを作成します。スクリプトの先頭に、ユーザーは Amazon Q Developer に追加コンテキストを提供するのに役立つ DynamoDB Javaライブラリを追加するプロンプトを追加します。

入力

```
// add libraries for Amazon DynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;

     /*
     * Store sorted UserIdName map into Amazon DynamoDB table named users
     */
```

出力:

```
// add libraries for Amazon DynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;

     /*
     * Store sorted UserIdName map into Amazon DynamoDB table named users
     */
    public static void storeSortedUserIdNameMap(HashMap<Integer, String> userIdNameMap) {
        // create Amazon DynamoDB client
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();

        // store sorted UserIdName map into Amazon DynamoDB table named users
        for (int userId : userIdNameMap.keySet()) {
            String name = userIdNameMap.get(userId);
            client.putItem("users", new HashMap<String, Object>() {{
                put("userId", userId);
                put("name", name);
            }});
        }
    }
```

## ユニットテストを生成する
<a name="tests-java"></a>

インラインコメントを使用すると、Amazon Q Developer はアサーションやその他の JUnit テストを実行するためのユニットテストを生成できます。この例では、ユーザーはまず Amazon Q にJUnit がテストスクリプトのコンテキストを設定するための適切なライブラリを生成するように依頼します。次に、ユーザーはパブリッククラスを作成し、その後に宣言子とコメント文字列を作成します。

入力

```
// import libraries for java unit tests
import org.junit.Test;
import static org.junit.Assert.*;

public class UnitTests {

    @Test 
    /*
     * Create unit test for getUserIdNameMap
     */  
}
```

出力:

```
@Test 
/*
* Create unit test for getUserIdNameMap
*/
public void testGetUserIdNameMap() {
    // create a new instance of the UserIdNameMap class
    UserIdNameMap userIdNameMap = new UserIdNameMap();

    // create a new instance of the User class
    User user = new User("John", "Doe");

    // add the user to the map
    userIdNameMap.addUser(user);

    // get the user from the map
    User retrievedUser = userIdNameMap.getUser("John");

    // assert that the retrieved user is the same as the user added to the map
    assertEquals(user, retrievedUser);
}
```

さらに、ユーザーと Amazon Q Developer の間のチャットでは、ユーザーのコード入力プロンプトに基づいてユニットテストを提案して生成します。詳細については、[「チャットの例](examples-chat.md)」を参照してください。