Results 1 to 8 of 8
Thread: Testing boolean method
- 10-21-2009, 06:23 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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.
- 10-21-2009, 03:26 PM #2
have you tried jUnit ?
- 10-21-2009, 04:39 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 25
- Rep Power
- 0
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); }
- 10-21-2009, 06:02 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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.");
- 10-21-2009, 09:44 PM #5
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 12
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(...)
- 10-22-2009, 05:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 10-22-2009, 06:35 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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?
- 10-23-2009, 04:12 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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
-
destroyApp(boolean unconditional) method
By thienphongvu in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 09:14 AM -
Array Method Testing
By Suzanne1187 in forum Java AppletsReplies: 1Last Post: 04-15-2009, 08:23 AM -
[SOLVED] calling a boolean method, confusion!!
By AngrYkIdzrUlE in forum New To JavaReplies: 18Last Post: 03-15-2009, 10:23 AM -
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM
Bookmarks