Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help with methods

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to Java and I am REALLY confused. We just started methods and I am still very bad at them. We have to add to some code to make it work. Here is what we have to do "Write a method isEventhat determines whether an integer is even or odd. The method should take an integer argument and return true if the integer is even. If the integer is odd, it should return false."

Here is the incorrect code



Now again I am still very new to this so I tried putting in code with the only way I know how to use methods. Here is the same code with the code I added.



With that code I am getting a bunch of errors. Here are the errors I am getting.

"6 errors found:
[line: 21]
Error: illegal start of expression

[line: 21]
Error: illegal start of expression

[line: 21]
Error: ';' expected

[line: 21]
Error: ';' expected

[line: 25]
Error: illegal start of expression

[line: 25]
Error: ';' expected"

So yeah I am very confused and I am not sure what to do next. Any advice would be greatly appreciated. In the meantime I think I will try to make my own code to do what the problem states without having to wok around code that's already there. So thanks in advanced and I will update this post if I make any progress.
 
Rancher
Posts: 903
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,

You have a Class called Lesson18Test, it has entry point, a method, called main:

Your assignment is to add another method. Can you see the pattern?
 
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you write the class MethodPractice? Lesson18Test is incomplete and won't compile as posted. In MethodPractice, is the method isEven() a static method?
 
Les Morgan
Rancher
Posts: 903
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: the test for even or odd has usually been defined as divisible by 2.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have attempted this many times and I am still failing. I was able to do this quite easily with no methods



I guess I should start by adding methods in this code. How would I go about doing this?
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to double post but I changed a few things in my code and I am still getting errors. Here is the new code


But I am still getting the following errors

"4 errors found:
[line: 21]
Error: illegal start of expression

[line: 21]
Error: illegal start of expression

[line: 21]
Error: ';' expected

[line: 21]
Error: ';' expected"

So what should I do? I have no idea how to work with the code I have been provided and I think I am not aloud to change it.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have in line 13?

This is not a valid, meaningful line inside the main() method. Why is that line there? Did you mean to put lines 13-18 in a separate method?

A method body starts with { and ends with }. For example in line 4, there's the opening { of the main method. Other methods should be defined outside of the main method - you cannot define methods inside methods in Java.

Make sure you learn the basics of how to structure methods inside a class in your source file.
 
Rancher
Posts: 4611
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the original code that (from what I can gather) needs fixing.
The problem states that you need to write the isEven() method.

Is there a MethodPractice class?
That is where you should be putting this method.
I don't get the impression you should actually be changing the above code at all.
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm,

Thanks. Have a cow.


John,

Please read this ... https://coderanch.com/t/660346/Wiki/Forthright-Cross-Posting-Sites. Cross posting on other sites *is* allowed on the ranch. We just ask that you be forthright regarding it, so as to not waste your fellow rancher's time and effort.

Henry
 
Marshal
Posts: 69782
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Sing wrote:. . .
. . .

As you have already noticed, there are several errors in that code. You can reduce the errors by
  • 1: Getting 99% of that code out of the main method.
  • 2: Only writing a few lines of code before trying to compile it.
  • The line numbers and the errors you quoted do not correspond to each other. In fact, it looks as if you had got the errors from completely different code. For one thing, you cannot post code after a return statement and you have no errors about unreachable code.

    Never write return true; ... return false; like that. It is
    return (test % 2);
    Or it would be if I had used the correct formula after return.
     
    Carey Brown
    Bartender
    Posts: 7196
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Never write return true; ... return false; like that. It is
    return (test % 2);


    Don't you mean:
     
    Sheriff
    Posts: 7646
    522
    Mac OS X VI Editor BSD Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Campbell Ritchie wrote:Never write return true; ... return false; like that. It is
    return (test % 2);


    Don't you mean:


    And if the operation performance is on an important side, you can try to use bitwise AND operator:
    Joshua Bloch in his puzzlers book says it is much faster than using remainder operator, as remainder operations are much slower comparing them with arithmetics.
     
    Bartender
    Posts: 10777
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:Joshua Bloch in his puzzlers book says it is much faster than using remainder operator...


    Maybe, but you need to be careful of "cryptic" code. And in these days when some compilers are smart enough to work out that:
      x * 17
    equals
      (x << 4) + x
    I doubt it'll be too long before writers plug the x % 2 "hole" (if they haven't already).

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 69782
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . .
    Don't you mean:

    Well, I did say

    it would be if I had used the correct formula after return.

    … in the hope of people knowing I Had given the wrong formula. Yes, maybe I should have given the correct formula.
     
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:

    This is the original code that (from what I can gather) needs fixing.
    The problem states that you need to write the isEven() method.

    Is there a MethodPractice class?
    That is where you should be putting this method.
    I don't get the impression you should actually be changing the above code at all.



    Yes there is a MethodPractice class

    I will try redoing my code step by step in there and see if I can get it to work that way. Also sorry about the cross posting thing, I did not know that would be considered rude. For now on I will put a link to my other post when I do that.
     
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried deleting everything I did in Lesson18Test and I started focusing on MethodPractice. Here is my new code



    I am getting the following errors from that code

    " [line: 19]
    Error: incompatible types: boolean cannot be converted to int

    [line: 21]
    Error: incompatible types: boolean cannot be converted to int

    [line: 13]
    Error: incompatible types: int cannot be converted to boolean"

    How can I fix these errors? I tried changing the method type to boolean but then I get a error in my other code



    The error is


    " [line: 13]
    Error: incompatible types: int cannot be converted to boolean"

    So either way I seem to be getting errors. How can I fix this?

    EDIT- Well I managed to fix it Thanks everyone
     
    Sheriff
    Posts: 4870
    317
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great to hear you got it sorted, and good job spotting that Dave Tolls gave you the key hint towards what you needed to do.

    For the benefit of others reading this thread, why not show us what your final working solution was?
     
    Winston Gutkowski
    Bartender
    Posts: 10777
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    John Sing wrote:How can I fix these errors? I tried changing the method type to boolean but then I get a error in my other code...


    Glad you got the problem fixed, but in partial answer to your question above, you can stop getting so many errors by compiling more often.

    The fact is that probably 80% (maybe more) of the "Help!" problems we get in the Beginning Java forum boil down to one simple fact:

    Newbies don't compile often enough.

    The advice I usually give can be found here, but there's a really simple rule you can follow:
    If you've written 10 lines of code since you last compiled: COMPILE AGAIN.

    It may seem like a pain, but it'll save you a lot of grief in the long run.

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 69782
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    John Sing wrote:I tried deleting everything I did in Lesson18Test and I started focusing on MethodPractice. Here is my new code

    ...

    That should read Don't write 6 in the method, otherwise it will return true from everything you pass. As I said yesterday, don't write return true; else return false. (but this time I have given you the right formula). The round brackets () in that method are redundant but it hardly ever does any harm to add () unnecessarily. You can also omit the else.
    As you have been told you can write this which will have slightly better performance, but I bet you won't understand it.
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shouldn't that be
     
    Campbell Ritchie
    Marshal
    Posts: 69782
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, of course it should. It should also be static.
     
    Dave Tolls
    Rancher
    Posts: 4611
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Yes, of course it should. It should also be static.



    No it shouldn't (as far as the exercise is concerned).
    The code in main() creates a MethodPractice instance and calls isEven() on that.

    Yes, since it doesn't actually do anything involving state of the MethodPractice instance, it should be static, but that's the nature of a lot of exercises.
     
    Campbell Ritchie
    Marshal
    Posts: 69782
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:. . . No it shouldn't . . . it should . . .

    Careful saying that sort of thing this time of year

    I still think, “Oh yes, it should!”, and it should be called from the methods of the method practice instance not from the main method.
     
    Dave Tolls
    Rancher
    Posts: 4611
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Dave Tolls wrote:. . . No it shouldn't . . . it should . . .

    Careful saying that sort of thing this time of year

    I still think, “Oh yes, it should!”, and it should be called from the methods of the method practice instance not from the main method.



    Seems a bit of pointless redirection of a call, for an exercise.

    And that's the key word.

    (Behind you!)
     
    Campbell Ritchie
    Marshal
    Posts: 69782
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, no, it isn't!

    The problem is that we have newbies here, and we want them to learn to do things without taking shortcuts. It is only when you are experienced that you take shortcuts.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic