How to solve below coding exercise

The comments say you “should” do a print, but nothing says you cannot print anything else after the print. Nothing even forces you to do a print, otherwise it would be a shall.


To be honest, reading questions about homework like this one makes me unhappy.

To me, the whole thing is useless, ugly, and does not teach anything valuable for future real-world software design and coding. The one thing it teaches is: given poor specifications, do poor work to please the client. Well … that is maybe a good lesson to learn ?

Since you did not give any piece of code of what you tried, nor any hint about what you understood, I took the liberty to propose some kind of a solution that does the required prints.

So here are 3 implementations. I tried to do this in a rough way. Maybe others would find other weird ways to do it.

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final String REQUEST_MUST_NOT_BE_EMPTY = "request must not be empty";
    private static final String REQUEST_IS_REQUIRED = "request is required";
    private static final String REQUEST = "request";
    private static final String EMPTY_STRING = "";
    private static final Map<String, Object> requestMessageMap = new HashMap<String, Object>();
    static {
        // requestMessageMap.put(REQUEST, Integer.valueOf(7));
        // requestMessageMap.put(REQUEST, Integer.valueOf(REQUEST.length()));
        requestMessageMap.put(EMPTY_STRING, REQUEST_MUST_NOT_BE_EMPTY);
        requestMessageMap.put(null, REQUEST_IS_REQUIRED);
    }

    public static void main(String[] args) {
        // Should print 7
        System.out.println(stringLength("request")); // this line cannot be changed

        // Should print "request must not be empty"
        System.out.println(stringLength("")); // this line cannot be changed

        // Should print "request is required"
        System.out.println(stringLength(null)); // this line cannot be changed
    }

    public static Integer stringLength(String request) // this line cannot be changed
    {
        return sillyMethod4(request);
    }

    private static Integer sillyMethod1(String request) {
        Integer returnValue = -1;
        if (request == null) {
            // do exactly what specification required
            // (completly pointeless)
            System.err.println(REQUEST_IS_REQUIRED);
        } else if (request.equals(EMPTY_STRING)) {
            // do exactly what specification required
            // (completly pointeless)
            System.err.println(REQUEST_MUST_NOT_BE_EMPTY);
        } else if (request.equals(REQUEST)) {
            // do exactly what specification required
            // (completly pointeless)
            returnValue = 7;
        } else {
            // my best guess about what we should really do
            returnValue = request.length();
        }
        return returnValue;
    }

    private static Integer lessSillyMethod2(String request) {
        Integer returnValue = -1;
        if (request == null) {
            // do exactly what specification required
            // (completly pointeless)
            System.err.println(REQUEST_IS_REQUIRED);
        } else if (request.equals(EMPTY_STRING)) {
            // do exactly what specification required
            // (completly pointeless)
            System.err.println(REQUEST_MUST_NOT_BE_EMPTY);
        } else {
            // my best guess about what we should really do
            returnValue = request.length();
        }
        return returnValue;
    }

    private static Integer sillyMethod3(String request) {
        Integer returnValue = -1;
        if (request == null) {
            // do exactly what specification required
            // (completly pointeless)
            System.err.println("request is required: ");
        } else {
            switch (request) {
            case EMPTY_STRING:
                // do exactly what specification required
                // (completly pointeless)
                System.err.println("request must not be empty: ");
                break;
            case REQUEST:
                // do exactly what specification required
                // (completly pointeless)
                returnValue = 7; //
                break;
            default:
                // my best guess about what we should really do
                returnValue = request.length();
                break;
            }
        }
        return returnValue;
    }

    private static Integer sillyMethod4(String request) {
        Integer returnValue = -1;
        if (requestMessageMap.containsKey(request)) {
            System.err.println(requestMessageMap.get(request));
        } else {
            returnValue = request.length();
        }
        return returnValue;
    }
}

Output:

7
request must not be empty
request is required
-1
-1

Leave a Comment