Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help please Write code that demonstrates one computer language calling and

ID: 3808257 • Letter: N

Question

Need help please Write code that demonstrates one computer language calling and receiving data back from another language. At the risk of repeating, the caller must be one language, the receiver a different one. The first language reads three integers from the user (A, B, C) and passes them as binary values to a separately compiled function written in a different language. That second function divides the first number by the second and subtracts the third (A/B-C), then returns the result to the caller as an integer. That first language then prints the integer it received from the second language out to screen. One language or the other must check and prevent or trap for division by zero. You can use any method of inter-language static or dynamic linkage or inter-process communications (CORBA, ZMQ, DCOM, Twisted, Node.JS, dynamic linking. static linking, RPC, etc). The two parts must be distinctly different languages and both parts supplied in source code format (for example VB calling native C++, Python calling C, C# calling C++, C calling Pascal, Python calling Java, Python calling C, Java calling Pascal, etc). Please do not use javascript, HTML or Assembler (that’s just way too easy). Please identify which versions of the languages, compilers, operating systems and developer environments you used.

Explanation / Answer

Following is the most basic way of running any other script from Java. We first save the code to an output file, "Pyscript.py". Then we execute the file using Runtime.exec() method. The output stream of the python is read in back and displayed to the user.

Explanation
1. First we are storing the required python code to perform A-B/C in a string called "code"
2. Then we are writing that string to a temporary file called "Pyscrip.py". Java will create one file and write to it.
3. Then we are taking three input values A, B, C from user console.
4. Executing the file created in step 2, using the command "python Pyscript.py ARG1 ARG2 ARG2"
5. Finally we are reading the output of the python program and printing it.

----------------------------------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Scanner;

public class PythonFromJava {

   public static void main(String a[]) {
       try {

           String code = "import sys "
                       +"print int(sys.argv[1])/int(sys.argv[2])-int(sys.argv[3]); ";

           BufferedWriter out = new BufferedWriter(new FileWriter("Pyscript.py"));
           out.write(code);
           out.close();
          
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter A : ");
           int A = sc.nextInt();
           System.out.print("Enter B : ");
           int B = sc.nextInt();
           System.out.print("Enter C : ");
           int C = sc.nextInt();
          
           if (B == 0) {
               System.out.println("Division By Zero Error!");
               return;
           }
          
           Process proc = Runtime.getRuntime().exec("python Pyscript.py " + A + " " + B + " " + C);
           BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
           double result = new Double(in.readLine()).doubleValue();
           System.out.println("result is : " + result);
       } catch (Exception e) {
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote