-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleCalc.java
More file actions
133 lines (98 loc) · 3.6 KB
/
simpleCalc.java
File metadata and controls
133 lines (98 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.bham.pij.assignments.calculator;
//Ahmed Khan - Student ID: 2255610
import java.util.ArrayList;
public class Calculator {
public float finalResult = 0;
public ArrayList<Float> ram = new ArrayList<Float>();
public float evaluate(String expression) {
// 1. Receive a string as an input
// 2. Return the output as a float
// This splits the input string to a list with all items seperated
ArrayList<String> token = new ArrayList<String>();
for (String retval : expression.split(" ")) {
token.add(retval);
}
float result = 0;
float item1 = Float.valueOf(token.get(0));
float item2 = Float.valueOf(token.get(2));
// This block iterates over all items in token
for(String item: token){
if (token.get(1).equals("+")){
result = item1 + item2;
}
if (token.get(1).equals("-")){
result = item1 - item2;
}
if (token.get(1).equals("/")){
result = item1 / item2;
}
if (token.get(1).equals("*")){
result = item1 * item2;
}
}
//System.out.println(result);
finalResult = result;
ram.add(result);
return result;
}
public float getCurrentValue() {
/*
* 1. This method returns the evaluated expression from evaluate()
*/
return finalResult;
}
public float getMemoryValue() {
/*
* If the user types “mr”, the program should print to the console the stored
* memory value (or zero if no value has been stored).
*/
if (!ram.isEmpty()){
System.out.println( ram.get(ram.size()-1));
}
else{
System.out.println(0);
}
return 0;
}
public void setMemoryValue(float memval) {
/*
* The memory function works as follows. If the user presses presses the ‘m’ key
* the program should store the most recent calculator result (if it was valid,
* zero otherwise).
*/
//append finalResult to the Arraylist ram
ram.add(finalResult);
return ;
}
public void clearMemory() {
/*
* If the user presses the ‘c’ key, the program should clear the memory by
* setting it to zero.
*/
//delete all items in the memory arraylist
ram.clear();
return;
}
public float getHistoryValue(int index) {
/*
* The calculator should also have a history function which works as follows. If
* the user presses ‘h’ then the program should print to the console all of the
* results since the program was started, on one line, separated by spaces. You
* should also add the following method to your class. This method returns the
* history value at the index in the parameter. If the parameter is zero, the
* method should return the first value that was added to the history. If the
* parameter is 1, the method should return the second value that was added to
* the history, and so on.
*/
// return historic value in ram at index n
return ram.get(index-1);
}
public void run() {
/*
* This method acts as the main controller
*/
}
public static void main(String[] args) {
new Calculator().run();
}
}