Write a program in Java reads user input to perform specific basic calculation.
ID: 3570528 • Letter: W
Question
Write a program in Java reads user input to perform specific basic calculation. Each calculation model is determined by type of the command (power, sum, multiply, difference, and factorial). The execution of any calculation should be done by a thread. Once the thread finishes its calculation, it will print the result for the user directly.
The following output should run exactly as follow:
msg > sum(8,2)
starting a new thread {1} to process sum(8,2)...
the result of thread {1} is 10
thread {1} finished...
msg > difference(5,8)
starting a new thread {2} to process difference(5,8)...
the result of thread {2} is 3
thread {2} finished...
msg > exit
Explanation / Answer
import java.util.Scanner;
public class SumInThread {
public static void main(String[] args) throws InterruptedException{
Thread operationThread;
Scanner in = new Scanner(System.in);
String command = ""; // arbitrary value to initialize
while(!command.equalsIgnoreCase("EXIT")){
if(command.equalsIgnoreCase("EXIT")){
System.exit(10);
}
System.out.print("msg> ");
command = in.next();
operationThread = new Thread(new Operator(command));
System.out.println("starting a new thread "+operationThread.getName()+" to process "+command);
operationThread.start();
operationThread.join();
}
in.close();
}
}
class Operator implements Runnable{
String command;
public Operator(String command) {
this.command = command;
}
@Override
public void run() {
String key = command.substring(0,command.indexOf("(")).toLowerCase();
String[] numbers = command.substring(command.indexOf("(")+1, command.lastIndexOf(")")).split(",");
int a = Integer.parseInt(numbers[0]);
int b = 0;
if(!key.equals("factorial")){
b = Integer.parseInt(numbers[1]);
}
int result = 0;
switch (key) {
case "power":
result = (int) Math.pow(a, b);
break;
case "sum":
result = a+b;
break;
case "multiply":
result = a*b;
break;
case "difference":
result = a-b;
break;
case "factorial":
result = 1;
for(int i=1; i<=a; i++){
result*=i;
}
break;
default:
System.out.println("Invalid command");
break;
}
System.out.println("the result of"+Thread.currentThread().getName()+" is "+result);
System.out.println(Thread.currentThread().getName()+" finished ");
}
}
=================output=================
msg> power(2,3)
starting a new thread Thread-0 to process power(2,3)
the result ofThread-0 is 8
Thread-0 finished
msg> sum(2,10)
starting a new thread Thread-1 to process sum(2,10)
the result ofThread-1 is 12
Thread-1 finished
msg> difference(9,5)
starting a new thread Thread-2 to process difference(9,5)
the result ofThread-2 is 4
Thread-2 finished
msg> multiply(5,6)
starting a new thread Thread-3 to process multiply(5,6)
the result ofThread-3 is 30
Thread-3 finished
msg> factorial(4)
starting a new thread Thread-4 to process factorial(4)
the result ofThread-4 is 24
Thread-4 finished
msg> exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.