Question

in java please withe comments & check output

Write a program that contains: Client Thread class

1 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
in java please withe comments & check output Write a program that contains: Client Thread class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

    PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...

  • Write two programs Client and Server

    Given two integer matrices A and B, you are requested to compose a program to perform matrix addition (A + B). Both matrices have N rows and M columns; N > 1, M > 1; You need to divide both (A and B) into four equal (or close to equal) size of submatrices (A0,0, A0,1, A1,0, A1,1 and B0,0, B0,1, B1.0, B1.1)andeachsubmatrixhasdimensioncloseto(N/2)x(M/2). YouneedtocreatefourJavathreads each thread performs a subset of addition on one pair of the submatrices. Example, thread 0 performs addition...

  • You are to write a Java program using the following instructions to build a bank-ATM server...

    You are to write a Java program using the following instructions to build a bank-ATM server system with sockets. A bank holds accounts that can have deposits, withdrawals, or retrievals of balance. The bank server provides the account for transactions when a client uses and ATM machine. The ATM asks for the type of transaction and the amount (if needed), then creates a socket connection to the bank to send the transaction for processing. The bank performs the transaction on...

  • Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input...

    Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input and create an object of the ScientificCalculator Class with those two numbers. 2. Then call the 8 operations one by one sequentially and display the result. Code must be written in Java. (Sample input/output: Enter number 1: 2 Enter number 2: 3 Add() result is : 5 Sub() result is: -1 ……. Power() result is: 8 …… sqrtNum1 result is:) The Problem: Given the...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public...

    JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public class ArrayIntList { private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } // post: constructs an empty list of default capacity...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT