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 cube in Calculator.java #78

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
# Mac OS System
.DS_Store*
._*
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Simple-Java-Calculator</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
5 changes: 4 additions & 1 deletion src/simplejavacalculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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, cube
}

private Double num1, num2;
Expand Down Expand Up @@ -87,6 +87,9 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
if (newMode == MonoOperatorModes.square) {
return num * num;
}
if (newMode == MonoOperatorModes.cube) {
return num * num * num;
}
if (newMode == MonoOperatorModes.squareRoot) {
return Math.sqrt(num);
}
Expand Down
14 changes: 11 additions & 3 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, butCube;
private final Calculator calc;

private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6",
Expand Down Expand Up @@ -98,14 +98,15 @@ public UI() throws IOException {
butSquare = new JButton("x*x");
butOneDividedBy = new JButton("1/x");
butCos = new JButton("Cos");
butSin = new JButton("Sin");
butSin = new JButton("Sin");
butTan = new JButton("Tan");
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");
butBinary = new JButton("Bin");
butCube = new JButton("x^3");

calc = new Calculator();

Expand All @@ -131,6 +132,7 @@ public void init() {
butEqual.setFont(font);
butSquareRoot.setFont(font);
butSquare.setFont(font);
butCube.setFont(font);
butOneDividedBy.setFont(font);
butCos.setFont(font);
butSin.setFont(font);
Expand Down Expand Up @@ -176,6 +178,7 @@ public void init() {
panel.add(panelSub5);

panelSub6.add(butSquare);
panelSub6.add(butCube);
panelSub6.add(butSquareRoot);
panelSub6.add(butOneDividedBy);
panelSub6.add(butxpowerofy);
Expand All @@ -200,6 +203,7 @@ public void init() {
butMultiply.addActionListener(this);
butDivide.addActionListener(this);
butSquare.addActionListener(this);
butCube.addActionListener(this);
butSquareRoot.addActionListener(this);
butOneDividedBy.addActionListener(this);
butCos.addActionListener(this);
Expand Down Expand Up @@ -264,6 +268,10 @@ public void actionPerformed(ActionEvent e) {
if (source == butSquare) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.square, reader()));
}

if (source == butCube) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.cube, reader()));
}

if (source == butSquareRoot)
writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot, reader()));
Expand Down
13 changes: 9 additions & 4 deletions src/simplejavacalculatorTest/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ void CalculateMonoSquareTest() {
Assertions.assertEquals(9.0, calculator.calculateMono(Calculator.MonoOperatorModes.square, 3.0));
}

@Test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding a test 💯

void CalculateMonoCubeTest() {
Calculator calculator = new Calculator();
Assertions.assertEquals(27.0, calculator.calculateMono(Calculator.MonoOperatorModes.cube, 3.0));
}

@Test
void CalculateMonoSquareRootTest() {
Calculator calculator = new Calculator();
Expand All @@ -82,19 +88,19 @@ void CalculateMonoOneDividedByTest() {
@Test
void CalculateMonoSinTest() {
Calculator calculator = new Calculator();
Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, java.lang.Math.PI / 6), 0.0000000001);
Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.sin, Math.toDegrees(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);
Assertions.assertEquals(0.5, calculator.calculateMono(Calculator.MonoOperatorModes.cos, Math.toDegrees(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);
Assertions.assertEquals(1.0, calculator.calculateMono(Calculator.MonoOperatorModes.tan, Math.toDegrees(java.lang.Math.PI) / 4), 0.0000000001);
}

@Test
Expand All @@ -115,5 +121,4 @@ void CalculateMonoAbsTest() {
Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, -3.0));
Assertions.assertEquals(3.0, calculator.calculateMono(Calculator.MonoOperatorModes.abs, 3.0));
}

}