Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new features and solve test fails #89

Closed
wants to merge 3 commits into from
Closed
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
71 changes: 61 additions & 10 deletions src/simplejavacalculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
public class Calculator {

public enum BiOperatorModes {
normal, add, minus, multiply, divide , xpowerofy
normal, add, minus, multiply, divide , xpowerofy, div, mod
}

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

private Double num1, num2;
Expand Down Expand Up @@ -52,6 +52,32 @@ private Double calculateBiImpl() {
if (mode == BiOperatorModes.xpowerofy) {
return pow(num1,num2);
}
if (mode == BiOperatorModes.div) {
if(num1 % 1==0 && num2 % 1==0){ //check if both numbers are integers
double temp1 = num1;
double temp2 = num2;
int numa = (int) temp1;
int numb = (int) temp2;
int result = numa/numb;
return 1.0*result;
}
else{
return NaN;
}
}
if (mode == BiOperatorModes.mod) {
if(num1 % 1==0 && num2 % 1==0){ //check if both numbers are integers
double temp1 = num1;
double temp2 = num2;
int numa = (int) temp1;
int numb = (int) temp2;
int result = numa % numb;
return 1.0*result;
}
else{
return NaN;
}
}

// never reach
throw new Error();
Expand Down Expand Up @@ -94,20 +120,27 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
return 1 / num;
}
if (newMode == MonoOperatorModes.cos) {
return Math.cos(Math.toRadians(num));
double wantedCos;
wantedCos = Math.cos(num);
wantedCos= Math.round(wantedCos*10000000000.0)/10000000000.0; //round to 10th decimal place
return wantedCos;
}
if (newMode == MonoOperatorModes.sin) {
return Math.sin(Math.toRadians(num));
double wantedSin;
wantedSin = Math.sin(num);
wantedSin= Math.round(wantedSin*10000000000.0)/10000000000.0;
return wantedSin;
}
if (newMode == MonoOperatorModes.tan) {
if (num == 0 || num % 180 == 0) {
return 0.0;
}
if (num % 90 == 0 && num % 180 != 0) {
double cos = Math.cos(num);
cos = Math.round(cos*10000000000.0)/10000000000.0;
if(cos==0.0){
return NaN;
}

return Math.tan(Math.toRadians(num));
double wantedTan;
wantedTan = Math.tan(num);
wantedTan = Math.round(wantedTan*10000000000.0)/10000000000.0;
return wantedTan;
}
if (newMode == MonoOperatorModes.log) {
return log10(num);
Expand All @@ -118,6 +151,24 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
if (newMode == MonoOperatorModes.abs){
return Math.abs(num);
}
if(newMode == MonoOperatorModes.xfact){
if(num % 1==0 && num>=0 && num<=20){ //check if num is integer and 0<=num<=20. For numbers>= 20! long int is not enough to calculate
if(num==1 || num==0){
return 1.0;
}
long result =1;
for(int i=2;i<=num;i++){
result = result*i;
}
double doubleResult = result*1.0;
return doubleResult;
}
else{
return NaN;
}


}

// never reach
throw new Error();
Expand Down
44 changes: 37 additions & 7 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, butdiv, butmod, butlog, butrate, butabs, butBinary, butPI, butxfact;
private final Calculator calc;

private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6",
Expand Down Expand Up @@ -88,7 +88,8 @@ public UI() throws IOException {
but = new JButton[10];
for (int i = 0; i < 10; i++) {
but[i] = new JButton(String.valueOf(i));
}
}
butPI = new JButton("π");
butAdd = new JButton("+");
butMinus = new JButton("-");
butMultiply = new JButton("*");
Expand All @@ -100,7 +101,10 @@ public UI() throws IOException {
butCos = new JButton("Cos");
butSin = new JButton("Sin");
butTan = new JButton("Tan");
butxpowerofy = new JButton("x^y");
butxpowerofy = new JButton("x^y");
butxfact = new JButton("x!");
butdiv = new JButton("div");
butmod = new JButton("mod");
butlog = new JButton("log10(x)");
butrate = new JButton("x%");
butabs = new JButton("abs(x)");
Expand All @@ -123,7 +127,8 @@ public void init() {

for (int i = 0; i < 10; i++) {
but[i].setFont(font);
}
}
butPI.setFont(font);
butAdd.setFont(font);
butMinus.setFont(font);
butMultiply.setFont(font);
Expand All @@ -136,6 +141,9 @@ public void init() {
butSin.setFont(font);
butTan.setFont(font);
butxpowerofy.setFont(font);
butxfact.setFont(font);
butdiv.setFont(font);
butmod.setFont(font);
butlog.setFont(font);
butrate.setFont(font);
butabs.setFont(font);
Expand All @@ -159,7 +167,7 @@ public void init() {
panelSub3.add(but[6]);
panelSub3.add(Box.createHorizontalStrut(15));
panelSub3.add(butMultiply);
panelSub3.add(butDivide);
panelSub3.add(butDivide);
panel.add(panelSub3);

panelSub4.add(but[7]);
Expand All @@ -172,18 +180,22 @@ public void init() {

panelSub5.add(Box.createHorizontalStrut(92));
panelSub5.add(but[0]);
panelSub5.add(butPI);
panelSub5.add(Box.createHorizontalStrut(210));
panel.add(panelSub5);

panelSub6.add(butSquare);
panelSub6.add(butSquareRoot);
panelSub6.add(butOneDividedBy);
panelSub6.add(butxpowerofy);
panelSub6.add(butxfact);
panel.add(panelSub6);

panelSub7.add(butCos);
panelSub7.add(butSin);
panelSub7.add(butTan);
panelSub7.add(butdiv);
panelSub7.add(butmod);
panel.add(panelSub7);

panelSub8.add(butlog);
Expand All @@ -194,18 +206,22 @@ public void init() {

for (int i = 0; i < 10; i++) {
but[i].addActionListener(this);
}
}
butPI.addActionListener(this);
butAdd.addActionListener(this);
butMinus.addActionListener(this);
butMultiply.addActionListener(this);
butDivide.addActionListener(this);
butdiv.addActionListener(this);
butmod.addActionListener(this);
butSquare.addActionListener(this);
butSquareRoot.addActionListener(this);
butOneDividedBy.addActionListener(this);
butCos.addActionListener(this);
butSin.addActionListener(this);
butTan.addActionListener(this);
butxpowerofy.addActionListener(this);
butxfact.addActionListener(this);
butlog.addActionListener(this);
butrate.addActionListener(this);
butabs.addActionListener(this);
Expand All @@ -222,7 +238,10 @@ public void init() {
public void actionPerformed(ActionEvent e) {
final Object source = e.getSource();
Double checkNum = null;

if(source == butPI){
text.replaceSelection("3.14159265358979323846");
return;
}
for (int i = 0; i < 10; i++) {
if (source == but[i]) {
text.replaceSelection(buttonValue[i]);
Expand Down Expand Up @@ -256,6 +275,14 @@ public void actionPerformed(ActionEvent e) {
writer(calc.calculateBi(Calculator.BiOperatorModes.divide, reader()));
text.replaceSelection(butDivide.getText());
}
if (source == butdiv) {
writer(calc.calculateBi(Calculator.BiOperatorModes.div, reader()));
text.replaceSelection(butdiv.getText());
}
if (source == butmod) {
writer(calc.calculateBi(Calculator.BiOperatorModes.mod, reader()));
text.replaceSelection(butmod.getText());
}

if (source == butxpowerofy) {
writer(calc.calculateBi(Calculator.BiOperatorModes.xpowerofy, reader()));
Expand All @@ -264,6 +291,9 @@ public void actionPerformed(ActionEvent e) {
if (source == butSquare) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.square, reader()));
}
if (source == butxfact){
writer(calc.calculateMono(Calculator.MonoOperatorModes.xfact, reader()));
}

if (source == butSquareRoot)
writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, reader()));
Expand Down
48 changes: 48 additions & 0 deletions src/simplejavacalculatorTest/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@ void calculateBiDivideTest() {
calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4);
Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0));
}

@Test
void calculateBiDivTest() {
Calculator calculator = new Calculator();
calculator.calculateBi(Calculator.BiOperatorModes.div, 5.0);
Assertions.assertEquals(2.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0));
calculator.calculateBi(Calculator.BiOperatorModes.div, 13.0);
Assertions.assertEquals(4.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0));
calculator.calculateBi(Calculator.BiOperatorModes.div, 18.0);
Assertions.assertEquals(4.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 4.0));
calculator.calculateBi(Calculator.BiOperatorModes.div, 5.1);
Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0));
calculator.calculateBi(Calculator.BiOperatorModes.div, 5.0);
Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.3));
}


@Test
void calculateModTest() {
Calculator calculator = new Calculator();
calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.0);
Assertions.assertEquals(1.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0));
calculator.calculateBi(Calculator.BiOperatorModes.mod, 14.0);
Assertions.assertEquals(2.0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0));
calculator.calculateBi(Calculator.BiOperatorModes.mod, 20.0);
Assertions.assertEquals(0, calculator.calculateBi(Calculator.BiOperatorModes.normal, 4.0));
calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.1);
Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0));
calculator.calculateBi(Calculator.BiOperatorModes.mod, 5.0);
Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.3));
}



@Test
void calculateEqualTest() {
Expand All @@ -66,6 +99,21 @@ void CalculateMonoSquareTest() {
Calculator calculator = new Calculator();
Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0));
}

@Test
void CalculateMonXFactTest() {
Calculator calculator = new Calculator();
Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 0.0));
Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 1.0));
Assertions.assertEquals(6.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 3.0));
Assertions.assertEquals(362880.0, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 9.0));
Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, -5.0));
Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 21.0));
Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 56.0));
Assertions.assertEquals(NaN, calculator.calculateMono(Calculator.MonoOperatorModes.xfact, 3.2));


}

@Test
void CalculateMonoSquareRootTest() {
Expand Down