diff --git a/Codes/Group A/Assignment-A3/A3.c b/Codes/Group A/Assignment-A3/A3.c index 7076eb0..5c9eff7 100644 --- a/Codes/Group A/Assignment-A3/A3.c +++ b/Codes/Group A/Assignment-A3/A3.c @@ -2,25 +2,29 @@ #include #include "A3.h" -JNIEXPORT jint JNICALL Java_A3_add(JNIEnv *env, jobject obj, jint a, jint b) { +// NOTE: No need to memorize this file +// The contents of this file can be referenced from A3.h which is the generated header file +// Refer explanation for more info: https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Assignment-A3/EXPLANATION.md + +JNIEXPORT jint JNICALL Java_A3_add(JNIEnv *env, jobject obj, jint a, jint b) { // Function for addition jint result = a + b; printf("\n%d + %d = %d\n", a, b, result); return result; // Return the result } -JNIEXPORT jint JNICALL Java_A3_sub(JNIEnv *env, jobject obj, jint a, jint b) { +JNIEXPORT jint JNICALL Java_A3_sub(JNIEnv *env, jobject obj, jint a, jint b) { // Function for subtraction jint result = a - b; printf("\n%d - %d = %d\n", a, b, result); return result; // Return the result } -JNIEXPORT jint JNICALL Java_A3_mult(JNIEnv *env, jobject obj, jint a, jint b) { +JNIEXPORT jint JNICALL Java_A3_mul(JNIEnv *env, jobject obj, jint a, jint b) { // Function for multiplication jint result = a * b; printf("\n%d * %d = %d\n", a, b, result); return result; // Return the result } -JNIEXPORT jint JNICALL Java_A3_div(JNIEnv *env, jobject obj, jint a, jint b) { +JNIEXPORT jint JNICALL Java_A3_div(JNIEnv *env, jobject obj, jint a, jint b) { // Function for division if (b == 0) { printf("Error: Division by zero.\n"); return 0; // Return 0 or handle error appropriately diff --git a/Codes/Group A/Assignment-A3/A3.java b/Codes/Group A/Assignment-A3/A3.java index e12fc79..963a1f4 100644 --- a/Codes/Group A/Assignment-A3/A3.java +++ b/Codes/Group A/Assignment-A3/A3.java @@ -1,45 +1,67 @@ -import java.io.*; -import java.util.*; +// Importing basic stuff +import java.io.*; // Used for I/O operations +import java.util.*; // Contains basic utilities class A3 { - static { - System.loadLibrary("A3"); - } - - private native int add(int a, int b); - private native int sub(int a, int b); - private native int mult(int a, int b); - private native int div(int a, int b); - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int a, b, ch; - - System.out.println("\nEnter value of a : "); - a = sc.nextInt(); - System.out.println("\nEnter value of b : "); - b = sc.nextInt(); - - do { - System.out.println("\nENTER YOUR CHOICE : "); - ch = sc.nextInt(); - switch (ch) { - case 1: - new A3().add(a, b); - break; - case 2: - new A3().sub(a, b); - break; - case 3: - new A3().mult(a, b); - break; - case 4: - new A3().div(a, b); - break; - default: - System.out.println("Your choice is wrong."); - } - } while (ch < 5); + // Class name has to be same as file name for Java + + static { + // Used for loading the .so (on Linux) or .dll (on Windows) file when running + // This is the main so called "dynamic library" + System.loadLibrary("A3"); + } + + // Function declaration + // private indicates the function is private, duh! + // Use of native indicates the function body will be written in a language other than Java, such as C/C++ + private native int add(int a, int b); // For addition + private native int sub(int a, int b); // For subtraction + private native int mul(int a, int b); // For multiplication + private native int div(int a, int b); // For division + + public static void main(String[] args) { // the main function + Scanner sc = new Scanner(System.in); // For taking input + + int a, b;// Declaring variables for calculation + int choice = 0; // Declaring variable for switch-case + + // Take input for a and b values + System.out.print("\nValue of a:\t"); + a = sc.nextInt(); + System.out.print("\nValue of b:\t"); + b = sc.nextInt(); + + // Main menu + while (true) { + System.out.println("----- MAIN MENU -----"); + System.out.println("1 -> Addition"); + System.out.println("2 -> Subtraction"); + System.out.println("3 -> Multiplication"); + System.out.println("4 -> Division"); + System.out.println("5 -> Exit"); + System.out.println("Choose an option:\t"); + choice = sc.nextInt(); + + switch (choice) { + case 1: + new A3().add(a, b); + break; + case 2: + new A3().sub(a, b); + break; + case 3: + new A3().mul(a, b); + break; + case 4: + new A3().div(a, b); + break; + case 5: + System.out.println("## END OF CODE"); + System.exit(0); + default: + System.out.println("Please choose a valid option."); + break; + } } + } } -