Results 1 to 18 of 18
Thread: Code for EvenCount
- 08-21-2012, 02:14 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
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:
Why do i have so many errors? :/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)); } }
& how can i get rid of them?
Thanks in advance :)
- 08-21-2012, 03:40 PM #2
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
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.
-
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.
- 08-21-2012, 03:53 PM #4
Re: Code for EvenCount
Moved from Advanced Java. Please confine beginner questions to the New to Java section.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-21-2012, 05:09 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
- 08-22-2012, 02:26 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
- 08-22-2012, 02:30 PM #7
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.
- 08-22-2012, 02:41 PM #8
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
- 08-22-2012, 02:52 PM #9
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
Re: Code for EvenCount
The semi-colon i was talking about was :
it should look likeJava Code:public static void main (String args[]);
Also the return type i was referring to isJava Code:public static void main (String args[])
The String after public is your return type and the String x in the parenthesis is the parameters.Java Code:public String count(String x) {
- 08-22-2012, 02:53 PM #10
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.
- 08-22-2012, 03:11 PM #11
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
- 08-22-2012, 03:12 PM #12
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
Re: Code for EvenCount
i have an error on each of these lines.. fustrating :/ can't get rid of themJava 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)); }
- 08-22-2012, 03:14 PM #13
Re: Code for EvenCount
Please post the full text of the compiler's error messages.i have an errorIf you don't understand my response, don't ignore it, ask a question.
- 08-22-2012, 03:24 PM #14
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
- 08-22-2012, 04:06 PM #15
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.
- 08-22-2012, 04:09 PM #16
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
- 08-22-2012, 04:33 PM #17
Member
- Join Date
- Aug 2012
- Posts
- 12
- Rep Power
- 0
Re: Code for EvenCount
- 08-22-2012, 08:45 PM #18
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
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
When using && or || you need to have the x on both sides to allow it to perform correctly.Java Code:((x >=10) && (x % 2 != 0)) //<-- This says if x is greater then 10 AND x mod 2 is not equal to 0
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks