Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/simplejavacalculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.lang.Math.log;
import static java.lang.Math.log10;
import static java.lang.Math.pow;
import static java.lang.Math.exp;

public class Calculator {

Expand All @@ -23,7 +24,7 @@ public enum BiOperatorModes {
}

public enum MonoOperatorModes {
square, squareRoot, oneDividedBy, cos, sin, tan ,log , rate, abs
square, squareRoot, oneDividedBy, cos, sin, tan ,log , rate, abs, fact, e
}

private Double num1, num2;
Expand Down Expand Up @@ -118,7 +119,15 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
if (newMode == MonoOperatorModes.abs){
return Math.abs(num);
}

if (newMode == MonoOperatorModes.fact) {
int fact = 1;
for(int i = 1;i <= num; i++) {
fact=fact*i;
}
}
if (newMode == MonoOperatorModes.e) {
return Math.exp(num);
}
// never reach
throw new Error();
}
Expand Down
19 changes: 17 additions & 2 deletions src/simplejavacalculator/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class UI implements ActionListener {
private final JTextArea text;
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy,
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary;
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butFact, butE;
private final Calculator calc;

private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6",
Expand Down Expand Up @@ -105,7 +105,9 @@ public UI() throws IOException {
butrate = new JButton("x%");
butabs = new JButton("abs(x)");
butCancel = new JButton("C");
butBinary = new JButton("Bin");
butBinary = new JButton("Bin");
butFact = new JButton("x!");
butE = new JButton("e^x");

calc = new Calculator();

Expand Down Expand Up @@ -141,6 +143,8 @@ public void init() {
butabs.setFont(font);
butCancel.setFont(font);
butBinary.setFont(font);
butFact.setFont(font);
butE.setFont(font);

panel.add(Box.createHorizontalStrut(100));
panelSub1.add(text);
Expand All @@ -166,8 +170,11 @@ public void init() {
panelSub4.add(but[8]);
panelSub4.add(but[9]);
panelSub4.add(Box.createHorizontalStrut(15));
panelSub4.add(butFact);
panelSub4.add(butE);
panelSub4.add(butEqual);
panelSub4.add(butCancel);

panel.add(panelSub4);

panelSub5.add(Box.createHorizontalStrut(92));
Expand Down Expand Up @@ -211,6 +218,8 @@ public void init() {
butabs.addActionListener(this);
butBinary.addActionListener(this);

butFact.addActionListener(this);
butE.addActionListener(this);
butEqual.addActionListener(this);
butCancel.addActionListener(this);

Expand Down Expand Up @@ -288,6 +297,12 @@ public void actionPerformed(ActionEvent e) {

if (source == butabs)
writer(calc.calculateMono(Calculator.MonoOperatorModes.abs, reader()));

if (source == butFact)
writer(calc.calculateMono(Calculator.MonoOperatorModes.fact, reader()));

if (source == butE)
writer(calc.calculateMono(Calculator.MonoOperatorModes.e, reader()));

if (source == butEqual)
writer(calc.calculateEqual(reader()));
Expand Down
16 changes: 16 additions & 0 deletions src/simplejavacalculatorTest/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,21 @@ void CalculateMonoAbsTest() {
Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0));
Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0));
}

@Test
void CalculateMonoFactTest() {
Calculator calculator = new Calculator();
Assertions.assertEquals(6, calculator.calculateMono(Calculator.MonoOperatorModes.fact, 3.0));
Assertions.assertEquals(120, calculator.calculateMono(Calculator.MonoOperatorModes.fact, 5.0));
}

@Test
void CalculateMonoFactE() {
Calculator calculator = new Calculator();
Assertions.assertEquals(1, calculator.calculateMono(Calculator.MonoOperatorModes.e, 0.0));
Assertions.assertEquals(54.59815003, calculator.calculateMono(Calculator.MonoOperatorModes.e, 4.0));
}



}