programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# checking the validity of the statement

Greenhorn
Posts: 27
Integer A is given. It is needed to check the validity of the statement: "Number A is odd".

Would  you be  so  kind to review my  code:

Is it correct? May be  there should  be something  other instead of (a % 2 == 0).

Greenhorn
Posts: 11
Hey there, welcome to the site! Make sure when you're writing you question to highlight your code and hit the the code button in the editor. It makes the code a lot easier to read. You're code looks fine to me! I believe that is all that is necessary to determine a number is even or odd.

Nick Mario
Greenhorn
Posts: 11
The only thing you could add to your code is a way to allow the user to enter a number. You could use Scanner for that. I don't know if your assignment requires that or not.

Sheriff
Posts: 4930
334
Welcome once again.

I'd say the solution isn't correct, because it does something else from what is asked, unless those instructions are'nt the ones you were given:
Instructions wrote:It is needed to check the validity of the statement: "Number A is odd".

And what you are actually doing? Checking if number is even, then printing it is even, if not even, then you printining it is odd. But what is actually asked? (read above your posted instructions).

For me that formulation suggests that it is needed a method, which returns an answer true or false.

Vital Jays
Greenhorn
Posts: 27
Thank you  for  your  answers. For  the  future i  will try  to highlight the code.

In  my  code i'm trying to show  that the  number is  odd.

Liutauras Vilda
Sheriff
Posts: 4930
334
Vital Jays wrote:In  my  code i'm trying to show  that the  number is  odd.

You are not. Assign a = 2 and you'll see what you are trying to show.

lowercase baba
Bartender
Posts: 12565
49
Programmers are EXTREMELY literal. It's kind of a hazard of the job - or the job attracts that kind of person.

If i was told to "check the validity of the statement: "Number A is odd", i'd interpret that as "tell me if the statement is true or false".  I would say the only valid answers to this question are "true" or "false". Anything else is not answering the question given to me.

Vital Jays
Greenhorn
Posts: 27
Well, i  see  that  to  some extent you  are right, cause  in case  we  put 2 or 4 or 6 we get an even number

Sheriff
Posts: 11494
180
Vital Jays wrote:Well, i  see  that  to  some extent you  are right, cause  in case  we  put 2 or 4 or 6 we get an even number

I wouldn't go so far as to say "EXTREMELY literal". Maybe "very particular about being precise and correct." (See what I did there?)

When the requirement is to "Verify the correctness of the statement 'The number A is odd'" then you'd expect a program that does something like this:

Given A = 1, the program outputs

The statement "Number A is odd" is true.

Given A = 2, the program outputs

The statement "Number A is odd" is false.

The way your program is written, it behaves differently:

Given A = 1, the program outputs

An odd number

Given A = 2, the program outputs

An even number

Given the stated requirement, the first program produces the correct and proper output. Your program does the correct evaluation but it produces incorrect improper (per the requirements) ouput.

Vital Jays
Greenhorn
Posts: 27
I rewrote my  code using true and  false.
Everything  depends here on initialised  number  of  A.
if it is 1 or 3  or 5 or 7 than  the  programme  shows true. If  there is 2 or 4 or 6 in  the programme than it shows false.

Liutauras Vilda
Sheriff
Posts: 4930
334
Would you like to create a method for that and put all that code in there? That way your code would become simpler, 1 line basically

Vital Jays
Greenhorn
Posts: 27
or we can  do  this just using variable boolean

Sheriff
Posts: 22844
43
Vital Jays wrote:Integer A is given. It is needed to check the validity of the statement: "Number A is odd".

There's your requirement. Your latest code does that, but it also prints something on the console. I don't see where the original requirement says anything about conveying the result of the validity check to the outside world.

That's why Liutauras suggested writing a method which checks the validity. If you have a method then when you need to check if a number is odd, you can call that method and it will say yes or no (or in Java, true or false).

 Don't get me started about those stupid light bulbs.