3
1

Simplified, added description and modified the code for better understanding.

This commit is contained in:
K 2024-11-06 02:49:59 +05:30
parent 7aa630ae71
commit c9864cb436
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 71 additions and 45 deletions

View File

@ -2,25 +2,29 @@
#include <stdio.h> #include <stdio.h>
#include "A3.h" #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; jint result = a + b;
printf("\n%d + %d = %d\n", a, b, result); printf("\n%d + %d = %d\n", a, b, result);
return result; // Return the 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; jint result = a - b;
printf("\n%d - %d = %d\n", a, b, result); printf("\n%d - %d = %d\n", a, b, result);
return result; // Return the 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; jint result = a * b;
printf("\n%d * %d = %d\n", a, b, result); printf("\n%d * %d = %d\n", a, b, result);
return result; // Return the 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) { if (b == 0) {
printf("Error: Division by zero.\n"); printf("Error: Division by zero.\n");
return 0; // Return 0 or handle error appropriately return 0; // Return 0 or handle error appropriately

View File

@ -1,45 +1,67 @@
import java.io.*; // Importing basic stuff
import java.util.*; import java.io.*; // Used for I/O operations
import java.util.*; // Contains basic utilities
class A3 { class A3 {
static { // Class name has to be same as file name for Java
System.loadLibrary("A3");
} static {
// Used for loading the .so (on Linux) or .dll (on Windows) file when running
private native int add(int a, int b); // This is the main so called "dynamic library"
private native int sub(int a, int b); System.loadLibrary("A3");
private native int mult(int a, int b); }
private native int div(int a, int b);
// Function declaration
public static void main(String[] args) { // private indicates the function is private, duh!
Scanner sc = new Scanner(System.in); // Use of native indicates the function body will be written in a language other than Java, such as C/C++
int a, b, ch; private native int add(int a, int b); // For addition
private native int sub(int a, int b); // For subtraction
System.out.println("\nEnter value of a : "); private native int mul(int a, int b); // For multiplication
a = sc.nextInt(); private native int div(int a, int b); // For division
System.out.println("\nEnter value of b : ");
b = sc.nextInt(); public static void main(String[] args) { // the main function
Scanner sc = new Scanner(System.in); // For taking input
do {
System.out.println("\nENTER YOUR CHOICE : "); int a, b;// Declaring variables for calculation
ch = sc.nextInt(); int choice = 0; // Declaring variable for switch-case
switch (ch) {
case 1: // Take input for a and b values
new A3().add(a, b); System.out.print("\nValue of a:\t");
break; a = sc.nextInt();
case 2: System.out.print("\nValue of b:\t");
new A3().sub(a, b); b = sc.nextInt();
break;
case 3: // Main menu
new A3().mult(a, b); while (true) {
break; System.out.println("----- MAIN MENU -----");
case 4: System.out.println("1 -> Addition");
new A3().div(a, b); System.out.println("2 -> Subtraction");
break; System.out.println("3 -> Multiplication");
default: System.out.println("4 -> Division");
System.out.println("Your choice is wrong."); System.out.println("5 -> Exit");
} System.out.println("Choose an option:\t");
} while (ch < 5); 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;
}
} }
}
} }