Old answer. The reason I am making this new answer is because I was using Boolean's compareTo
method, which uses a ternary operator to convert boolean expressions to binary.
Here is my new answer, which is much more unreadable.
public static String positiveOrNegative(int n) {
ArrayList<String> responses = new ArrayList<String>();
// first element should be "Zero", so if n is 0, the response is "Zero"
responses.add("Zero");
// this populates the ArrayList with elements "Positive" for n elements
// so that if n is positive, n will be an index in the ArrayList
// and the return will be "Positive"
// but still if n is negative, it will never be an index in the ArrayList
for (int i = 0; i < n; i++) {
responses.add("Positive");
}
String response = "";
try {
// try to get a response from the ArrayList
response = responses.get(n);
} catch (Exception e) {
// index is out of bounds, so it must have been negative
response = "Negative";
}
return response;
}
public static void main(String[] args) {
System.out.println(positiveOrNegative(4)); // Positive
System.out.println(positiveOrNegative(1)); // Positive
System.out.println(positiveOrNegative(0)); // Zero
System.out.println(positiveOrNegative(-1)); // Negative
System.out.println(positiveOrNegative(-4)); // Negative
}