Simplified, added description and modified the code for better understanding.
This commit is contained in:
parent
7aa630ae71
commit
c9864cb436
@ -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
|
||||||
|
@ -1,29 +1,48 @@
|
|||||||
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 {
|
||||||
|
// Class name has to be same as file name for Java
|
||||||
|
|
||||||
static {
|
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");
|
System.loadLibrary("A3");
|
||||||
}
|
}
|
||||||
|
|
||||||
private native int add(int a, int b);
|
// Function declaration
|
||||||
private native int sub(int a, int b);
|
// private indicates the function is private, duh!
|
||||||
private native int mult(int a, int b);
|
// Use of native indicates the function body will be written in a language other than Java, such as C/C++
|
||||||
private native int div(int a, int b);
|
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) {
|
public static void main(String[] args) { // the main function
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in); // For taking input
|
||||||
int a, b, ch;
|
|
||||||
|
|
||||||
System.out.println("\nEnter value of a : ");
|
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();
|
a = sc.nextInt();
|
||||||
System.out.println("\nEnter value of b : ");
|
System.out.print("\nValue of b:\t");
|
||||||
b = sc.nextInt();
|
b = sc.nextInt();
|
||||||
|
|
||||||
do {
|
// Main menu
|
||||||
System.out.println("\nENTER YOUR CHOICE : ");
|
while (true) {
|
||||||
ch = sc.nextInt();
|
System.out.println("----- MAIN MENU -----");
|
||||||
switch (ch) {
|
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:
|
case 1:
|
||||||
new A3().add(a, b);
|
new A3().add(a, b);
|
||||||
break;
|
break;
|
||||||
@ -31,15 +50,18 @@ class A3 {
|
|||||||
new A3().sub(a, b);
|
new A3().sub(a, b);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
new A3().mult(a, b);
|
new A3().mul(a, b);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
new A3().div(a, b);
|
new A3().div(a, b);
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
System.out.println("## END OF CODE");
|
||||||
|
System.exit(0);
|
||||||
default:
|
default:
|
||||||
System.out.println("Your choice is wrong.");
|
System.out.println("Please choose a valid option.");
|
||||||
}
|
break;
|
||||||
} while (ch < 5);
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user