Abdul Ahmad
2013-11-15 06:53:54 UTC
java MyCalculatorProgram 10 x 10
i get the result (100.00),
what my program does is see if the user input includes a number, followed by a math sign, followed by another number. and depending on the math sign (+, -, /, x) it will do the right math operation to give the right result...
so MyCalculatorProgram 10 / 10 would give 1 (this is all command line)
and MyCalculatorProgram 10 + 10 would give 20 etc...
however, I dont know how to make this happen using a scanner... Im really new to java so im still learning things on my own. one thing i want to avoid is having to pass arguments through code and compile the code everytime i want to do a math operation... anyways... heres a copy of the code if anyone is interested (if you have suggestions to make the code better i would be very thankful and more than happy to learn);
public enum MathFunctions1_0 {
MULTIPLY, DIVIDE, ADD, SUBTRACT
}
public class MathCalculatorTest2_0 {
MathFunctions1_0 mathFunction1;
public MathCalculatorTest2_0 (MathFunctions1_0 mathFunction1) {
this.mathFunction1 = mathFunction1;
}
public void DoMath(double digit1, double digit2) {
double mathResult = 0;
switch (mathFunction1) {
case MULTIPLY:
mathResult = digit1*digit2;
break;
case DIVIDE:
mathResult = digit1/digit2;
break;
case ADD:
mathResult = digit1+digit2;
break;
case SUBTRACT:
mathResult = digit1-digit2;
break;
default:
System.out.println("Incorrect arithmatic character, please insert a correct arithmatic character between the two numbers (x, /, +, -)");
}
System.out.println(mathResult);
}
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Please correct syntax");
} else {
double firstDigit = Double.parseDouble(args[0]);
String userInput1 = args[1];
double secondDigit = Double.parseDouble(args[2]);
MathCalculatorTest2_0 C;
switch (userInput1) {
case "x":
C = new MathCalculatorTest2_0(MathFunctions1_0.MULTIPLY);
C.DoMath(firstDigit, secondDigit);
break;
case "/":
C = new MathCalculatorTest2_0(MathFunctions1_0.DIVIDE);
C.DoMath(firstDigit, secondDigit);
break;
case "+":
C = new MathCalculatorTest2_0(MathFunctions1_0.ADD);
C.DoMath(firstDigit, secondDigit);
break;
case "-":
C = new MathCalculatorTest2_0(MathFunctions1_0.SUBTRACT);
C.DoMath(firstDigit, secondDigit);
break;
default: System.out.println("Please insert correct arithmatic character.");
}
}
}
}