Activity ‐ Generating Java Code - accentient/github-copilot-devs GitHub Wiki

Generating Java Code with GitHub Copilot

Objective

This activity introduces Java developers to GitHub Copilot while building a simple Unit Converter application. The activity demonstrates how Copilot assists in generating code, providing suggestions, and reducing development time. By the end of the activity, students will:

  • Understand how to integrate and use GitHub Copilot in VS Code or IntelliJ
  • Experience generating Java code snippets with Copilot
  • Build and test a Unit Converter application using Copilot-generated code
  • Learn how to refine Copilot suggestions through comments and prompts

Project

Use GitHub Copilot to scaffold and write a simple Unit Converter console app in Java that converts length (meters ↔ feet), temperature (Celsius ↔ Fahrenheit), and weight (kilograms ↔ pounds).

Prerequisites

  • Java Development Kit (JDK) installed
  • VS Code or IntelliJ IDEA with GitHub Copilot enabled
  • Active GitHub Copilot subscription

Steps

Step 1: Project setup

  • Create a new Java project
Scaffold a new Java console app named JavaDemo

Step 2: Create a UnitConverter class

  • Open Main.java and add a comment at the top to prompt GitHub Copilot
// Create an empty private UnitConverter class
  • Press Tab and Enter to accept the generated code

Step 3: Implement Length Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
// Implement length conversion methods
  • Press Tab and Enter to accept the generated code, method by method; or, press Alt + ] or Alt + [ to see the next or previous suggestion. You can also press Ctrl + Enter to open a new tab showing multiple suggestions. To accept a suggestion, below the suggestion, click Accept suggestion NUMBER. To reject all suggestions, close the tab.

Step 4: Implement Temperature Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
// Implement temperature conversion methods

Step 5: Implement Weight Conversion

  • Within the UnitConverter class, add a comment to prompt GitHub Copilot
// Implement weight conversion methods
  • Review the code and ensure that it builds

Step 6: Implement a Client UI

  • Within the Main class, add a comment to prompt GitHub Copilot
// Call conversion methods
  • Review the code
  • Build/Run the app

Step 7: Ask GitHub Copilot to Explain Code

  • Within the UnitConverter class, highlight a method, right-click and select Copilot > Explain
  • In the GitHub Copilot Chat window, ask some additional questions about the code
Review all of the code. Is it secure?
Is the code threadsafe?
Do you see any open source code?
Is this code easy to understand and maintainable?
Are there any edge cases this code might fail to handle?

Step 8: Add Error Handling

  • Within the UnitConverter class, highlight one of the conversion methods, right-click and select GitHub Copilot > Inline Chat
Add error handling
  • Accept/insert the code if you want
  • Do this for other methods if you want, or for multiple methods at once
  • Review the code
  • Build/Run the app

Step 9: Improve the Code

  • Highlight all the code and bring up Inline Chat
/fix Improve the code to use common Java coding conventions. As part of the refactoring, configure constants, refactor code into methods, and use meaningful variable names.
  • Insert or copy/paste the code if you want
  • Review the code
  • Build/Run the app

Summary

In this hands-on activity, you built a unit converter application in Java using GitHub Copilot as an AI coding assistant. Through guided prompts, you learned to integrate Copilot into their development workflow, generated code for length, temperature, and weight conversions, wrote JUnit tests, and refined AI-generated suggestions, gaining practical experience in accelerating software development with AI tools.