Help with simple enum problem
It receives two double numbers and an operation to be performed on these numbers. This operation could be PLUS, MINUS, TIMES or DIVIDES. (Define an “enum” with these operations.) The method should return a double with the result.
I dont know what to write for it to test properly in main, and im not sure if the code runs correctly. Does it?
Code Java:
public class Perform {
public enum Operation {
PLUS,
MINUS,
TIMES,
DIVIDES
}
private static Operation _operation;
double result;
double num1;
double num2;
public Perform(Operation _operation) {
this._operation = _operation;
}
public Operation getOperation() {
return _operation;
}
public void setOperation(Operation anOperation) {
this._operation = anOperation;
}
public static double theResult (double input1, double input2, Operation aOperation) {
double temp1 = input1;
double temp2 = input2;
_operation = aOperation;
switch(_operation) {
case PLUS: return temp1 + temp2;
case MINUS: return temp1 - temp2;
case TIMES: return temp1 * temp2;
case DIVIDES: return temp1/temp2;
default: return -1;
}
}
public static void main(String[] args) {
Perform p = new Perform(2.0, 2.0, PLUS);
System.out.println(" 2 + 2 = " + p.theResult( 2.0, 2.0 ) );
}
}
Re: Help with simple enum problem
Does that output what you expect? Maybe you should use the java.util.Random class and use that to test, see if you get some obviously incorrect results, if not, your code probably runs fine.
Re: Help with simple enum problem
It doesnt run. I was just taking a shot in the dark at testing it with those values.. the program doesnt actually compile with those lines in the main class. Im unsure what you write to test it though..
Re: Help with simple enum problem
Quote:
the program doesnt actually compile
You should post the full text of the error messages.