Okay so I am building a simple PostFix Calculator in Java and I was asked to cre
ID: 3571188 • Letter: O
Question
Okay so I am building a simple PostFix Calculator in Java and I was asked to create a couple functions for it, one that I am struggling with is the memory. I am required to use something called HashMap, but i don't quite understand how to implement it into my program correctly. The way the Program will work is the User will Start it and it will say its a postFix calculator and will be prompted for input like this:
1 import java. util 2 import java.io.*; 3 public class Programs ix public static void main (String args System. out.println Servando Hernandez System. out.println RPN command line calculator Scanner Scan new Scanner (System.in); System.out.print 10 while scan. hasNext Line 11 12 system. out.print 13 ext Line String a Scan, n 14 String b quit 15 String c 3 "mem" 16 String d clear 17 if(a. equals (b)) 18 19 System.exit(0); 20 21 else 22 23 System. out.println (compute (a)); 25 System. out.print 26 27 28 29 30 31 public static String compute (String input) 32 33 ListExplanation / Answer
Tested on Eclipse
/********************ProgramSix.java******************/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class ProgramSix {
/**
* memory map for storing variable value
*/
static Map<String, Integer> memory = new HashMap<String, Integer>();
// main method start
public static void main(String args[]) {
System.out.println("Servando Hernandez");
System.out.println("RPN command Line calculator");
Scanner scan = new Scanner(System.in);
System.out.println(">");
/**
* It will continue until we type exit
*/
while (scan.hasNextLine()) {
System.out.println("> ");
String a = scan.nextLine();
String b = "exit";
String c = "mem";
String d = "clear";
if (a.equals(b)) {
System.exit(1);
} /**
* if we typed mem then it will print all variable with values
* present in Map
*
*/
else if (a.equals(c)) {
// loop a Map
for (Map.Entry<String, Integer> entry : memory.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} else {
System.out.println(compute(a));
}
System.out.println(">");
}
}
/**
* compute method implementation
*/
private static String compute(String input) {
List<String> processedList = new ArrayList<String>();
boolean isequal = false;
/**
* checking input string contains = or not
*/
if (input.contains("=")) {
isequal = true;
}
/**
* creating token from input string
*/
if (!input.isEmpty()) {
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
processedList.add(st.nextToken());
}
} else {
return "Error";
}
Stack<String> tempList = new Stack<String>();
Iterator<String> iter = processedList.iterator();
String key = "";
boolean flag = false;
/**
* Iterating all tokens
*/
while (iter.hasNext()) {
String temp = iter.next();
if (temp.matches("[0-9]*")) {
tempList.push(temp);
} else if (temp.matches("[*-/+%]")) {
if (temp.equals("*")) {
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls * rs;
tempList.push("" + result);
} else if (temp.equals("-")) {
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls - rs;
tempList.push("" + result);
} else if (temp.equals("/")) {
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls / rs;
tempList.push("" + result);
} else if (temp.equals("+")) {
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls + rs;
tempList.push("" + result);
} else if (temp.equals("%")) {
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls % rs;
tempList.push("" + result);
}
} else if (temp.matches("[a-zA-z]*")) {
if (!flag) {
key = temp;
}
if (memory.get(temp) == null && isequal == false) {
System.out.println(temp + " not found");
return "";
} else if (memory.containsKey(temp)) {
tempList.push(memory.get(temp) + "");
}
} else if (temp.equals("=")) {
flag = true;
} else {
return "Error";
}
}
String value = tempList.pop();
if (isequal) {
memory.put(key, Integer.parseInt(value));
}
return value;
}
}
/*********************output**************************/
Servando Hernandez
RPN command Line calculator
>
a = 3 5 + 1 -
>
7
>
bee = a 3 *
>
21
>
a bee +
>
28
>
bee 3 %
>
0
>
a = 4
>
4
>
57
>
57
>
2 c +
>
c not found
>
mem
>
a: 4
bee: 21
>
exit
>
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.