From 278d1083e0001e7448f942d21db9ff979b8e2492 Mon Sep 17 00:00:00 2001 From: Lampros Giotis Date: Fri, 9 Feb 2024 16:44:13 +0200 Subject: [PATCH 1/3] Added a new contrast button.It changes the colour of the background and the buttons.When pressed again it turn back to normal --- .idea/.gitignore | 3 + .idea/ant.xml | 6 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + Simple-Java-Calculator.iml | 11 + src/simplejavacalculator/UI.java | 204 +++++++++++++-- .../CalculatorTest.java | 238 +++++++++--------- 8 files changed, 344 insertions(+), 138 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/ant.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Simple-Java-Calculator.iml mode change 100755 => 100644 src/simplejavacalculatorTest/CalculatorTest.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/ant.xml b/.idea/ant.xml new file mode 100644 index 0000000..a2a4769 --- /dev/null +++ b/.idea/ant.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a818314 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..60716c0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Simple-Java-Calculator.iml b/Simple-Java-Calculator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Simple-Java-Calculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 087bfb1..4075680 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -18,7 +18,7 @@ package simplejavacalculator; -import java.awt.FlowLayout; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -26,12 +26,10 @@ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; -import java.awt.Font; import javax.swing.Box; import javax.swing.BoxLayout; -import java.awt.Image; -import javax.swing.ImageIcon; +import javax.swing.ImageIcon; import java.io.*; public class UI implements ActionListener { @@ -49,10 +47,11 @@ public class UI implements ActionListener { private final JPanel panelSub8; private final JTextArea text; + private boolean colourCounter; private final JButton but[], butAdd, butMinus, butMultiply, butDivide, - butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy, - butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butln; + butEqual, butCancel,butSquareRoot, butSquare, butOneDividedBy, + butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butln,butContrast; private final Calculator calc; private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6", @@ -66,7 +65,7 @@ public class UI implements ActionListener { public UI() throws IOException { frame = new JFrame("Calculator PH"); - + imageReturn = new BufferedImageCustom(); image = new ImageIcon(imageReturn.imageReturn()); @@ -86,9 +85,9 @@ public UI() throws IOException { text = new JTextArea(1, 30); textFont = new Font("Consolas",Font.BOLD, 24); - - but = new JButton[10]; - for (int i = 0; i < 10; i++) { + //colourCounter = false; + but = new JButton[11]; + for (int i = 0; i < 11; i++) { but[i] = new JButton(String.valueOf(i)); } butAdd = new JButton("+"); @@ -106,10 +105,10 @@ public UI() throws IOException { butxpowerofy = new JButton("x^y"); butlog = new JButton("log10(x)"); butrate = new JButton("x%"); - butabs = new JButton("abs(x)"); - butCancel = new JButton("C"); - butBinary = new JButton("Bin"); - + butabs = new JButton("abs(x)"); + butCancel = new JButton("C"); + butBinary = new JButton("Bin"); + butContrast = new JButton("Contrast"); calc = new Calculator(); } @@ -124,7 +123,7 @@ public void init() { text.setFont(textFont); text.setEditable(false); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 11; i++) { but[i].setFont(font); } butAdd.setFont(font); @@ -144,7 +143,8 @@ public void init() { butrate.setFont(font); butabs.setFont(font); butCancel.setFont(font); - butBinary.setFont(font); + butBinary.setFont(font); + butContrast.setFont(font); panel.add(Box.createHorizontalStrut(100)); panelSub1.add(text); @@ -176,7 +176,8 @@ public void init() { panelSub5.add(Box.createHorizontalStrut(92)); panelSub5.add(but[0]); - panelSub5.add(butln); + panelSub5.add(butln); + panelSub5.add(butContrast); panelSub5.add(Box.createHorizontalStrut(210)); panel.add(panelSub5); @@ -196,8 +197,9 @@ public void init() { panelSub8.add(butabs); panelSub8.add(butBinary); panel.add(panelSub8); + panel.setOpaque(true); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 11; i++) { but[i].addActionListener(this); } butAdd.addActionListener(this); @@ -219,6 +221,7 @@ public void init() { butEqual.addActionListener(this); butCancel.addActionListener(this); + butContrast.addActionListener(this); frame.add(panel); frame.setVisible(true); @@ -229,7 +232,7 @@ public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); Double checkNum = null; - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 11; i++) { if (source == but[i]) { text.replaceSelection(buttonValue[i]); return; @@ -307,7 +310,10 @@ public void actionPerformed(ActionEvent e) { if (source == butBinary) parsetoBinary(); + } + if(source == butContrast) + contrastButton(); text.selectAll(); } @@ -336,4 +342,164 @@ public void writer(final Double num) { text.setText(Double.toString(num)); } } + + /** + * @Author Lampros Giotis + * This method implements the contrast function.It switches the colours of the buttons as well as + * the panels to make it more contrast.When pressed again it switches back to normal + */ + public void contrastButton(){ + if(colourCounter == false){//The button has not been pressed.Therfor the starting value remains the same as the initiliazed + panel.setBackground(Color.black);////// + panelSub1.setBackground(Color.black);// + panelSub2.setBackground(Color.black);// + panelSub3.setBackground(Color.black);//////Adjusting the black colour to the panels first + panelSub4.setBackground(Color.black);// + panelSub5.setBackground(Color.black);// + panelSub6.setBackground(Color.black);// + panelSub7.setBackground(Color.black);// + panelSub8.setBackground(Color.black);// + + //We also change the contrast of the buttons. + for (int i = 0; i < 11; i++) { + but[i].setBackground(Color.white); + but[i].setForeground(Color.black); + } + //========================================================================== + butabs.setBackground(Color.white);//We change the colours on the butabs button + butabs.setForeground(Color.black); + //====================================================================== + butContrast.setBackground(Color.white);//We change the colours on the contrast button + butContrast.setForeground(Color.black); + //====================================================================== + butCancel.setBackground(Color.white);//We change the colours on the cancel button + butCancel.setForeground(Color.black); + //====================================================================== + butAdd.setBackground(Color.white);//We change the colours on the add button + butAdd.setForeground(Color.black); + //====================================================================== + butTan.setBackground(Color.white);//We change the colours on the tan button + butTan.setForeground(Color.black); + //====================================================================== + butDivide.setBackground(Color.white);//We change the colours on the divide button + butDivide.setForeground(Color.black); + //====================================================================== + butSin.setBackground(Color.white);//We change the colours on the sin button + butSin.setForeground(Color.black); + //====================================================================== + butSquare.setBackground(Color.white);//We change the colours on the square button + butSquare.setForeground(Color.black); + //====================================================================== + butSquareRoot.setBackground(Color.white);//We change the colours on the squareroot button + butSquareRoot.setForeground(Color.black); + //====================================================================== + butEqual.setBackground(Color.white);//We change the colours on the equal button + butEqual.setForeground(Color.black); + //======================================================================= + butMinus.setBackground(Color.white);//We change the colours on the minus button + butMinus.setForeground(Color.black); + //======================================================================= + butMultiply.setBackground(Color.white);//We change the colours on the multiply button + butMultiply.setForeground(Color.black); + //======================================================================= + butCos.setBackground(Color.white);//We change the colours on the cos button + butCos.setForeground(Color.black); + //======================================================================= + butxpowerofy.setBackground(Color.white);//We change the colours on the powerofy button + butxpowerofy.setForeground(Color.black); + //======================================================================= + butlog.setBackground(Color.white);//We change the colours on the log button + butlog.setForeground(Color.black); + //======================================================================= + butrate.setBackground(Color.white);//We change the colours on the rate button + butrate.setForeground(Color.black); + //======================================================================= + butln.setBackground(Color.white);//We change the colours on the ln button + butln.setForeground(Color.black); + //======================================================================= + butBinary.setBackground(Color.white);//We change the colours on the binary button + butBinary.setForeground(Color.black); + //Finally... + colourCounter = true;//We set the button to true as the action listener has been enabled and the colour is changed to black + }else if(colourCounter == true){//The button is pressed + panel.setBackground(Color.lightGray); + panelSub1.setBackground(Color.lightGray); + panelSub2.setBackground(Color.lightGray); + panelSub3.setBackground(Color.lightGray); + panelSub4.setBackground(Color.lightGray); + panelSub5.setBackground(Color.lightGray); + panelSub6.setBackground(Color.lightGray); + panelSub7.setBackground(Color.lightGray); + panelSub8.setBackground(Color.lightGray); + //We also change the contrast of the buttons. + for (int i = 0; i < 11; i++) { + but[i].setBackground(Color.white); + but[i].setForeground(Color.black); + } + //===================================================================== + butabs.setBackground(Color.white);//We change the colours on the butabs button + butabs.setForeground(Color.black); + //====================================================================== + butContrast.setBackground(Color.white);//We change the colours on the contrasts button + butContrast.setForeground(Color.black); + //====================================================================== + butCancel.setBackground(Color.white);//we change the colour on the cancel button + butCancel.setForeground(Color.black); + //====================================================================== + butAdd.setBackground(Color.white);//we change the colour on add button + butAdd.setForeground(Color.black); + //====================================================================== + butTan.setBackground(Color.white);//we change the colour of the tan button + butTan.setForeground(Color.black); + //====================================================================== + butDivide.setBackground(Color.white);//we change the clour of the division button + butDivide.setForeground(Color.black); + //====================================================================== + butSin.setBackground(Color.white);//we change the colour of the sin button + butSin.setForeground(Color.black); + //====================================================================== + butSquare.setBackground(Color.white);//we change the colour of the square button + butSquare.setForeground(Color.black); + //====================================================================== + butSquareRoot.setBackground(Color.white);//we change the colour the squareroot button + butSquareRoot.setForeground(Color.black); + //====================================================================== + butOneDividedBy.setBackground(Color.white);//we change the colour of the deivideby button + butOneDividedBy.setForeground(Color.black); + //====================================================================== + butEqual.setBackground(Color.white);//we change the colour of the equal button + butEqual.setForeground(Color.black); + //======================================================================= + butMinus.setBackground(Color.white);//we change the colour of the minus button + butMinus.setForeground(Color.black); + //======================================================================= + butMultiply.setBackground(Color.white);//we change the colour of the multiply button + butMultiply.setForeground(Color.black); + //======================================================================= + butCos.setBackground(Color.white);//we change the colour of the cos button + butCos.setForeground(Color.black); + //======================================================================= + butxpowerofy.setBackground(Color.white);//we change the colour of the powerofy button + butxpowerofy.setForeground(Color.black); + //======================================================================= + butlog.setBackground(Color.white);//we change the colour of the log button + butlog.setForeground(Color.black); + //======================================================================= + butrate.setBackground(Color.white);//we change the colour of the rate button + butrate.setForeground(Color.black); + //======================================================================= + butln.setBackground(Color.white);//we change the colour of the ln button + butln.setForeground(Color.black); + //======================================================================= + butCancel.setBackground(Color.white);//we change the colour of the cancel button + butCancel.setForeground(Color.black); + //======================================================================= + butBinary.setBackground(Color.white);//we change the colour of the binary button + butBinary.setForeground(Color.black); + colourCounter = false;//We change the value back to false in order to suppose + // in some way that the next time the button is pressed,it should change back to black colour. + + } + } + } diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java old mode 100755 new mode 100644 index 1ea20f7..f625893 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -1,119 +1,119 @@ -package simplejavacalculatorTest; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import static java.lang.Double.NaN; -import java.lang.Math; - - -import simplejavacalculator.Calculator; - -class CalculatorTest { - - @Test - void calculateBiNormalTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0); - Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); - } - - @Test - void calculateBiAddTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.add, 3.0); - Assertions.assertEquals(5.5, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.5)); - } - - @Test - void calculateBiMinusTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.minus, 3.1415); - Assertions.assertEquals(2.0415, calculator.calculateBi(Calculator.BiOperatorModes.normal, 1.1)); - } - - @Test - void calculateBiMultiplyTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.multiply, 3.2); - Assertions.assertEquals(6.4, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); - } - - @Test - void calculateBiDivideTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); - Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); - } - - @Test - void calculateEqualTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); - calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0); - Assertions.assertEquals(11.4, calculator.calculateEqual(3.0)); - } - - @Test - void resetTest() { - Calculator calculator = new Calculator(); - calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); - Assertions.assertEquals(8.4, calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0)); - Assertions.assertEquals(NaN, calculator.reset()); - } - - @Test - void CalculateMonoSquareTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); - } - - @Test - void CalculateMonoSquareRootTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(5.0, calculator.calculateMono(Calculator.MonoOperatorModes.squareRoot, 25.0)); - } - - @Test - void CalculateMonoOneDividedByTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0)); - } - - @Test - void CalculateMonoSinTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001); - } - - @Test - void CalculateMonoCosTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, java.lang.Math.PI / 3), 0.0000000001); - } - - @Test - void CalculateMonoTanTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); - } - - @Test - void CalculateMonoLogTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(2.0, calculator.calculateMono(Calculator.MonoOperatorModes.log, 100.0)); - } - - @Test - void CalculateMonoRateTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(.75, calculator.calculateMono(Calculator.MonoOperatorModes.rate, 75.0)); - } - - @Test - void CalculateMonoAbsTest() { - Calculator calculator = new Calculator(); - Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); - Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); - } - -} +//package simplejavacalculatorTest; +// +//import org.junit.jupiter.api.Assertions; +//import org.junit.jupiter.api.Test; +//import static java.lang.Double.NaN; +//import java.lang.Math; +// +// +//import simplejavacalculator.Calculator; +// +//class CalculatorTest { +// +// @Test +// void calculateBiNormalTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0); +// Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); +// } +// +// @Test +// void calculateBiAddTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.add, 3.0); +// Assertions.assertEquals(5.5, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.5)); +// } +// +// @Test +// void calculateBiMinusTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.minus, 3.1415); +// Assertions.assertEquals(2.0415, calculator.calculateBi(Calculator.BiOperatorModes.normal, 1.1)); +// } +// +// @Test +// void calculateBiMultiplyTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.multiply, 3.2); +// Assertions.assertEquals(6.4, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); +// } +// +// @Test +// void calculateBiDivideTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); +// Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); +// } +// +// @Test +// void calculateEqualTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); +// calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0); +// Assertions.assertEquals(11.4, calculator.calculateEqual(3.0)); +// } +// +// @Test +// void resetTest() { +// Calculator calculator = new Calculator(); +// calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); +// Assertions.assertEquals(8.4, calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0)); +// Assertions.assertEquals(NaN, calculator.reset()); +// } +// +// @Test +// void CalculateMonoSquareTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); +// } +// +// @Test +// void CalculateMonoSquareRootTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(5.0, calculator.calculateMono(Calculator.MonoOperatorModes.squareRoot, 25.0)); +// } +// +// @Test +// void CalculateMonoOneDividedByTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0)); +// } +// +// @Test +// void CalculateMonoSinTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001); +// } +// +// @Test +// void CalculateMonoCosTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, java.lang.Math.PI / 3), 0.0000000001); +// } +// +// @Test +// void CalculateMonoTanTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); +// } +// +// @Test +// void CalculateMonoLogTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(2.0, calculator.calculateMono(Calculator.MonoOperatorModes.log, 100.0)); +// } +// +// @Test +// void CalculateMonoRateTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(.75, calculator.calculateMono(Calculator.MonoOperatorModes.rate, 75.0)); +// } +// +// @Test +// void CalculateMonoAbsTest() { +// Calculator calculator = new Calculator(); +// Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); +// Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); +// } +// +//} From d9e045555ea4177e7c6f2dc30ce4924235c2f283 Mon Sep 17 00:00:00 2001 From: Lampros Giotis Date: Mon, 12 Feb 2024 19:08:48 +0200 Subject: [PATCH 2/3] I made one more commit because i hadn't installed any kind of JUNIT in my pc and i had to mark everything as a multiple lines comment in the Junit class.Also i added some additional documentation. --- Simple-Java-Calculator.iml | 26 ++ src/simplejavacalculator/UI.java | 4 + .../CalculatorTest.java | 242 +++++++++--------- 3 files changed, 153 insertions(+), 119 deletions(-) diff --git a/Simple-Java-Calculator.iml b/Simple-Java-Calculator.iml index c90834f..d8a1435 100644 --- a/Simple-Java-Calculator.iml +++ b/Simple-Java-Calculator.iml @@ -7,5 +7,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 4075680..31c1a2b 100644 --- a/src/simplejavacalculator/UI.java +++ b/src/simplejavacalculator/UI.java @@ -312,6 +312,10 @@ public void actionPerformed(ActionEvent e) { parsetoBinary(); } + /* + *We want the contrast button to be outside the IF condition,otherwise it will only work + * when the field is NOT empty. + */ if(source == butContrast) contrastButton(); diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index f625893..119b921 100644 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -1,119 +1,123 @@ -//package simplejavacalculatorTest; -// -//import org.junit.jupiter.api.Assertions; -//import org.junit.jupiter.api.Test; -//import static java.lang.Double.NaN; -//import java.lang.Math; -// -// -//import simplejavacalculator.Calculator; -// -//class CalculatorTest { -// -// @Test -// void calculateBiNormalTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0); -// Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); -// } -// -// @Test -// void calculateBiAddTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.add, 3.0); -// Assertions.assertEquals(5.5, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.5)); -// } -// -// @Test -// void calculateBiMinusTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.minus, 3.1415); -// Assertions.assertEquals(2.0415, calculator.calculateBi(Calculator.BiOperatorModes.normal, 1.1)); -// } -// -// @Test -// void calculateBiMultiplyTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.multiply, 3.2); -// Assertions.assertEquals(6.4, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); -// } -// -// @Test -// void calculateBiDivideTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); -// Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); -// } -// -// @Test -// void calculateEqualTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); -// calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0); -// Assertions.assertEquals(11.4, calculator.calculateEqual(3.0)); -// } -// -// @Test -// void resetTest() { -// Calculator calculator = new Calculator(); -// calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); -// Assertions.assertEquals(8.4, calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0)); -// Assertions.assertEquals(NaN, calculator.reset()); -// } -// -// @Test -// void CalculateMonoSquareTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); -// } -// -// @Test -// void CalculateMonoSquareRootTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(5.0, calculator.calculateMono(Calculator.MonoOperatorModes.squareRoot, 25.0)); -// } -// -// @Test -// void CalculateMonoOneDividedByTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0)); -// } -// -// @Test -// void CalculateMonoSinTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001); -// } -// -// @Test -// void CalculateMonoCosTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, java.lang.Math.PI / 3), 0.0000000001); -// } -// -// @Test -// void CalculateMonoTanTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); -// } -// -// @Test -// void CalculateMonoLogTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(2.0, calculator.calculateMono(Calculator.MonoOperatorModes.log, 100.0)); -// } -// -// @Test -// void CalculateMonoRateTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(.75, calculator.calculateMono(Calculator.MonoOperatorModes.rate, 75.0)); -// } -// -// @Test -// void CalculateMonoAbsTest() { -// Calculator calculator = new Calculator(); -// Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); -// Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); -// } -// -//} +package simplejavacalculatorTest; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import static java.lang.Double.NaN; + + +import java.lang.Math; + + +import simplejavacalculator.Calculator; + + +class CalculatorTest { + + + + @Test + void calculateBiNormalTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0); + Assertions.assertEquals(NaN, calculator.calculateBi(Calculator.BiOperatorModes.normal, 3.0)); + } + + @Test + void calculateBiAddTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.add, 3.0); + Assertions.assertEquals(5.5, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.5)); + } + + @Test + void calculateBiMinusTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.minus, 3.1415); + Assertions.assertEquals(2.0415, calculator.calculateBi(Calculator.BiOperatorModes.normal, 1.1)); + } + + @Test + void calculateBiMultiplyTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.multiply, 3.2); + Assertions.assertEquals(6.4, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + } + + @Test + void calculateBiDivideTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.divide, 6.4); + Assertions.assertEquals(3.2, calculator.calculateBi(Calculator.BiOperatorModes.normal, 2.0)); + } + + @Test + void calculateEqualTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); + calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0); + Assertions.assertEquals(11.4, calculator.calculateEqual(3.0)); + } + + @Test + void resetTest() { + Calculator calculator = new Calculator(); + calculator.calculateBi(Calculator.BiOperatorModes.add, 6.4); + Assertions.assertEquals(8.4, calculator.calculateBi(Calculator.BiOperatorModes.add, 2.0)); + Assertions.assertEquals(NaN, calculator.reset()); + } + + @Test + void CalculateMonoSquareTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0)); + } + + @Test + void CalculateMonoSquareRootTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(5.0, calculator.calculateMono(Calculator.MonoOperatorModes.squareRoot, 25.0)); + } + + @Test + void CalculateMonoOneDividedByTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(0.10, calculator.calculateMono(Calculator.MonoOperatorModes.oneDividedBy, 10.0)); + } + + @Test + void CalculateMonoSinTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001); + } + + @Test + void CalculateMonoCosTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, java.lang.Math.PI / 3), 0.0000000001); + } + + @Test + void CalculateMonoTanTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, java.lang.Math.PI / 4), 0.0000000001); + } + + @Test + void CalculateMonoLogTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(2.0, calculator.calculateMono(Calculator.MonoOperatorModes.log, 100.0)); + } + + @Test + void CalculateMonoRateTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(.75, calculator.calculateMono(Calculator.MonoOperatorModes.rate, 75.0)); + } + + @Test + void CalculateMonoAbsTest() { + Calculator calculator = new Calculator(); + Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0)); + Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0)); + } +} From c8072d4c3b90df76b021760fd02c86e706286860 Mon Sep 17 00:00:00 2001 From: Lampros Giotis Date: Mon, 12 Feb 2024 19:11:49 +0200 Subject: [PATCH 3/3] Small correction to the previous commit.Optimised just a little the code because i was having lots of commits warning everytime i was about to commit.No major change --- src/simplejavacalculatorTest/CalculatorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simplejavacalculatorTest/CalculatorTest.java b/src/simplejavacalculatorTest/CalculatorTest.java index 119b921..1454790 100644 --- a/src/simplejavacalculatorTest/CalculatorTest.java +++ b/src/simplejavacalculatorTest/CalculatorTest.java @@ -5,7 +5,7 @@ import static java.lang.Double.NaN; -import java.lang.Math; + import simplejavacalculator.Calculator;