Created
October 31, 2012 09:32
-
-
Save simonharrer/3986086 to your computer and use it in GitHub Desktop.
Simple Calculator as a Java Webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jws.WebService; | |
import javax.xml.ws.Endpoint; | |
@WebService | |
public class Calculator { | |
public static void main(String[] args) { | |
Calculator.calculate(1, 1, "ADD"); | |
Calculator.calculate(2, 3, "MULT"); | |
Calculator.calculate(5, 2, "asdf"); | |
Endpoint.publish("http://localhost:12345/calc", new Calculator()); | |
} | |
public int compute(int x, int y, String operation) { | |
return Calculator.calculate(x, y, operation); | |
} | |
public static int calculate(int x, int y, String operation) { | |
int result; | |
String op; | |
if ("ADD".equals(operation)) { | |
result = x + y; | |
op = "+"; | |
} else if ("SUB".equals(operation)) { | |
result = x - y; | |
op = "-"; | |
} else if ("MULT".equals(operation)) { | |
result = x * y; | |
op = "*"; | |
} else if ("DIV".equals(operation)) { | |
result = x / y; | |
op = "/"; | |
} else { | |
// defaults to SUB | |
result = x - y; | |
op = "-"; | |
} | |
log(x, y, result, op); | |
return result; | |
} | |
private static void log(int x, int y, int result, String op) { | |
System.out.format("%d %s %d = %d%n", x, op, y, result); | |
} | |
} |
Should do. But I don't know for sure as I've written this code 7 years ago.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Does it work on Java EE?