Skip to content

Commit c72e147

Browse files
committed
Changed the basic logic of cos sin tan calculation, Move the 0 button to the position under the 8 button, Changed the variable name oneDevidedBy to oneDividedBy. Done by Shieh Eik.
1 parent 58cfe1d commit c72e147

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
# Mac OS System
77
.DS_Store*
88
._*
9+
/bin/

src/simplejavacalculator/Calculator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum BiOperatorModes {
2323
}
2424

2525
public enum MonoOperatorModes {
26-
square, squareRoot, oneDevidedBy, cos, sin, tan ,log , rate, abs
26+
square, squareRoot, oneDividedBy, cos, sin, tan ,log , rate, abs
2727
}
2828

2929
private Double num1, num2;
@@ -90,14 +90,14 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
9090
if (newMode == MonoOperatorModes.squareRoot) {
9191
return Math.sqrt(num);
9292
}
93-
if (newMode == MonoOperatorModes.oneDevidedBy) {
93+
if (newMode == MonoOperatorModes.oneDividedBy) {
9494
return 1 / num;
9595
}
9696
if (newMode == MonoOperatorModes.cos) {
97-
return Math.cos(num);
97+
return Math.cos(Math.toRadians(num));
9898
}
9999
if (newMode == MonoOperatorModes.sin) {
100-
return Math.sin(num);
100+
return Math.sin(Math.toRadians(num));
101101
}
102102
if (newMode == MonoOperatorModes.tan) {
103103
if (num == 0 || num % 180 == 0) {
@@ -107,7 +107,7 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
107107
return NaN;
108108
}
109109

110-
return Math.tan(num);
110+
return Math.tan(Math.toRadians(num));
111111
}
112112
if (newMode == MonoOperatorModes.log) {
113113
return log10(num);

src/simplejavacalculator/UI.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class UI implements ActionListener {
5050

5151
private final JTextArea text;
5252
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
53-
butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy,
53+
butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy,
5454
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary;
5555
private final Calculator calc;
5656

@@ -87,7 +87,7 @@ public UI() throws IOException {
8787

8888
but = new JButton[10];
8989
for (int i = 0; i < 10; i++) {
90-
but[i] = new JButton(String.valueOf(i));
90+
but[i] = new JButton(String.valueOf(i));
9191
}
9292
butAdd = new JButton("+");
9393
butMinus = new JButton("-");
@@ -96,7 +96,7 @@ public UI() throws IOException {
9696
butEqual = new JButton("=");
9797
butSquareRoot = new JButton("sqrt");
9898
butSquare = new JButton("x*x");
99-
butOneDevidedBy = new JButton("1/x");
99+
butOneDividedBy = new JButton("1/x");
100100
butCos = new JButton("Cos");
101101
butSin = new JButton("Sin");
102102
butTan = new JButton("Tan");
@@ -133,7 +133,7 @@ public void init() {
133133
butEqual.setFont(font);
134134
butSquareRoot.setFont(font);
135135
butSquare.setFont(font);
136-
butOneDevidedBy.setFont(font);
136+
butOneDividedBy.setFont(font);
137137
butCos.setFont(font);
138138
butSin.setFont(font);
139139
butTan.setFont(font);
@@ -172,13 +172,14 @@ public void init() {
172172
panelSub4.add(butCancel);
173173
panel.add(panelSub4);
174174

175+
panelSub5.add(Box.createHorizontalStrut(92));
175176
panelSub5.add(but[0]);
176177
panelSub5.add(Box.createHorizontalStrut(210));
177178
panel.add(panelSub5);
178179

179180
panelSub6.add(butSquare);
180181
panelSub6.add(butSquareRoot);
181-
panelSub6.add(butOneDevidedBy);
182+
panelSub6.add(butOneDividedBy);
182183
panelSub6.add(butxpowerofy);
183184
panel.add(panelSub6);
184185

@@ -202,7 +203,7 @@ public void init() {
202203
butDivide.addActionListener(this);
203204
butSquare.addActionListener(this);
204205
butSquareRoot.addActionListener(this);
205-
butOneDevidedBy.addActionListener(this);
206+
butOneDividedBy.addActionListener(this);
206207
butCos.addActionListener(this);
207208
butSin.addActionListener(this);
208209
butTan.addActionListener(this);
@@ -261,9 +262,9 @@ public void actionPerformed(ActionEvent e) {
261262
reader()));
262263
}
263264

264-
if (source == butOneDevidedBy) {
265+
if (source == butOneDividedBy) {
265266
writer(calc.calculateMono(
266-
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
267+
Calculator.MonoOperatorModes.oneDividedBy, reader()));
267268
}
268269

269270
if (source == butCos) {

src/simplejavacalculatorTest/CalculatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void CalculateMonoSquareRootTest() {
7676
@Test
7777
void CalculateMonoOneDividedByTest() {
7878
Calculator calculator = new Calculator();
79-
Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDevidedBy, 10.0));
79+
Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0));
8080
}
8181

8282
@Test

0 commit comments

Comments
 (0)