0

This is a weird question, I know, but I need to write a program with 3 questions basically [(a OR b) AND c] without using if. What my teacher wants us to ask the user if an animal is black and answer y or n. If n ask if it is white and answer y or n. If either statement is true, then ask if it is friendly, answering y or n. If it is black or white and friendly then we get a message that it can come home with me or else we get a sorry message My problem is that she says we can use only one if and must use compareToIgnoreCase and a function. I can do this with if, but I can't figure out even how to begin without using if. Please help, I've Googled, read all kinds of answers to anything sounding at all promising, and all I keep finding directs me to use if statements.

Mary Ross
  • 31
  • 2
  • not sure if I completely understood, but couldn't you just save booleans and do a single if at the end? Like: `boolean flag = input.equals("black");` something like that. – Ken Mar 07 '13 at 18:05

3 Answers3

1

This seems like a question to teach you short-circuit evaluation. The idea is to have function answersYesTo(String question) and use that in your boolean expression (a || b) && c. Short-circuit evaluation will start with evaluating a and only evaluate b if a evalutes to false. The reason for this is that if a is true, then the we already know that a||b is true, so there is no need to evalute the last part of the subexpression.

Furthermore, c will NOT be evaluated if a||b evalutes to false, since we at that point know that the expression will evalute to false.

The following code shows one possible implementation:

import java.io.Console;

public class App 
{

    static public void main(String [] args) {
        boolean allowedToBringHome = 
            (answersYesTo("Is the animal black?")||answersYesTo("Is the animal white?")) 
            && answersYesTo("Is it friendly?");

        if( allowedToBringHome ) {
            print("You can bring the animal home.");
        }
        else {
            print("Sorry, you can't bring the animal home.");
        }
    }

    static boolean answersYesTo(String question) {
        String answer = System.console().readLine(question);
        return answer.compareToIgnoreCase("y")==0;
    }

    static void print(String msg) {
        System.out.println(msg);
    }
}

NOTE: When using short-circuit evaluation always consider readbility of your code. Complex expressions become difficult to read and grasp very quickly, which increases the risk of introducing bugs.

TAS
  • 2,039
  • 12
  • 17
  • I had thought to use the OR/And like you did to eliminate ifs, but I still ran into too many of them. I see now that my problem was how I was writing my function. Thank you, I think that I can wrap my head around the problem better now. – Mary Ross Mar 07 '13 at 20:22
0

You can try using the ternary operator (http://en.wikipedia.org/wiki/%3F:)

You not using the actual "if" operator, but instead an if/else.

Example:

if (a == b) return 1; else return 0;

is the same as

return (a == b) ? 1 : 0

0

I don't think we're going to do your homework for you, but depending on your teacher's definition of using an 'if', you may be able to use a ternary operator.

i.e. you can write if (A) do x else do y as A ? x : y.

Alternatively, read up on switch/case statements. This isn't a great solution for this sort of thing but does work with your constraints.

IvanJoukov
  • 24
  • 1
  • That's ok, IvanJoukov, I don't want my homework done for me, well ok, I was so frustrated that I would have taken it, but that is why I deliberately gave only the section that I was stuck on, so I wouldn't be tempted. I thought about using switch, but I kept running into if again. However, I looked at it again, and I think I see how this could work now. – Mary Ross Mar 07 '13 at 20:15