5 Replies - 1275 Views - Last Post: 07 January 2016 - 04:44 AM Rate Topic: -----

#1 logonin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 27-July 15

Help with methods (beginner question)

Posted 06 January 2016 - 09:47 PM

Hi I am a beginner to Java and I really need some help. I have to add to some code that has been provided to do the following "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 code I have been provided

public class Lesson18Test
{
  public static void main( String[] args )
  {
    MethodPractice mp = new MethodPractice();
    
    boolean isEvenTest = false;
    boolean multipleTest = false;
    double minimum3Test = 0.0;
    int gcdTest = 0;
    
    //Problem 1 Test
    isEvenTest = mp.isEven(6);
    System.out.println( "isEven(6) output: " + isEvenTest );
    


So here is what I tried to do
public class Lesson18Test
{
  public static void main( String[] args )
  {
    MethodPractice mp = new MethodPractice();
    
    boolean isEvenTest = false;
    boolean multipleTest = false;
    double minimum3Test = 0.0;
    int gcdTest = 0;
    
    //Problem 1 Test
    public void setisEvenTest ( int Test )
    int test = 6;
    if (test % 2)
     return true;
    else
      return false;
    isEvenTest = mp.isEven(6);
    System.out.println( "isEven(6) output: " + isEvenTest );


Here are the errors I am currently getting in my code
" [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 to fix these errors? Am I even doing this the right way? I am not entirely sure how to work with the code my teacher provided, but I think I coded the right way. If I am correct my code will print "isEvenTest" which will either be true or false because it is connected to the int Test. So can someone please help me fix this? Thanks in advanced :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with methods (beginner question)

#2 logonin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 27-July 15

Re: Help with methods (beginner question)

Posted 06 January 2016 - 10:43 PM

EDIT- I was able to fix the errors I was getting :) But just as I feared this paved the way for an entire new set of errors. I didn't really have a chance to look at it yet but here is my new code

public class NewTest
{
  public static void main( String[] args )
  {
    MethodPractice mp = new MethodPractice();
    {
    boolean isEvenTest = false;
    boolean multipleTest = false;
    double minimum3Test = 0.0;
    int gcdTest = 0;
    }
  }
    //Problem 1 Test
    public void setisEvenTest ( int Test ){
    int test = 6;
    if (test % 2)
     return true;
    else
      return false;
    isEvenTest = mp.isEven(6);
    System.out.println( "isEven(6) output: " + isEvenTest );


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

[line: 25] Error: incompatible types: unexpected return value

[line: 27] Error: incompatible types: unexpected return value

[line: 28] Error: cannot find symbol symbol: variable isEvenTest location: class NewTest

[line: 28] Error: cannot find symbol symbol: variable mp location: class NewTest

[line: 29] Error: cannot find symbol symbol: variable isEvenTest location: class NewTest"

Thanks in advanced :)
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: Help with methods (beginner question)

Posted 06 January 2016 - 11:44 PM

You haven't posted all the code, since those errors refer to line numbers that aren't shown.

A couple of things:

1. Why are you implementing the logic for isEven() inside setIsEvenTest? A method beginning with set should only be, well, setting the value of a member variable.

2. Are you sure the test on line 16 is correct? If test is even, what is the value of test % 2?

3. Your setIsEvenTest() takes a parameter called Test and you're declaring a local variable called test. Don't use similarly named variables like that, as it's confusing.

4. Why the curly braces on lines 6 and 11?

This post has been edited by ndc85430: 06 January 2016 - 11:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 logonin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 27-July 15

Re: Help with methods (beginner question)

Posted 07 January 2016 - 12:27 AM

View Postndc85430, on 06 January 2016 - 11:44 PM, said:

You haven't posted all the code, since those errors refer to line numbers that aren't shown.

A couple of things:

1. Why are you implementing the logic for isEven() inside setIsEvenTest? A method beginning with set should only be, well, setting the value of a member variable.

2. Are you sure the test on line 16 is correct? If test is even, what is the value of test % 2?

3. Your setIsEvenTest() takes a parameter called Test and you're declaring a local variable called test. Don't use similarly named variables like that, as it's confusing.

4. Why the curly braces on lines 6 and 11?


I made a few changes to the code and I think I am making progress. But I am still very confused. Here is my new code

public class NewTest
{
  public static void main( String[] args );
  
    MethodPractice mp = new MethodPractice();
   
    boolean isEvenTest = false;
    boolean multipleTest = false;
    double minimum3Test = 0.0;
    int gcdTest = 0;
    
  
    //Problem 1 Test
    public void setisEvenTest ( int Test )
    {
    isEvenTest = Test;
    }
    
      public String isEven()
      {
        return isEvenTest;
    int Apollo = 6;
    
    if (Apollo % 2 == 0)
     return true;
    else
      return false;
    isEvenTest = mp.isEven(6);
    System.out.println( "isEven(6) output: " + isEvenTest );
      }
}


Here are the errors I am currently getting

" [line: 11]
Error: missing method body, or declare abstract

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

[line: 29]
Error: incompatible types: boolean cannot be converted to java.lang.String

[line: 33]
Error: incompatible types: boolean cannot be converted to java.lang.String

[line: 35]
Error: incompatible types: boolean cannot be converted to java.lang.String

[line: 36]
Error: cannot find symbol
symbol: method isEven(int)
location: variable mp of type MethodPractice"
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: Help with methods (beginner question)

Posted 07 January 2016 - 02:21 AM

I don't have too much time to look at this right now, but several things in this code don't make any sense:

1. Why does isEven() return a String (line 19)?
2. If isEvenTest is a boolean (line 7), why are you trying to assign an int to it on line 16?

Also, the code is poorly indented. Please indent in a sensible way, as it helps to read and therefore understand code. Here is a guide to Java coding conventions that includes a section on indentation.

This post has been edited by ndc85430: 07 January 2016 - 02:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Help with methods (beginner question)

Posted 07 January 2016 - 04:44 AM

Also posted at: http://www.coderanch...va/java/methods
and: http://forums.codegu...lp-with-methods

This post has been edited by NormR: 07 January 2016 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1