There are two ways to run Java code:
- VS Code: Use Java extensions. Write your Java code in separate
.javafiles and run them inside VS Code. Install the Java Extension Pack for support. - Jupyter Notebooks: Install the IJava Kernel from here. Follow the instructions to set it up, then create notebooks and select the Java kernel.
Make sure to enable assertions by setting the value "-ea" in VS Code. This is essential for running assertions in your Java test cases. You can enable it by navigating to this setting:
vscode://settings/java.debug.settings.vmArgs
For running assertions in IJava Kernel:
You have to edit the kernel.json file located at:
C:\Users\k26ra\AppData\Local\Programs\Python\Python312\share\jupyter\kernels\java.
Note: Replace "k26ra" with your own username in the path.
To enable assertions, the -ea flag must be passed in the JVM arguments. In the IJava kernel, this can be done by editing the kernel.json and adding the -ea flag to the argv list.
The updated kernel.json would look like this:
{
"argv": [
"java",
"-ea",
"-jar",
"C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python312\\share\\jupyter\\kernels\\java/ijava-1.3.0.jar",
"{connection_file}"
],
"display_name": "Java",
"env": {},
"interrupt_mode": "message",
"language": "java"
}-
Navigate to the Package Directory:
- Navigate to the package directory for the set you want to solve the problems, e.g.,
Basic_17.
- Navigate to the package directory for the set you want to solve the problems, e.g.,
-
Create a New Java File:
- Create a new file with the
.javaextension, e.g.,Problem_2.java. - Implement your Java class within this file.
package Basic_17; public class Problem_2 { public static void main(String[] args) { // Implementation call System.out.println(problemFunction(arguments)); // Uncomment the following line to run tests // runTests(); } // Define your problem-specific method here public static ReturnType problemFunction(Parameters) { // Your implementation here } // Define a method for running tests public static void runTests() { // Test calls System.out.println("All tests passed!"); } }
- Create a new file with the
-
Problem-Specific Method: Define a specific method for the problem you are solving (e.g.,
problemFunction). This method should contain the logic for solving the problem. -
Test Method: Create a
runTestsmethod that contains assertions to validate your problem function. Each assertion should check if the method returns the expected result. This helps ensure your implementation is correct. -
Main Method: The
mainmethod should include a call to your problem-specific method with sample input and print the result. After confirming your implementation is correct, you can uncomment the call torunTeststo run your test cases.
-
Open VS Code:
- Open your project directory (
Java-Practice) in VS Code.
- Open your project directory (
-
Install Java Extensions (if not already installed):
- Install the "Java Extension Pack" from the Extensions view (Ctrl+Shift+X) to enable Java development features.
-
Open the Java File:
- Navigate to the file you want to run (e.g.,
Problem_2.java) and open it in the editor.
- Navigate to the file you want to run (e.g.,
-
Run the Java File:
- You can use the "Run" button provided by the Java extension. This button appears just above the
mainmethod in your class. Clicking it will compile and run the file automatically.
Note: After creating a new Java file, you do not need to manually compile it. VS Code automatically handles the compilation and stores the
.classfiles in thebindirectory. - You can use the "Run" button provided by the Java extension. This button appears just above the