Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Odd and Even Numbers

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Question [SOLVED] Odd and Even Numbers

    I am trying to create an "algorithm" (what I have now really isn't), I just can't seem to wrap my head around the concept, or on how to do this.. Here is the monstrosity I have created so far:

    package org.buchanan.math;
     
    import java.util.ArrayList;
    import java.util.List;
     
    public class Numbers {
     
    	private List<Integer> oddNumbers = new ArrayList<Integer>();
    	private List<Integer> evenNumbers = new ArrayList<Integer>();
     
    	public void populateLists() {
    		for (int j = 0; j < Integer.MAX_VALUE; j += 1) {
    			this.oddNumbers.add(j);
    			System.out.println(j);
    		}
    		System.out.println("Odd numbers added!");
    		for (int i = 0; i < Integer.MAX_VALUE; i += 2) {
    			this.evenNumbers.add(i);
    		}
    		System.out.println("Even numbers added!");
    	}
     
    	public boolean isOddNumber(int n) {
    		return this.oddNumbers.contains(n) ? true : false;
    	}
     
    	public boolean isEvenNumber(int n) {
    		return this.evenNumbers.contains(n) ? true : false;
    	}
    }

    As you see, this destroys my memory, is slow as all get out, and not efficient in any way. Anybody have any suggestions on how to make this? Maybe even somewhere easier to start learning things like this?
    Last edited by tyb97; September 30th, 2012 at 04:31 PM. Reason: Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Odd and Even Numbers

    Try using the Modulo operator. It will save you a lot of time and memory.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    tyb97 (September 30th, 2012)

  4. #3
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Odd and Even Numbers

    Quote Originally Posted by helloworld922 View Post
    Try using the Modulo operator. It will save you a lot of time and memory.
    Ahh, thanks a ton. I still don't fully understand them, but in time I will. I was able to reduce my code from the above to this:

    package org.buchanan.math;
     
    public class Numbers {
     
    	public boolean isOddNumber(int n) {
    		return n % 2 != 0 ? true : false;
    	}
     
    	public boolean isEvenNumber(int n) {
    		return n % 2 == 0 ? true : false;
    	}
    }

    Which really helps a ton, and save a lot of memory.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Odd and Even Numbers

    return n % 2 != 0 ? true : false;
    You could return the results of the != directly and not need to use the ternary statement.
    return n % 2 != 0;
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    tyb97 (September 30th, 2012)

  7. #5
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Norm View Post
    You could return the results of the != directly and not need to use the ternary statement.
    return n % 2 != 0;
    I see. Thank you for the kind insight.

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. [SOLVED] Scale Numbers from 0.0f to 1.0f
    By techwiz24 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 09:50 PM
  3. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 12:45 AM
  4. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM