Results 1 to 5 of 5
  1. #1
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default problems with boolean and recursive methods

    reposted under more descriptive title, couldn't delete the post, here's a link to the new one:
    problems with boolean and recursive methods
    Last edited by mflb94; 09-27-2012 at 07:43 AM.

  2. #2
    huupderitis is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Some problems I am having with my homework.

    I'm a nooby too... however this is how i would write the isTriangle() method

    public boolean isTriangle(int a, int b, int c)
    {
    boolean triangle = false;
    if (a + b < c || a + c < b || b + c < a )
    {
    triangle = true;
    }
    return triangle;
    }

    I'm not sure how your code is set up I didn't really look at it... It is quite possible that the scope of your boolean is outside of the test and it is remaining true no matter what. For each new test the boolean should be set to false until it can be proven true. In other words you have to re-initialize the variable each time you use it. For instance if you were ringing up someones purchase on a cash register. When you go to ring up the next person you want to make sure that the purchase amount = 0; otherwise they'll accumulate the purchase of the person before them.

  3. #3
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default problems with boolean and recursive methods

    Hey everyone, complete noob here, hoping you guys can help me with some of my homework problems.
    First one: my professor assigned us to create a method in a program to test whether certain side lengths would form a triangle. This would be simple, however he wrote most of the program himself, and is only having us write one method, and we are not allowed to modify the methods he wrote. Here is the code to show where this is problematic.

    Java Code:
    /**
     * Homework 03.1 - Triangle
     * Exercise 6.3, page 70.
     */
    
    /**
     * @author andre
     * @helpedby LIST PEOPLE WHO HELPED OR WORKED WITH YOU
     * @consulted LIST RESOURCES (BOOKS, WEB SITES) THAT YOU USED
     *
     */
    public class Triangle {
    
    	/**
    	 * Main program: run test cases via a testing method.
    	 * @param args
    	 */
        public static void main(String[] args) {
            isTriangleTest(0, 0, 0, false);
            isTriangleTest(0, 0, 1, false);
            isTriangleTest(0, 1, 0, false);
            isTriangleTest(1, 0, 0, false);
            isTriangleTest(0, 1, 1, false);
            isTriangleTest(1, 0, 1, false);
            isTriangleTest(1, 1, 0, false);
            isTriangleTest(1, 1, 1, true);
            isTriangleTest(3, 3, 1, true);
            isTriangleTest(3, 1, 3, true);
            isTriangleTest(1, 3, 3, true);
            isTriangleTest(3, 4, 5, true);
            isTriangleTest(3, 5, 4, true);
            isTriangleTest(4, 3, 5, true);
            isTriangleTest(4, 5, 3, true);
            isTriangleTest(5, 3, 4, true);
            isTriangleTest(5, 4, 3, true);
            isTriangleTest(1, 2, 7, false);
            isTriangleTest(1, 7, 2, false);
            isTriangleTest(2, 1, 7, false);
            isTriangleTest(2, 7, 1, false);
            isTriangleTest(7, 1, 2, false);
            isTriangleTest(7, 2, 1, false);
            isTriangleTest(-1, -1, -1, false);
            isTriangleTest(-3, 4, 5, false);
        }
    
        /**
         * Run isTriangle() with a single test case, and report results.
         * @param a first length
         * @param b second length
         * @param c third length
         * @param shouldBe what the result of isTriangle(a, b, c) *should* be
         */
        public static void isTriangleTest(int a, int b, int c, boolean shouldBe) {
            boolean result = isTriangle(a, b, c);
            System.out.print("isTriangle("
                    + a + ", " + b + ", " + c 
                    + ") returns " + result 
                    + ", should be " + shouldBe + " ... ");
            if (result == shouldBe) {
                System.out.println("correct");
            } else {
                System.out.println("*** OOPS!! ***");
            }
        }
    
        /**
         * Tests whether three lengths could form the sides of a triangle. The
         *  test is simply this: is the longest length less than the sum of the
         *  other two lengths? [Note: if several sides share the longest length,
         *  it does not matter which is picked to be the "longest" side.]
         * @param a a length
         * @param b a second length
         * @param c a third length
         * @return true if the lengths could form a triangle, false otherwise 
         */
    
        public static boolean isTriangle(int a, int b, int c) {
        if (a + b >= c && a + c >= b && b + c >= a) {
        	return true;
        }else{
        	return false;
        }
        }
      }
    the method isTriangle is the one we are supposed to write, and originally I wrote it the way he said to, returning a value of true if the lengths do make a triangle, and false if not. But the problem is in the way the isTriangleTest method is written, where it states that if result is equal to shouldBe, than it will print "correct". This works for the true ones, but the error lies in the fact that it will also print a result of "correct" if both of the variables return false, effectively leading the program to print out every test value as "correct". I already asked the professor about this and he just said "he'd look at it" but the assignment is due next class.
    Last edited by mflb94; 09-27-2012 at 04:14 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Some problems I am having with my homework.

    But the problem is in the way the isTriangleTest method is written, where it states that if result is equal to shouldBe, than it will print "correct". This works for the true ones, but the error lies in the fact that it will also print a result of "correct" if both of the variables return false, effectively leading the program to print out every test value as "correct"
    I think you might be misinterpreting what "correct" means in this context. You teacher does not mean "the number forms a correct triangle" rather, he or she means "the method correctly determined whether the numbers form a triangle or not". So, you are supposed to get "correct" every time: even for things that can't possibly be triangles like 1,1,42.

    ---

    You use && in your condition, and huupderitis uses ||. Which do you prefer? Why?

    ---

    Both of you might like to consider the following:

    Java Code:
    public static boolean a() {
        if(whatever) {
            return true;
        } else {
            return false;
        }
    }
    
    public static boolean b() {
        boolean isWhatever = false;
        if(whatever) {
            isWhatever = true;
        }
        return isWhatever;
    }
    
    public static boolean c() {
        return whatever;
    }
    Here whatever stands for some boolean expression or other. Same things to think about: which do you prefer? and why?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: problems with boolean and recursive methods

    I've merged the two threads. You could have edited the subject in the original thread (you need to click 'Go Advanced' on the quick edit screen for that).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Boolean methods & how to read this in plain English
    By bigsonny in forum New To Java
    Replies: 6
    Last Post: 06-19-2011, 09:27 PM
  3. help with recursive return boolean
    By Yakg in forum New To Java
    Replies: 11
    Last Post: 01-11-2011, 05:54 PM
  4. Boolean methods
    By sonny in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 10:16 PM
  5. Boolean problems
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 09-15-2009, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •