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

Write a Python code from this Java codes its 4 different class /////------------

ID: 667967 • Letter: W

Question

Write a Python code from this Java codes its 4 different class

/////-----------------------------------------------------------------------

import java.io.*;
import java.net.Socket;

public class Client {

public static final int PORT = 12321;

public static void main(String[] args) throws IOException {
String resp1 = sendAndReceive("S8 10 -945 1689 -950 230 -25 1 1e-13");
if(resp1.charAt(0) == 'S') {
double val1 = Double.parseDouble(resp1.substring(1));
System.out.println("Value returned from bisection: " + val1 );
String resp2 = sendAndReceive("E" + val1 + " -945 1689 -950 230 -25 1");
if(resp2.charAt(0) == 'E') {
double val2 = Double.parseDouble(resp2.substring(1));
System.out.println("Value returned from eval: " + val2 );
} else {
System.out.println("Error in evaluation call: " + resp2.substring(1));

}
} else {
System.out.println("Error in bisection call: " + resp1.substring(1));
}
}


/**
* Sends a request to the server, prints it and then prints the response.
*
*/
public static String sendAndReceive(String request) throws IOException {
System.out.println("request: " + request);
Socket conn = new Socket("localhost", PORT);
OutputStream os = conn.getOutputStream();
Writer wr = new OutputStreamWriter(os, "UTF-8");
wr.write(request);
wr.flush();
conn.shutdownOutput();

InputStream is = conn.getInputStream();
Reader rdr = new InputStreamReader(is,"UTF-8");
StringBuilder sb = new StringBuilder();
int c = rdr.read();
while(c >= 0) {
sb.append((char)c);
c = rdr.read();
}
// System.out.println("response: " + sb);
conn.shutdownInput();

conn.close();
return sb.toString();

}

}

//////-----------------------------------------------------------------------

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.DoubleSummaryStatistics;

public class Server {

public static final int PORT = 12321;

public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(PORT);
while(true) {
Socket conn = ss.accept();

Reader rdr = new InputStreamReader(conn.getInputStream(), "UTF-8");
StringBuilder sb = new StringBuilder();
int c = rdr.read();
while(c >= 0) {
sb.append((char)c);
c = rdr.read();
}
conn.shutdownInput();
String req = sb.toString();
System.out.println("request: " + req );

String response = "response was not set properly";
if(req.length() == 0) {
response = "XInvalid request syntax: first character should be the operation code";
} else if(req.charAt(0) == 'E') {
String[] params = req.substring(1).split(" ");
if(params.length < 1) {
response = "XInvalid request syntax: wrong number of fields in request";
} else {
try {
double x = Double.parseDouble(params[0]);
double poly[] = new double[params.length-1];
for (int i = 1 ; i < params.length; i++) {
poly[i-1] = Double.parseDouble(params[i]);
}
response = "E" + evaluate(x, poly);
} catch (NumberFormatException nfe) {
response = "Xinvalid format numeric data";
}
}
} else if(req.charAt(0) == 'S') {
String[] params = req.substring(1).split(" ");
if(params.length < 3) {
System.out.println("params length " + params.length);
response = "XInvalid request syntax: wrong number of fields in request";
} else {
try {
double a = Double.parseDouble(params[0]);
double b = Double.parseDouble(params[1]);
double tol = Double.parseDouble(params[params.length-1]);
double poly[] = new double[params.length-3];
for (int i = 2 ; i < params.length-1; i++) {
poly[i-2] = Double.parseDouble(params[i]);
}
response = "S" + bisection(a,b,poly,tol);
} catch (NumberFormatException nfe) {
response = "Xinvalid format numeric data";
} catch (IllegalArgumentException iae) {
response = "X" + iae.getMessage();
}

}
} else {
response = "XInvalid operation code |" + req.charAt(0) + "|";
}
Writer wrt = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
wrt.write(response);
wrt.flush();
conn.shutdownOutput();
conn.close();
}
}
private static double evaluate(double x, double[] poly) {
double val = 0.0;
for( int i = poly.length-1; i >= 0; i--) {
val = val * x + poly[i];
}
return val;
}
private static double bisection(double a, double b, double[] poly, double tolerance) {
if(evaluate(a, poly) > 0 ){
throw new IllegalArgumentException("Value of polynomial at a must be non-positive: " + a + " -> " + evaluate(a,poly));
}
if(evaluate(b, poly) < 0 ){
throw new IllegalArgumentException("Value of polynomial at b must be non-negative: " + b + " -> " + evaluate(b,poly));
}
while(Math.abs(b-a) > tolerance) {
double mid = (a+b)/2;
double val = evaluate(mid, poly);
if( val <= 0)
a = mid;
else
b = mid;
}
return (a+b)/2;
}
}

//////////////////////-----------------------------------------------

import static org.junit.Assert.*;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
public class TestJ {

public static final int PORT = 12321;
private static final double TOLERANCE = 1e-10;
@Test
public void evaluate1() throws IOException {
String result;
result = sendAndReceive("E1 -945 1689 -950 230 -25 1");
assertEquals('E', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(0.0, value, TOLERANCE);
}
@Test
public void evaluate2() throws IOException {
String result;
result = sendAndReceive("E3 -945 1689 -950 230 -25 1");
assertEquals('E', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(0.0, value, TOLERANCE);
}

@Test
public void evaluate3() throws IOException {
String result;
result = sendAndReceive("E5 -945 1689 -950 230 -25 1");
assertEquals('E', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(0.0, value, TOLERANCE);
}

@Test
public void evaluate4() throws IOException {
String result;
result = sendAndReceive("E7 -945 1689 -950 230 -25 1");
assertEquals('E', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(0.0, value, TOLERANCE);
}

@Test
public void evaluate5() throws IOException {
String result;
result = sendAndReceive("E9 -945 1689 -950 230 -25 1");
assertEquals('E', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(0.0, value, TOLERANCE);
}


@Test
public void bisect1() throws IOException {
String result;
result = sendAndReceive("S0 2 -945 1689 -950 230 -25 1 1e-10");
assertEquals('S', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(1.0, value, TOLERANCE);
}


@Test
public void bisect2() throws IOException {
String result;
result = sendAndReceive("S4 2 -945 1689 -950 230 -25 1 1e-10");
assertEquals('S', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(3.0, value, TOLERANCE);
}

@Test
public void bisect3() throws IOException {
String result;
result = sendAndReceive("S4 6 -945 1689 -950 230 -25 1 1e-10");
assertEquals('S', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(5.0, value, TOLERANCE);
}

@Test
public void bisect4() throws IOException {
String result;
result = sendAndReceive("S8 6 -945 1689 -950 230 -25 1 1e-10");
assertEquals('S', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(7.0, value, TOLERANCE);
}

@Test
public void bisect5() throws IOException {
String result;
result = sendAndReceive("S8 10 -945 1689 -950 230 -25 1 1e-10");
assertEquals('S', result.charAt(0));
double value = Double.parseDouble(result.substring(1));
assertEquals(9.0, value, TOLERANCE);
}


@Test
public void formatErrorExtraSpace1() throws IOException {
String result;
result = sendAndReceive("S8 10 -945 1689 -950 230 -25 1 1e-10");
assertEquals('X', result.charAt(0));
}

@Test
public void formatErrorExtraSpace2() throws IOException {
String result;
result = sendAndReceive("E8 -945 1689 -950 230 -25 1");
assertEquals('X', result.charAt(0));
}

@Test
public void formatErrorBadNumber1() throws IOException {
String result;
result = sendAndReceive("S8 10 -945 1689 -950a 230 -25 1 1e-10");
assertEquals('X', result.charAt(0));
}

@Test
public void formatErrorBadNumber2() throws IOException {
String result;
result = sendAndReceive("S8 -945 x1689 -950 230 -25 1");
assertEquals('X', result.charAt(0));
}

@Test
public void formatErrorBadOperation() throws IOException {
String result;
result = sendAndReceive("T8 -945 x1689 -950 230 -25 1");
assertEquals('X', result.charAt(0));
}
@Test
public void formatErrorEmptyRequest() throws IOException {
String result;
result = sendAndReceive("");
assertEquals('X', result.charAt(0));
}
public static String sendAndReceive(String request) throws IOException {
System.out.println("request: " + request);
Socket conn = new Socket("localhost", PORT);
OutputStream os = conn.getOutputStream();
Writer wr = new OutputStreamWriter(os, "UTF-8");
wr.write(request);
wr.flush();
conn.shutdownOutput();

InputStream is = conn.getInputStream();
Reader rdr = new InputStreamReader(is,"UTF-8");
StringBuilder sb = new StringBuilder();
int c = rdr.read();
while(c >= 0) {
sb.append((char)c);
c = rdr.read();
}
//System.out.println("response: " + sb);
conn.shutdownInput();

conn.close();
return sb.toString();
}
}

////////----------------------------------------------------------------------------

package cs4320.assign02;

import java.io.*;
import java.net.Socket;
public class TryoutServer {

public static final int PORT = 12321;

public static void main(String[] args) throws IOException {
sendAndReceive("E1 -945 1689 -950 230 -25 1");
sendAndReceive("E3 -945 1689 -950 230 -25 1");
sendAndReceive("E5 -945 1689 -950 230 -25 1");
sendAndReceive("E7 -945 1689 -950 230 -25 1");
sendAndReceive("E9 -945 1689 -950 230 -25 1");
sendAndReceive("S8 10 -945 1689 -950 230 -25 1 1e-10");
sendAndReceive("S8 6 -945 1689 -950 230 -25 1 1e-10");
sendAndReceive("S4 6 -945 1689 -950 230 -25 1 1e-10");
sendAndReceive("S4 2 -945 1689 -950 230 -25 1 1e-10");
sendAndReceive("S0 2 -945 1689 -950 230 -25 1 1e-10");

}
public static String sendAndReceive(String request) throws IOException {
System.out.println("request: " + request);
Socket conn = new Socket("localhost", PORT);
OutputStream os = conn.getOutputStream();
Writer wr = new OutputStreamWriter(os, "UTF-8");
wr.write(request);
wr.flush();
conn.shutdownOutput();

InputStream is = conn.getInputStream();
Reader rdr = new InputStreamReader(is,"UTF-8");
StringBuilder sb = new StringBuilder();
int c = rdr.read();
while(c >= 0) {
sb.append((char)c);
c = rdr.read();
}
System.out.println("response: " + sb);
conn.shutdownInput();

conn.close();
return sb.toString();
}
}

Explanation / Answer

Hi there,

You want python code for this language, I convert most of itlike your java code.

step 1:

I think this is your practical class code in java: RPC/RMI ->remote procedure call and remote method invocation in socket.

Step 2:

First in python you should create a socket by

Step 3:

Connect to the port

step 4:

   Connect to client socket means.

Step 5:

   Connect to server socket means, and you will use this code

above listed codings are some of the socket client and server connection code.

Then i listed some of the code

Step 6:

server side program

from socket import *

import sys

import select address = ('localhost', 8080)

server_socket = socket(AF_INET, SOCK_DGRAM)

server_socket.bind(address)

while(1):

print "Listening"

recv_data, addr = server_socket.recvfrom(2048)

print recv_data if recv_data == "Request 1" :

print "Received request 1"

server_socket.sendto("Response 1", address)

elif recv_data == "Request 2" :

print "Received request 2"

data = "Response 2"

server_socket.sendto(data, address)

step 7:

client side socket programming.

This is full socket server and client Programming

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