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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions devops_calculator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<conelement value="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="0" />
<src_folder value="file://$MODULE_DIR$/src/test/java" expected_position="1" />
</src_description>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/${project.build.directory}/classes" />
<output-test url="file://$MODULE_DIR$/${project.build.directory}/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/classes" />
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/test-classes" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="library" name="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" level="application" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit5.7.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
29 changes: 29 additions & 0 deletions src/main/java/calculator/Addition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package calculator;

import java.text.DecimalFormat;

/**
* The <code>Addition</code> class represents the operation of addition.
*/
public class Addition extends BinaryOperation {

public Addition() {
this.name = "Addition";
this.operator = "+";
}

public Addition(double a, double b) {
this();
this.firstOperand = a;
this.secondOperand = b;
}

public double perform() {
return this.firstOperand + this.secondOperand;
}

public String showExpression() {
return simplifyNumber(this.firstOperand) + " + " + (this.secondOperand < 0? "(": "") + simplifyNumber(this.secondOperand) + (this.secondOperand < 0? ")": "");
}

}
140 changes: 93 additions & 47 deletions src/main/java/calculator/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,103 @@
import java.util.*;
public class App
{
public static void main(String args[])
public static void main(String[] args)
{
int choice, flag=0;
double num1,num2;
Scanner scan = new Scanner(System.in);
Calculator calculator = new Calculator();

int choice;
boolean validInput;
double num1 = 0, num2 = 0;
System.out.println("Calculator");

do {
System.out.println("1 for Addition\n2 for Subtraction\n3 for Multiplication\n4 for Division\n");
System.out.print("Enter your choice: ");
choice = scan.nextInt();
if (choice > 4 || choice <1) {
flag = 1;
System.out.println("Invalid choice, exiting\n");
// Choose an action
choice = calculator.chooseAction();

if (choice == 0) {
System.out.println("Exiting, Bye");
return;
}
else if (flag != 1){
System.out.println("Enter two numbers");
System.out.print("Enter number 1: ");
num1 = scan.nextDouble();
System.out.print("Enter number 2: ");
num2 = scan.nextDouble();

switch(choice) {
case 1: System.out.println(num1+" + "+num2+" = "+Add(num1, num2));
break;
case 2: System.out.println(num1+" - "+num2+" = "+Subtract(num1, num2));
break;
case 3: System.out.println(num1+" * "+num2+" = "+Multiply(num1, num2));
break;
case 4: System.out.println(num1+" / "+num2+" = "+Divide(num1, num2));
break;
default: System.out.println("Exiting, Bye");
flag=1;

Operation chosenOperation = calculator.getOperations().get(choice - 1);
System.out.println(chosenOperation.getName());
Scanner scan = new Scanner(System.in).useLocale(Locale.US);

// Scan operand(s) of the chosen operation

// BINARY OPERATIONS (+, -, *, /)
if (chosenOperation instanceof BinaryOperation) {
System.out.println("Enter two numbers");

// first operand
validInput = false;
while (!validInput) {
try {
System.out.print("Enter number 1: ");
num1 = scan.nextDouble();
} catch (Exception e) {
System.out.println("Invalid number, please try again.");
scan.nextLine();
continue;
}
validInput = true;
}

// second operand
validInput = false;
while (!validInput) {
try {
System.out.print("Enter number 2: ");
num2 = scan.nextDouble();
} catch (Exception e) {
System.out.println("Invalid number, please try again.");
scan.nextLine();
continue;
}
validInput = true;
}
}
System.out.println("\n");
}while(flag==0);
}

static double Add(double a, double b) {
return a + b;
}
static double Subtract(double a, double b) {
return a - b;
}
static double Multiply(double a, double b) {
return a * b;
}
static double Divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Divisor cannot be zero, Exiting");
}
else return a / b;

// UNARY OPERATIONS (sin, cos, tan, sqrt, exp)
if (chosenOperation instanceof UnaryOperation) {
validInput = false;
while (!validInput) {
try {
System.out.print("Enter number" + ((chosenOperation instanceof TrigonometricOperation)? " (in radians)": "") + ": ");
num1 = scan.nextDouble();
} catch (Exception e) {
System.out.println("Invalid number, please try again.");
scan.nextLine();
continue;
}
validInput = true;
}
}

// Set operand(s) to the chosen operation,
// perform the operation and
// print the results.

double result;
chosenOperation = calculator.getOperations().get(choice - 1);
if (chosenOperation instanceof UnaryOperation) {
((UnaryOperation) chosenOperation).setOperand(num1);

result = chosenOperation.perform();

System.out.println(chosenOperation.showExpression() + " = " + chosenOperation.simplifyNumber(result));

} else if (chosenOperation instanceof BinaryOperation) {
((BinaryOperation) chosenOperation).setFirstOperand(num1);
((BinaryOperation) chosenOperation).setSecondOperand(num2);

result = chosenOperation.perform();

System.out.println(chosenOperation.showExpression() + " = " + chosenOperation.simplifyNumber(result));
}

} while (choice != 0);

}
}

}
26 changes: 26 additions & 0 deletions src/main/java/calculator/BinaryOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package calculator;

/**
* The <code>BinaryOperation</code> class represents a binary operation.
*/
abstract public class BinaryOperation extends Operation {
protected double firstOperand;
protected double secondOperand;

public double getFirstOperand() {
return firstOperand;
}

public void setFirstOperand(double firstOperand) {
this.firstOperand = firstOperand;
}

public double getSecondOperand() {
return secondOperand;
}

public void setSecondOperand(double secondOperand) {
this.secondOperand = secondOperand;
}

}
Loading