user4365131
user4365131

Reputation: 9

Return boolean in java?

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

Answers (7)

Eran
Eran

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

Woodchuck
Woodchuck

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

KhAn SaAb
KhAn SaAb

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

wam090
wam090

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

Ganesh Anil Kalumbe
Ganesh Anil Kalumbe

Reputation: 310

       public class ChkNum {
            public boolean inEven(int o) {   
                   return o%2 ==0;
            }
       }

Upvotes: 3

SagarVimal
SagarVimal

Reputation: 259

You can try this-

public boolean inEven(int o)
{   
    if ((o%2)==0) 
   { 
    inEven = true;
   } 
    else  
    { 
      inEven = false;}
    }
   return inEven; 
}

Upvotes: 1

Secondo
Secondo

Reputation: 451

Try this one

public boolean inEven(int o)
{   
     if ((o%2)==0) { inEven = true;} 
     else  
     { inEven = false;}

     return inEven;  

 }

Upvotes: 1

Related Questions