Answered You can buy a ready-made answer or pick a professional tutor to order an original one.

QUESTION

Calculator Server and Client Java The goal: Make a client that sends simple arithmetic expressions, and a server that solves them and sends the result back to the client. Class design requirements:

Calculator Server and Client  Java 

The goal: Make a client that sends simple arithmetic expressions, and a server that solves them and sends the result back to the client. 

Class design requirements: 

Your program should contain at least the following classes • CalculatorClient • CalculatorServer Important note: This is an unusual project in that it does not require a Tester class! I will run your two classes myself, calculatorolient and calculatorserver, and test them. 

Each of your classes, CalculatorClient and CalculatorServer, should have main(String[] args) methods, allowing them to be run! Thus, they will never actually be instantiated; they will just run. 

Here are the requirements for each class: CalculatorServer: • Makes a ServerSocket that awaits a connection on a port of your choice. • When a connection is made, it sends a one-time welcome message to the connected socket. 

• Repeatedly awaits data from the connected socket in the form <double> <operator> <double>, where <double> is a string that can be parsed to a Java Double data type, and <operator> is a string that is either "+", "-", "r, or "*", representing addition, subtraction, division, or multiplication, respectively. • When data of this form is received, representing an arithmetic calculation to be made, actually does the calculation and sends the result back to the connecting socket. 

CalculatorClient: 

• Makes a Socket that connects to some port of your choice on the host "localhost". This port should match that of the server! 

• Repeatedly asks the user for input, and sends that input over the socket connection to the server. 

• When a response is received (which should be the solution to the arithmetic problem), prints that to the console. 

Note: It's okay in this case for your server or client to crash if the client sends data in the wrong format (for example, if the client sent the string "5+5" with no spaces, or the string "foo"). You will not be marked down for this occurring. 

Show more
ANSWER

CalculatorClient.java

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.InetAddress;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;

public class CalculatorClient

{

public static void main(String[] args) throws IOException

{

    InetAddress ip = InetAddress.getLocalHost();

    int port = 13;

    Scanner sc = new Scanner(System.in);

     Socket s = new Socket(ip, port);

     DataInputStream Datainput = new DataInputStream(s.getInputStream());

    DataOutputStream Dataoutput = new DataOutputStream(s.getOutputStream());

         String welcome = Datainput.readUTF();

        System.out.println(  welcome);

    while (true)

    {

        System.out.println("Please enter a simple dyadic expression: ");

        String inp = sc.nextLine();

        if (inp.equals("Exit"))

            break;

        Dataoutput.writeUTF(inp);

        String ans = Datainput.readUTF();

        System.out.println("Result received:" + ans);

    }

}

}

CalculatorServer.java

    import java.io.DataInputStream;

    import java.io.DataOutputStream;

    import java.io.IOException;

    import java.net.ServerSocket;

    import java.net.Socket;

    import java.util.StringTokenizer;

    public class CalculatorServer

    {

    public static void main(String args[]) throws IOException

    {

        System.out.println("CalculatorServer ");

        ServerSocket ss = new ServerSocket(13);

        Socket s = ss.accept();

        DataInputStream Datainput = new DataInputStream(s.getInputStream());

        DataOutputStream Dataoutput = new DataOutputStream(s.getOutputStream());

       Dataoutput.writeUTF("Welcome to CalculatorServer 1.0!");

        while (true)

        {

            String input = Datainput.readUTF();

            if(input.equals("Exit"))

                break;

            double result;

             StringTokenizer st = new StringTokenizer(input);

            double oprnd1 = Double.parseDouble(st.nextToken());

            String operation = st.nextToken();

            double oprnd2 = Double.parseDouble(st.nextToken());

            if (operation.equals("+"))

            {

                result = oprnd1 + oprnd2;

            }

            else if (operation.equals("-"))

            {

                result = oprnd1 - oprnd2;

            }

            else if (operation.equals("*"))

            {

                result = oprnd1 * oprnd2;

            }

            else

            {

                result = oprnd1 / oprnd2;

            }

            Dataoutput.writeUTF(String.valueOf(result));

        }

    }

    }

LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question