in java please withe comments check output Write a program

in java please withe comments & check output

Write a program that contains: Client Thread class that send full operation (\"1+2\", \"1-2\", \"1*2\", \" 1/2\") to CalculaterServer Server Thread class named CalculaterServer, the server create thread to receives the request and decode the operation and call the appropriate method (add: sub, mul div) which are in class named Calculator to do the operation, and then sends back the result to client. Client show the following window: After choosing operation ask to insert 2 numbers to calculate.

Solution

Calculator.java

public class Calculator {
   static int add(int a,int b)
   {
       System.out.println(a+b);
       return a+b;
   }
   static int sub(int a,int b)
   {
       return a-b;
   }
   static int mul(int a,int b)
   {
       return a*b;
   }
   static int div(int a,int b)
   {
       return a/b;
   }
  
}

Calculatorclient.java

import java.util.Scanner;

public class CalculatorClient {
   Scanner sc;
   int operation;
   int a,b;
   public CalculatorClient() {
       sc=new Scanner(System.in);
   }
   void choose() throws InterruptedException
   {
       System.out.println(\"Welcome to Client Calendar\");
       System.out.println(\"Main Menu: choose operation to perform\");
       System.out.println(\"To addition 2 number: insert 1\");
       System.out.println(\"To substract 2 number: insert 2\");
       System.out.println(\"To multiply 2 number: insert 3\");
       System.out.println(\"To divide 2 number: insert 4\");
       System.out.println(\"Exit: insert 5\");
       operation=sc.nextInt();
       System.out.println(\"Enter the 1st number\");
       a=sc.nextInt();
       System.out.println(\"Enter the 2nd number\");
       b=sc.nextInt();
       String msg=null;
       switch(operation)
       {
           case 1:
               msg=a+\"+\"+b;
               break;
           case 2:
               msg=a+\"-\"+b;
               break;
           case 3:
               msg=a+\"*\"+b;
               break;
           case 4:
               msg=a+\"/\"+b;
               break;      
       }
       if(msg!=null)
       {
           CalculatorServer cs1=new CalculatorServer(msg);
           CalculatorServer cs2= new CalculatorServer(2+\"*\"+3);
           Thread servert1=new Thread(cs1);
           servert1.start();
           Thread servert2=new Thread(cs2);
           servert2.start();
           Thread.sleep(2000);
           System.out.println(\"Result at client site\"+cs1.getResult());
           System.out.println(\"Result at client site\"+cs2.getResult());
          
       }
       sc.close();
   }
}

Calculatorserver.java

public class CalculatorServer implements Runnable {
   String msg;
   int result;
   public CalculatorServer(String message) {
       // TODO Auto-generated constructor stub
       msg=message;
   }
   @Override
   public void run() {
       String value[]=msg.split(\"\");
       int a=Integer.parseInt(value[0]);
       int b=Integer.parseInt(value[2]);
       //System.out.println(value[1]);
       if(value[1].equals(\"+\"))
       {
           result=Calculator.add(a, b);
       }
       else if(value[1].equals(\"-\"))
       {
           result=Calculator.sub(a, b);
       }
       else if(value[1].equals(\"*\"))
       {
           result=Calculator.mul(a, b);
       }
       else if(value[1].equals(\"/\"))
       {
           result=Calculator.div(a, b);
       }
       System.out.println(\"Result at server site\"+result);
       setresult(result);
      
   }
   void setresult(int re)
   {
       result=re;
   }
   int getResult()
   {
       return result;
   }
}

Assignment.java

public class Assignment {

   public static void main(String[] args) throws InterruptedException {
       CalculatorClient cc=new CalculatorClient();
       cc.choose();
   }

}

Output:

Welcome to Client Calendar
Main Menu: choose operation to perform
To addition 2 number: insert 1
To substract 2 number: insert 2
To multiply 2 number: insert 3
To divide 2 number: insert 4
Exit: insert 5
2
Enter the 1st number
8
Enter the 2nd number
5
Result at server site3
Result at server site6
Result at client site3
Result at client site6

in java please withe comments & check output Write a program that contains: Client Thread class that send full operation (\
in java please withe comments & check output Write a program that contains: Client Thread class that send full operation (\
in java please withe comments & check output Write a program that contains: Client Thread class that send full operation (\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site