Please I just need a simple program that will print out what is shown in the exa
ID: 3553845 • Letter: P
Question
Please I just need a simple program that will print out what is shown in the example. Please I need ASAP deadline is tonight! I have parts of it done but I need to see a complete one so I can better understand the process and where I went wrong. Thanks I will give a 5 star rating if it is complete and runs properly. I am only in computer science one so please nothing that is to complicated. Thanks.
In this program, you will write a command line calculator that can evaluate simple mathematical
expressions typed in by the user in floating point. In addition, your calculator will have a
memory that allows it to store or modify named variables. When the program starts, it should
print your name and state that it is a command line calculator. Then it should go into "command
mode", showing a prompt (such as %), and responding to inputs typed by the user. Example
usage is given as follows.
java MemCalc
Command line calculator with memory by <your name>
% a = 3 + 4
7.0
% bee = a * 3
21.0
% a + bee
28.0
% bee + 3.1
24.1
% a = 4.3
4.3
% 57
57.0
% c
c not found
% var
a: 4.0
bee: 21.0
% quit
The calculator must accept the following 5 input types of input from the user:
1. A command (quit, clear, var)
Result: The program executes the command
quit
Explanation / Answer
import java.util.Scanner;
import java.util.TreeMap;
public class MemCalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Command line calculator with memory by ");
TreeMap<String,String> map = new TreeMap<String,String>();
while(true){
System.out.print("%");
String var = input.nextLine();
if(var.equalsIgnoreCase("quit")){
System.out.println("Bye!");
System.exit(1);
} else if(var.equalsIgnoreCase("clear")){
map.clear();
} else if(var.equalsIgnoreCase("var")){
for(String s:map.keySet()){
System.out.println(s+"="+map.get(s));
}
} else {
String temp = var;
String left = temp.split("=")[0].trim();
String right = temp.split("=")[1].trim();
if(right.split(" ").length==3){
String i = right.split(" ")[0];
String j = right.split(" ")[2];
String operand = right.split(" ")[1];
double result = 0;
double i1,j1=0;
if(map.containsKey(i)){
i1 = Double.parseDouble(map.get(i));
} else {
i1 = Double.parseDouble(i);
}
if(map.containsKey(j)){
j1 = Double.parseDouble(map.get(j));
} else {
j1 = Double.parseDouble(j);
}
if(operand.equalsIgnoreCase("+")){
result = i1+j1;
}else if(operand.equalsIgnoreCase("-")){
result = i1-j1;
}else if(operand.equalsIgnoreCase("*")){
result = i1*j1;
}else if(operand.equalsIgnoreCase("/")){
result = i1/j1;
}
System.out.println(result);
map.put(left, Double.toString(result));
} else {
map.put(left, right);
}
}
}
}
}