Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Boolean Method Problem

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Boolean Method Problem

    I'm writing a program called Curse Detector. The idea is that you answer 5 questions and this program tells you if an item is cursed.

    The item is cursed if:
    1) The item adds +6 or less to the player's AC bonus AND is worth less than 250,000 gold pieces AND either provides Energy Immunity or Weapon Breaking

    2) Adds more than +6 to AC and does not provide Energy Immunity

    3) Summons Monsters

    I am using two classes, and interface, and CinReader.

    My problem is that: no matter what I put into the test, it tells me that the item is cursed, even if it shouldn't be.

    Here's the interface:

    public interface CurseDetector
    {
        /**
         * Welcome to the Curse Detector program. This program will help you determine if your magical items are cursed
         *
         * @param  addsLessThan7   true if greater than 6.
         * @param  worthOver250K   true if worth more than 250,000 gold peices
         * @param  energyImmunity  true if it provides energy immunity
         * @param  weaponBreaking  true if it provides weapons breaking
         * @param  summonsMonsters true if it provides monster summoning
         *
         * @return   true if likely cursed, false if likely not cursed
         */
        public boolean detectCurse(boolean addsLessThan7,
                            boolean worthOver250K,
                            boolean energyImmunity,
                            boolean weaponBreaking,
                            boolean summonsMonsters
                            );
    }

    Here's the AberrationsDetector:

    /**
     * Write a description of class AberrationsDetector here.
     *
     * @author Erik Ingvoldsen
     * @version 1.0
     */
    public class AberrationsDetector implements CurseDetector
    {
        boolean returnBoolean=false;
        public boolean detectCurse(boolean addsLessThan7, boolean worthOver250K,boolean energyImmunity, boolean weaponBreaking, boolean summonsMonsters)
        {
                if(summonsMonsters=true){
                returnBoolean=true;
                }else{
                if(summonsMonsters=false){
                if(addsLessThan7==true){
                if(worthOver250K==false){
                if(energyImmunity==true){
                returnBoolean=true;
                }
                else if(weaponBreaking==true){
                returnBoolean=true;
                }
                else if(energyImmunity||weaponBreaking==true){
                returnBoolean=false;
                }
                }
                }else{
                if(addsLessThan7==false){
                if(energyImmunity=false){
                returnBoolean=true;
                }
                }
                }
                }else{
                returnBoolean=false;
                }
                }
    return returnBoolean;
    }
    }

    And here is the test:

    /**
     * A tool designed to use the Curse Detector
     *
     * @author Erik Ingvoldsen
     * @version 1.0
     */
    public class CurseTest
    {
    static CinReader Cin= new CinReader();
    public CurseTest()
    {
        System.out.println("Welcome to the Curse Detector program.");
        System.out.println("his program will help you determine if your magical items are cursed.");
        System.out.println("Here are a few questions for  you:");
        System.out.println("    ");
        Question1();
    }
    public static void Question1(){
        System.out.println("What AC advantage does the magic item provide? (Enter an integer)");
        int Q1=Cin.readInt();
        if(Q1>6){
        boolean addsLessThan7=true;
        }else{
        boolean addsLessThan7=false;
        }
        Question2();
    }
    public static void Question2(){
        System.out.println("    ");
        System.out.println("How much is the magic item worth? (Enter an integer)");
        int Q2=Cin.readInt();
        if(Q2>250000){
        boolean worthOver250K=true;
        }else{
        boolean worthOver250K=false;
        }
        Question3();
    }
    public static void Question3(){
        System.out.println("    ");
        System.out.println("Does the item provide Energy Immunity? (Yes or No)");
        String Q3=Cin.readString();
        if(Q3.equals("yes")){
        boolean energyImmunity=true;
        }else if(Q3.equals("no")){
        boolean energyImmunity=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question3();
        }
        Question4();
    }
    public static void Question4(){
        System.out.println("    ");
        System.out.println("Does the item provide Weapon Breaking? (Yes or No)");
        String Q4=Cin.readString();
        if(Q4.equals("yes")){
        boolean weaponBreaking=true;
        }else if(Q4.equals("no")){
        boolean weaponBreaking=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question4();
        }
        Question5();
    }
    public static void Question5(){
        System.out.println("    ");
        System.out.println("Does the item Summon Monsters? (Yes or No)");
        String Q5=Cin.readString();
        if(Q5.equals("yes")){
        boolean summonsMonsters=true;
        }else if(Q5.equals("no")){
        boolean summonsMonsters=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question5();
        }
        Answer();
    }
    public static void Answer(){
        AberrationsDetector myDetector= new AberrationsDetector();
        myDetector.detectCurse(false, false, false, false, false);
        System.out.println("    ");
        if(myDetector.detectCurse(false, false, false, false, false)==true){
        System.out.println("This time is cursed!");
        }else{
        System.out.println("This item is not cursed.");
        }
    }
    }
    Last edited by DarkestLord; September 24th, 2014 at 03:09 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    I would guess its probably an error because of improper indention.
    Indent all your curly brackets correctly as per the java style convention and you are probably going to be able to see the mistake yourself.
    Another note: Why do you use a boolean variable in your AberrationsDetector class? The variable seems completely redundant. Just return the correct boolean value when you know it.
    Furthermore, you might want to think about applying the logical AND and/or OR operators to your boolean variables. You could probably save a lot of if-then-else statements this way.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Problem

    Quote Originally Posted by Cornix View Post
    Another note: Why do you use a boolean variable in your AberrationsDetector class? The variable seems completely redundant. Just return the correct boolean value when you know it.
    Uhhh how would I go about this? ^^;

    Quote Originally Posted by Cornix View Post
    Furthermore, you might want to think about applying the logical AND and/or OR operators to your boolean variables. You could probably save a lot of if-then-else statements this way.
    Could you give me an example? I'm still learning things like this.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    You defined the outcome of your method like this:
    The item is cursed if:
    1) The item adds +6 or less to the player's AC bonus AND is worth less than 250,000 gold pieces AND either provides Energy Immunity or Weapon Breaking
    2) Adds more than +6 to AC and does not provide Energy Immunity
    3) Summons Monsters
    I am using two classes, and interface, and CinReader.
    And you have a method with a signature like this:
        public boolean detectCurse(boolean addsLessThan7,
                            boolean worthOver250K,
                            boolean energyImmunity,
                            boolean weaponBreaking,
                            boolean summonsMonsters
        );
    So what you can do is simply this:
        public boolean detectCurse(boolean addsLessThan7,
                            boolean worthOver250K,
                            boolean energyImmunity,
                            boolean weaponBreaking,
                            boolean summonsMonsters
    	) {
    		if (addsLessThan7 && worthOver250K && (energyImmunity || weaponBreaking)) {
    			return true;
    		}
    		if (!worthOver250K && !energyImmunity) {
    			return true;
    		}
    		if (summonsMonsters) {
    			return true;
    		}
    		return false;
    	}
    This is just a 1:1 translation of your text to code. Study the logical operators "&&", "||" and "!".
    The && is a logical AND; the boolean A && B is only true if both A and B are true.
    The || is a logical OR; the boolean A || B is only true if neither A nor B are false.
    The ! is a logical negation; the boolean !A is only true if A is false.

    Using these you can combine multiple boolean values to one which drastically cuts down the amount of code you have and makes everything more readable and maintainable.


    Of course, you should always keep in mind, that your error might also be in the other parts of your program. It does not necessarily have to be your "CurseDetector" implementation.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Problem

    Oh! I wasn't familiar with &&! Thanks! The program still isn't working, but this is very helpful!

    Hmmm...the only other thing I can think of is the way I coded the test. When I call the method, I call it like this:

    myDetector.detectCurse(false, false, false, false, false);

    Because I'm not sure what else I could put in there (aside from true, true, true, true, true). Would this affect my results?

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Boolean Method Problem

    myDetector.detectCurse(false, false, false, false, false);

    Because I'm not sure what else I could put in there (aside from true, true, true, true, true). Would this affect my results?
    What about: myDetector.detectCurse(true, false, false, false, false);
    Or: myDetector.detectCurse(false, false, false, false, true);
    Or: myDetector.detectCurse(false, true, true, false, false);
    Or any of the other (I'm going to guess: hundreds) of differences?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    I would say its 2^5 different combinations, but thats just an educated guess.

    You probably want to save the input from the user in private member variables and then send the input to your CurseDetector implementation at the end.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Boolean Method Problem

    I would say its 2^5 different combinations, but thats just an educated guess.
    I think that permutations...? Can't remember that stuff, lol. Although I admit it probably isn't hundreds.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Problem

    OK, this IS the problem. It's connected to the input somehow. If I do true, true, true, true, true, the fifth "true" for "Summons Monsters" is always true, which means the item is always cursed. I tried making it "true, true, false, false, false" and now it's never cursed, regardless of input. Now sure what I can do to fix this though....hmmmm...

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    You have to use the input from the user. You must NOT use "true" or "false" at all when calling the method. Use variables that store what the user has entered.

  11. #11
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Problem

    OK, updated the detector and the test...

    Currently the program is still telling me the item is cursed no matter what though. I think the problem is in the test, but I'm not sure what it is...

    Test:

    /**
     * A tool designed to use the Curse Detector
     * 
     * @author Erik Ingvoldsen
     * @version 1.0
     */
    public class CurseTest
    {
    static CinReader Cin= new CinReader();
    static boolean addsLessThan7;
    static boolean worthOver250K;
    static boolean energyImmunity;
    static boolean weaponBreaking;
    static boolean summonsMonsters;
    public CurseTest()
    {
        System.out.println("Welcome to the Curse Detector program.");
        System.out.println("his program will help you determine if your magical items are cursed.");
        System.out.println("Here are a few questions for  you:");
        System.out.println("    ");
        Question1();
    }
    public static void Question1(){
        System.out.println("What AC advantage does the magic item provide? (Enter an integer)");
        int Q1=Cin.readInt();
        if(Q1>6){
        boolean addsLessThan7=true;
        }else{
        boolean addsLessThan7=false;
        }
        Question2();
    }
    public static void Question2(){
        System.out.println("    ");
        System.out.println("How much is the magic item worth? (Enter an integer)");
        int Q2=Cin.readInt();
        if(Q2>250000){
        boolean worthOver250K=true;
        }else{
        boolean worthOver250K=false;
        }
        Question3();
    }
    public static void Question3(){
        System.out.println("    ");
        System.out.println("Does the item provide Energy Immunity? (yes or no)");
        String Q3=Cin.readString();
        if(Q3.equals("yes")){
        boolean energyImmunity=true;
        }else if(Q3.equals("no")){
        boolean energyImmunity=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question3();
        }
        Question4();
    }
    public static void Question4(){
        System.out.println("    ");
        System.out.println("Does the item provide Weapon Breaking? (yes or no)");
        String Q4=Cin.readString();
        if(Q4.equals("yes")){
        boolean weaponBreaking=true;
        }else if(Q4.equals("no")){
        boolean weaponBreaking=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question4();
        }
        Question5();
    }
    public static void Question5(){
        System.out.println("    ");
        System.out.println("Does the item Summon Monsters? (yes or no)");
        String Q5=Cin.readString();
        if(Q5.equals("yes")){
        boolean summonsMonsters=true;
        }else if(Q5.equals("no")){
        boolean summonsMonsters=false;
        }else{
        System.out.println("Not a valid answer. Do not use any capital letters.");
        Question5();
        }
        Answer();
    }
    public static void Answer(){
        AberrationsDetector myDetector= new AberrationsDetector();
        myDetector.detectCurse(addsLessThan7, worthOver250K, energyImmunity, weaponBreaking, summonsMonsters);
        System.out.println("    ");
        if(myDetector.detectCurse(addsLessThan7, worthOver250K, energyImmunity, weaponBreaking, summonsMonsters)==true){
        System.out.println("This time is cursed!");
        }else{
        System.out.println("This item is not cursed.");
        }
    }
    }

    Detector:

    /**
     * Write a description of class AberrationsDetector here.
     *
     * @author Erik Ingvoldsen
     * @version 1.0
     */
    public class AberrationsDetector implements CurseDetector
    {
           public boolean detectCurse(boolean addsLessThan7,
                            boolean worthOver250K,
                            boolean energyImmunity,
                            boolean weaponBreaking,
                            boolean summonsMonsters) 
    {
    		if (addsLessThan7 && !worthOver250K && (energyImmunity || weaponBreaking)) {
    			return true;
    		}
    		if (!addsLessThan7 && !energyImmunity) {
    			return true;
    		}
    		if (summonsMonsters) {
    			return true;
    		}
    		return false;
    }
    }
    Last edited by DarkestLord; September 24th, 2014 at 03:23 PM.

  12. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    You are getting closer, but you still have a little error in your class "CurseTest".
    The problem is, that your variables are never changed. They will always keep their initial value, that is "false".

    This is because you create new local variables in your methods Question1 - Question5. You change the values of these new variables, but the old ones will never get changed.

  13. #13
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Problem

    Quote Originally Posted by Cornix View Post
    This is because you create new local variables in your methods Question1 - Question5. You change the values of these new variables, but the old ones will never get changed.
    Ahhh...but...what makes them new? They are called the same thing in each question? I tried removing "boolean" in front of the question variables, but the problem remains. Is it something else that makes them "new" local variables?

    Edit: Oh wait! I had the first question wrong! It was supposed to be Q1<6, not Q1>6!

    Thanks guys, the programs working now!

  14. #14
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Boolean Method Problem

    If you write "boolean a = true;" you create a new boolean variable called "a" and assign the value "true" to it. If you just write "a = true" you assign the value "true" to a previously defined variable "a". If no variable "a" was previously defined you will get an error.

Similar Threads

  1. Boolean method help!
    By Scorks in forum Object Oriented Programming
    Replies: 3
    Last Post: April 2nd, 2013, 12:33 PM
  2. I need a little help with a boolean method.
    By bankston13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2012, 08:02 PM
  3. Boolean Method Test Problem
    By Khadafi in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2012, 10:07 PM
  4. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 09:18 AM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:56 AM