Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Exclamation Testing boolean method

    There are two parts I need help with.

    Here is my code for the first method
    public static boolean isOdd (int x)
    {
    if (x%2==1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    I declared this in a separate file than my main method and I need to know how to test it in the main method.

    2.
    public static boolean closeAccount (double acctBalance, int checksLeft) {
    if ((acctBalance < 0)||(checksLeft <= 0))
    return true;
    else
    return false;
    // returns true if acctBalance is less than zero or checksLeft is less than or equal to zero.
    }
    public static boolean closeAccount (int acctBalance, int checksLeft) {
    if ((acctBalance < 0)||(checksLeft <= 0))
    return true;
    else
    return false;

    I also need to know how to test these two methods in the main method.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    have you tried jUnit ?

  3. #3
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    Your method is static, so you should be able to call it directly in the main method. Just make sure you reference the class the method is contained in.

    Java Code:
    public static void main(String[] args) {
        YourClass.isOdd(3);
    }

  4. #4
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    thanks for the help but i figured it out. I just needed to call the method in if statements to make sure that it was functioning correctly. Here is the code i used:

    if (timothyfarmertoolbox.isOdd(32))
    System.out.println("Integer is odd.");

    else
    System.out.println("Integer is even.");

    if (timothyfarmertoolbox.isOdd(31))
    System.out.println("Integer is odd.");

    else
    System.out.println("Integer is even.");

    if (timothyfarmertoolbox.closeAccount(200,20))
    System.out.println("Account has been closed.");

    else
    System.out.println("Account is still open.");


    if (timothyfarmertoolbox.closeAccount(200.78,20))
    System.out.println("Account has been closed.");

    else
    System.out.println("Account is still open.");

    if (timothyfarmertoolbox.closeAccount(-200.78,20))
    System.out.println("Account has been closed.");

    else
    System.out.println("Account is still open.");

    if (timothyfarmertoolbox.closeAccount(200,0))
    System.out.println("Account has been closed.");

    else
    System.out.println("Account is still open.");

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    why dont you put your main class extend to the "class that has the odd function"..Then you need not call the class method again and again...

    you can do isodd(...)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by [RaIdEn] View Post
    why dont you put your main class extend to the "class that has the odd function"..Then you need not call the class method again and again...

    you can do isodd(...)
    I agreed with you. The way that logic build is not good at all.

  7. #7
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    I've only been programming since the beginning of the semester so I don't know how to do that. If you could give me an example that would be awesome. Do you need me to post my whole code so you can show me?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Is it a long code? You can post it here and all the other members can comment on that relevant to there experience. It's much better if your code is relatively small, easy to read. And also don't forget to use code tags when you posting here. Good luck!

Similar Threads

  1. destroyApp(boolean unconditional) method
    By thienphongvu in forum CLDC and MIDP
    Replies: 1
    Last Post: 08-07-2009, 09:14 AM
  2. Array Method Testing
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 04-15-2009, 08:23 AM
  3. [SOLVED] calling a boolean method, confusion!!
    By AngrYkIdzrUlE in forum New To Java
    Replies: 18
    Last Post: 03-15-2009, 10:23 AM
  4. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 02:36 AM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 02:01 AM

Tags for this Thread

Posting Permissions

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