Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a calculator than can execute three operations:
- * - "add" : to sum up a list of integer numbers
- * - "count" : to count the amount of substrings in a larger string
- * - "weekday" : to specify the week day of a given date, where 1=Sunday, 2=Monday, ...
- *
- * The input for the calculator is given as a JSON string that needs to be parsed. Each string only
- * contains one command. The output for each calculation is just a long number of the result.
- *
- * Examples for the three operations in JSON are:
- * - {"operator":"add", "operands":[5,8,-3]}" to add the 5+8+(-3). Result: 10
- * - {"operator":"count", "text":"abbaabbababb", "searchToken":"bb"} to count the "bb" is the string. Result: 3
- * - {"operator":"weekday", "date":"1976-03-12"} to extract Friday, thus Result: 6
- *
- * Hint:
- * - You may use any of the installed libraries: JSON-Simple, JUnit 4 and Apache Commons Lang3.
- * - If needed, you are allowed to use the internet to search for documentations and code snippets.
- * - You can run your code as often as you like by pressing "Run" in the lower left corner of this screen.
- * - When everything is correct, the result 10 / 3 / 6 should appear on the console.
- * - Try to use clean coding techniques as much as possible to create a good readable source code.
- *
- * Last remark: All actions you perform in this screen are recorded for a later review by CAS.
- * Please send an email back to CAS as soon as you have finished.
- */
- import java.io.*;
- import org.json.simple.*;
- import org.json.simple.parser.*;
- // TODO: add additional imports if needed
- class Calculator {
- public static void main (String[] args) throws Exception {
- String[] expressions = new String[3];
- expressions[0] = "{\"operator\":\"add\",\"operands\":[5,8,-3]}";
- expressions[1] = "{\"operator\":\"count\",\"text\":\"abbaabbababb\",\"searchToken\":\"bb\"}";
- expressions[2] = "{\"operator\":\"weekday\",\"date\":\"1976-03-12\"}";
- for (int i = 0; i < expressions.length; i++) {
- long result = execute(expressions[i]);
- System.out.println(result);
- }
- }
- private static long execute(String jsonExpression) throws Exception {
- // TODO: Put in your code here.
- return 0;
- }
- // TODO: Add additional helper methods here if needed.
- }
Advertisement
Add Comment
Please, Sign In to add comment