• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with methods  RSS feed

 
John Sing
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.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • 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?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm,

Thanks. Have a cow.


John,

Please read this ... http://www.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
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • 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
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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:
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • 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.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate 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: 56595
    172
    • 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
     
    Tim Cooke
    Marshal
    Posts: 4051
    239
    Clojure IntelliJ IDE Java
    • 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: 10575
    66
    Eclipse IDE Hibernate 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: 56595
    172
    • 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.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shouldn't that be
     
    Campbell Ritchie
    Marshal
    Posts: 56595
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, of course it should. It should also be static.
     
    Dave Tolls
    Ranch Foreman
    Posts: 3068
    37
    • 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: 56595
    172
    • 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
    Ranch Foreman
    Posts: 3068
    37
    • 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: 56595
    172
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!