Reputation: 9
I need help on this question.
"Inside the ChkNum class,the method inEven()return if the value that it is passed " is Even.It return false that is odd.Therefore isEven()has a return type of boolean."
also those line below cannot be changed.
if(e.inEven(10))System.out.println("10 is even");
if(e.inEven(9))System.out.println("9 is even");
if(e.inEven(8))System.out.println("8 is even");"
i am a beginner in java. I try to finish this question like the following program.Anyway, it doesn't work.=( What was I doing wrong?
public class ChkNum{
boolean inEven=true;
public boolean inEven(int o)
{
if ((o%2)==0) { inEven = true;}
else
{ inEven = false;}
return inEven;
}
;
}
class main{
public static void main(String args[])
{ChkNum e=new ChkNum();
if(e.inEven(10))System.out.println("10 is even");
if(e.inEven(9))System.out.println("9 is even");
if(e.inEven(8))System.out.println("8 is even");
}
}
Upvotes: 0
Views: 9177
Reputation: 394126
If you'd indent your code, you'd find the problem:
public boolean inEven(int o)
{
if ((o%2)==0) {
inEven = true;
} else {
inEven = false;
}
return inEven;
} // remove this
; // remove this
}
It would also make more sense to make inEven
a local variable (i.e., declare it inside the inEven
method).
Upvotes: 6
Reputation: 4464
Note that while some of these answers work, the code can be simplified:
public class ChkNum {
//boolean inEven=true;
public boolean inEven(int o) {
//if ((o%2)==0) {
// inEven= true;
//} else {
// inEven = false;
//}
//return inEven;
// replace the above with:
return o % 2 == 0;
}
}
This would make the code more succinct and readable. It also eliminates the need for the global variable inEven. The boolean expression returns a boolean already, so you can simply return that.
Here's the Test class for completeness (kept the same):
public class Test {
public static void main (String args[]) {
ChkNum e = new ChkNum();
if(e.inEven(10))System.out.println("10 is even");
if(e.inEven(9))System.out.println("9 is even");
if(e.inEven(8))System.out.println("8 is even");
}
}
Upvotes: 0
Reputation: 5376
There is no any specific way to return boolean value, you can return any type through the same way
1.Boolean
public boolean inEven(int o)
{
if ((o%2)==0) {
inEven = true;
} else {
inEven = false;
}
return inEven;
}
2.Integer
public int inEven(int o)
{
if ((o%2)==0) {
inEven = 1;
} else {
inEven = 0;
}
return inEven;
}
Like the same way for String,Double
etc you can return any type value.
Upvotes: 3
Reputation: 2873
Your code indentation is bad why dont you make it simple and clear for yourself, you have time to be a pro and write in style..
Thats your inEven function..
public boolean inEven(int o)
{
if ((o%2)==0) { inEven = true;}
else
{ inEven = false;}
return inEven;
} // <- What is this doing
; // <-What is this doing
}
Should be like :
public boolean inEven(int o)
{
if ((o%2)==0)
{
inEven = true;
}
else
{
inEven = false;
}
return inEven;
}
Upvotes: 1
Reputation: 310
public class ChkNum {
public boolean inEven(int o) {
return o%2 ==0;
}
}
Upvotes: 3
Reputation: 259
You can try this-
public boolean inEven(int o)
{
if ((o%2)==0)
{
inEven = true;
}
else
{
inEven = false;}
}
return inEven;
}
Upvotes: 1
Reputation: 451
Try this one
public boolean inEven(int o)
{
if ((o%2)==0) { inEven = true;}
else
{ inEven = false;}
return inEven;
}
Upvotes: 1