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..d8a1435 --- /dev/null +++ b/Simple-Java-Calculator.iml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/simplejavacalculator/UI.java b/src/simplejavacalculator/UI.java index 087bfb1..31c1a2b 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,14 @@ public void actionPerformed(ActionEvent e) { if (source == butBinary) 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(); text.selectAll(); } @@ -336,4 +346,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..1454790 --- 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 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)); + } +}