Results 1 to 18 of 18
  1. #1
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Code for EvenCount

    Create a class called EvenCount that contains a method called count that takes an integer as an argument and returns an integer (this method should not be declared static). The count method should use a recursive approach to find the amount of even digits in the integer passed into the method, and return this number. For example, if the count method was called with the input value 783312 as an argument it should return 2 (there are two even numbers in the input value). Hint: in Java a single digit that is even will produce a result of 0 when the remainder operator (the percent sign) is used to find its remainder when divided by two. For example 2 % 2 and 6 % 2 will return 0, whereas 3 % 2 and 7 % 2 will produce a result of 1.

    Java code:
    Java Code:
     package mocktest;
    
    public class EvenCount {
    
        public String count(String x) {
    
    	if (x < 10 && x % 2 == 0)
    	    return 1; 
    	else if (x < 10 && % 2 !0)
    	    return 0;
    	else if ((x >=10 && (x / 10 ) % 2 == 0)
    	    return 1 + count(x/10) + 1;
    		 else
    		     return count (x/10);
        }
        public static void main (String args[]); {
    
    	EvenCount c = new EvenCount ();
    	System.out.println(x.count(73221));
        }
    }
    Why do i have so many errors? :/
    & how can i get rid of them?
    Thanks in advance :)

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Code for EvenCount

    Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Code for EvenCount

    Please don't post the same question on multiple forums without notifying us. This risks our wasting our time answering questions that have already been answered elsewhere. All we ask is that you provide links to your other questions, and that's not asking too much. Please help us out here. Doing this will also help prevent you from going on user's do-not-help lists.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Code for EvenCount

    Moved from Advanced Java. Please confine beginner questions to the New to Java section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by Fubarable View Post
    Please don't post the same question on multiple forums without notifying us. This risks our wasting our time answering questions that have already been answered elsewhere. All we ask is that you provide links to your other questions, and that's not asking too much. Please help us out here. Doing this will also help prevent you from going on user's do-not-help lists.
    Sorry, im new so dont really know much on how to use this and how to provide links

  6. #6
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by jhuber151 View Post
    Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.
    Im confused.. i dont know which semi-colon to take off. I tried all but still an error. and which return type is declared as String?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Code for EvenCount

    Please post the full text of the compiler's error messages.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by Norm View Post
    Please post the full text of the compiler's error messages.
    I cant run it because it says it doesnt have a main method :/

  9. #9
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Code for EvenCount

    The semi-colon i was talking about was :
    Java Code:
     public static void main (String args[]);
    it should look like
    Java Code:
     public static void main (String args[])
    Also the return type i was referring to is
    Java Code:
    public String count(String x) {
    The String after public is your return type and the String x in the parenthesis is the parameters.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Code for EvenCount

    You do not need a main() method to compile a program.

    Please compile the program and copy the full text of the error messages here.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by Norm View Post
    You do not need a main() method to compile a program.

    Please compile the program and copy the full text of the error messages here.
    I am using netbeans therefore i do need main() method

  12. #12
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by jhuber151 View Post
    The semi-colon i was talking about was :
    Java Code:
     public static void main (String args[]);
    it should look like
    Java Code:
     public static void main (String args[])
    Also the return type i was referring to is
    Java Code:
    public String count(String x) {
    The String after public is your return type and the String x in the parenthesis is the parameters.
    Java Code:
    	if (x < 10 && x % 2 == 0)
    	    return 1;
    	else if (x < 10 && % 2 !0)
    	    return 0;
    	else if ((x >=10 && (x / 10 ) % 2 == 0)
    	    return 1 + count(x/10) + 1;
    		 else
    		     return count (x/10);
        }
        public static void main (String args[]) {
    
    	EvenCount c = new EvenCount ();
    	System.out.println(c.count(73221));
    }
    i have an error on each of these lines.. fustrating :/ can't get rid of them

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Code for EvenCount

    i have an error
    Please post the full text of the compiler's error messages.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by Norm View Post
    Please post the full text of the compiler's error messages.
    I cannot run it because i am using netbeans. Can you please just paste it on netbeans and see the syntax error. There are to many to describe. thanks!

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Code for EvenCount

    Sorry, I don't use an IDE.
    Try using the javac command with the source in a command prompt window.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by jhuber151 View Post
    The semi-colon i was talking about was :
    Java Code:
     public static void main (String args[]);
    it should look like
    Java Code:
     public static void main (String args[])
    Also the return type i was referring to is
    Java Code:
    public String count(String x) {
    The String after public is your return type and the String x in the parenthesis is the parameters.
    Quote Originally Posted by jhuber151 View Post
    Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.
    Quote Originally Posted by Mehwish-S View Post
    Java Code:
    	if (x < 10 && x % 2 == 0)
    	    return 1;
    	else if (x < 10 && % 2 !0)
    	    return 0;
    	else if ((x >=10 && (x / 10 ) % 2 == 0)
    	    return 1 + count(x/10) + 1;
    		 else
    		     return count (x/10);
        }
        public static void main (String args[]) {
    
    	EvenCount c = new EvenCount ();
    	System.out.println(c.count(73221));
    }
    i have an error on each of these lines.. fustrating :/ can't get rid of them
    In my previous posts I gave you hints to solve each of those syntax errors

  17. #17
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Code for EvenCount

    Quote Originally Posted by jhuber151 View Post
    In my previous posts I gave you hints to solve each of those syntax errors
    Thanks! i got most of the errors sorted but what does it mean by class,interface or enum expected?
    Lastly, on this line
    Java Code:
     else if (c < 10 && % 2 !0)
    it says illegal start of expression
    ')' and ';' expected
    not a statement

    what does this mean?

  18. #18
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Code for EvenCount

    For this line you technically what you are doing is first seeing if c is less then 10, but then after that the second half of the line is not complete
    Java Code:
    ((x >=10) && (x % 2 != 0)) //<-- This says if x is greater then 10 AND x mod 2 is not equal to  0
    When using && or || you need to have the x on both sides to allow it to perform correctly.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 10:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •