View Full Version : Even + method
NichoLe
03-21-2005, 02:45 PM
Good Afternoon all ...
Ive beeen trying this question out for 2 days now but no improvment i might be losing it or something ... can someone help
1- Create the class Even. It should contain two methods:
isEven: The method should take an integer argument and return true if the integer in even and false otherwise. The method should use the reminder operator to determine if the integer is even or not.
The main method should ask the user to input a number and it will call the isEven method to check if it is even or not. If the number is even the maid method will display "YOU ENTERED AN EVEN NUMBER" If the number is odd the mmain method should continue to ask the user to input numbers until he enters "0".
This is what i did
import javax.swing.JOptionPane;
class Even
{
public static void main (String args[])
{
String Num1;
int number1, num;
Num1= JOptionPane.showInputDialog("Enter a number: ");
number1 = Integer.parseInt(Num1);
isEven(num);
System.exit( 0 );
}
static void isEven(int a)
{
if ( a % 2 == 1)
{
JOptionPane.showMessageDialog(null, "You entered an even number " + a);
}
else if ( a % 2 == 0)
{
JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
}
}//isEven()
}//end main
Please tell me if am on the wrong track!!
shmoove
03-21-2005, 04:48 PM
Your isEven method is reversed. If a number can be divided by 2 with no remainder then that number is even - hence (a % 2 == 0) => a is even.
Other than that, you haven't filled the requirements of your homework to the letter:
1) The isEven method should return a boolean value (true or false) and not be the one printing the statement (that should be in main).
2) The main method should have a while loop so that it can keep asking for input until the user enters an even number.
shmoove
NichoLe
03-22-2005, 11:07 AM
Thank you bro ...
i tried this ... :confused:
import javax.swing.JOptionPane;
class Even
{
public static void main (String args[])
{
String Num1;
int number1;
//run a loop for this to go for ever unless user enters 0
while(true)
{
Num1= JOptionPane.showInputDialog("Enter 0 (zero) to quit\nOr Enter a number: ");
number1 = Integer.parseInt(Num1);
if(number1<=0)
{
System.exit(0);
}
else{
isEven(number1);
}
}//end loop
static void isEven(int a)
{
if ( a % 2 == 1)
{
JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
}
else if ( a % 2 == 0)
{
JOptionPane.showMessageDialog(null, "You entered an even number " + a);
}
}//isEven()
}//end main
shmoove
03-22-2005, 11:58 AM
Better. But again, the assignment asks for an isEven() method that returns a boolean value:
isEven(number)
if number is even return true
if number is odd return false
Then you can use it in a conditional statement inside main():
if (isEven(someNumber))
print out "It's even"
else
print out "It's odd"
shmoove
NichoLe
03-23-2005, 10:00 AM
it worked :D
import javax.swing.JOptionPane;
class Even
{
public static void main (String args[])
{
String Num1;
int number1;
//run a loop for this to go for ever unless user enters 0
while(true)
{
Num1= JOptionPane.showInputDialog("Enter 0 (zero) to quit\n" +
"Or Enter a number: ");
number1 = Integer.parseInt(Num1);
if(number1<=0)
System.exit(0);
else
isEven(number1);
}//end loop
}// end main
static void isEven(int a)
{
if ( a % 2 == 1)
JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
else if ( a % 2 == 0)
JOptionPane.showMessageDialog(null, "You entered an even number " + a);
}//isEven()
}//end class
Letz party
Roelf
03-23-2005, 10:26 AM
But not as you were told to do:
isEven: The method should take an integer argument and return true if the integer in even and false otherwise. The method should use the reminder operator to determine if the integer is even or not.
your IsEven method does not return anything (void). It just outputs some text to a userinterface
The main method should ask the user to input a number and it will call the isEven method to check if it is even or not. If the number is even the maid method will display "YOU ENTERED AN EVEN NUMBER" If the number is odd the mmain method should continue to ask the user to input numbers until he enters "0".
the outputting of your IsEven method should be done by the main method
Please tell me if am on the wrong track!!
you are a bit on the wrong track, follow shmoove's hints and you will be graded better than you will be now
NichoLe
03-23-2005, 10:29 AM
mmmmmmmmmmm .. am confused now ... :(
ill try 2 follow ur hints
hinokata
03-23-2005, 10:52 AM
public boolean thisIsABigHint();
Roelf
03-23-2005, 11:50 AM
main(){
if (thisIsABigHint()) {
it sure is;
} else {
perhaps NicholE needs more hints
}
}
NichoLe
03-24-2005, 07:17 PM
Thank you i submitted it :]
Roelf
03-25-2005, 05:02 PM
which one did you submit, the last posted version? Or an improved version which meets the requirements?
NichoLe
03-25-2005, 07:54 PM
This one
class Even
{
public static void main (String args[])
{
String Num1;
int number1;
//run a loop for this to go for ever unless user enters 0
while(true)
{
Num1= JOptionPane.showInputDialog("Enter 0 (zero) to quit\n" +
"Or Enter a number: ");
number1 = Integer.parseInt(Num1);
if(number1<=0)
System.exit(0);
else
isEven(number1);
}//end loop
}// end main
static void isEven(int a)
{
if ( a % 2 == 1)
JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
else if ( a % 2 == 0)
JOptionPane.showMessageDialog(null, "You entered an even number " + a);
}//isEven()
}//end class
why :confused:
Roelf
03-25-2005, 08:11 PM
your isEven should look like this:
public static boolean isEven(int a)
{
if ( a % 2 == 1) {
// the number is odd, so return false
return (false);
} else {
// the number is even, so return true
return (true);
}
}//isEven()
that way it returns a boolean, true if the supplied argument is even and false if it is odd.
Now you can ask in the main method whether a number is odd or even by doing:
if (isEven(number)) {
// the number is even
} else {
// the number is odd
}
NichoLe
03-27-2005, 05:53 AM
Thank you soooooooo much ..
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.