Results 1 to 7 of 7
  1. #1
    mxr808 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default First attempt at Java.

    Hello guys! I've only started learning Java in the past couple weeks and have no real programming experience. I've been reading a book called Big Java which is helping me along really well. Anyhow I just wrote my first program which is basically just a little bit of code that finds prime numbers between 1 and n.

    I managed to get it to do what I wanted it to, I was just wanting to know whether people could give me any advice on whether this is good coding layout and whether it's as efficient as it can be. Just any criticism is welcome. Thanks!

    Java Code:
    public class PrimeNumberFinderInt
    {
    	public static void main(String[] args)
    	{
    		int currentNumber = 1;
    		int divisibleBy = 2; 
    		for(int currentNumberx = currentNumber; currentNumberx < 30; currentNumberx += 1)
    			{
    				for(int divisibleByFor = divisibleBy; divisibleByFor < currentNumberx; divisibleByFor += 1)
    				{
    					int a = currentNumberx%divisibleByFor;
    					if(a > 0 && divisibleByFor < currentNumberx - 1)
    					{
    						continue;
    					}
    					else if(a == 0) 
    					{
    						System.out.println("The number " + currentNumberx + " was divided by " + divisibleByFor); 
    						break;
    					}
    					else
    					{
    						System.out.println(currentNumberx + " is a prime number!");
    					}
    				}
    			}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    I personally find that if( ... ) else if( ... ) else construct very cryptic. Why not make a boolean method isPrime( ... ) that returns true if the parameter passed in is a prime number and false otherwise. Also note that if a number has divisors a lot of them are smaller or equal than the root of that number; in other words: if it has a divisor larger than the root of the number it also has a divisor smaller or equal than that root; consequently you only have to search for numbers smaller or equal to that root. Also also note that all primes (except 2 and 3) are multiples of 6 plus or minus one.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mxr808 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I personally find that if( ... ) else if( ... ) else construct very cryptic. Why not make a boolean method isPrime( ... ) that returns true if the parameter passed in is a prime number and false otherwise. Also note that if a number has divisors a lot of them are smaller or equal than the root of that number; in other words: if it has a divisor larger than the root of the number it also has a divisor smaller or equal than that root; consequently you only have to search for numbers smaller or equal to that root. Also also note that all primes (except 2 and 3) are multiples of 6 plus or minus one.

    kind regards,

    Jos
    Thanks for the constructive advice. I will try to use the boolean method you suggested. I'm also not sure whether I'm using break or continue correctly and so I kind of just threw them in through a trial and error process.

  4. #4
    bmeu is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    could set currentNumber as final too, as its only declared once anyway.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    wonders how the original code actually compiled

  6. #6
    cejay is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Your code is pretty dang good for a beginner. All i ever used to use were if's ha ha.

    If i were you, i would take lots of notes. it saves a lot of time down the road when you forget what a method does and have to go back and re-read through it. It only adds unneeded frustration.

    good job =)

    edit:

    just remember the continue statement just stops the loop at where the continue was stated, and starts the loop over again. The break statement kills the loop entirely. trial and error is your best teacher with java programming =) its fun to go on adventures.
    Last edited by cejay; 11-27-2010 at 09:57 AM.

  7. #7
    mxr808 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by cejay View Post
    Your code is pretty dang good for a beginner. All i ever used to use were if's ha ha.

    If i were you, i would take lots of notes. it saves a lot of time down the road when you forget what a method does and have to go back and re-read through it. It only adds unneeded frustration.

    good job =)

    edit:

    just remember the continue statement just stops the loop at where the continue was stated, and starts the loop over again. The break statement kills the loop entirely. trial and error is your best teacher with java programming =) its fun to go on adventures.
    I wasn't sure whether putting the continue and break within the if and else checks would mean the the FOR loop is effected by the continue and break. I obviously don't want it to keep checking for numbers if it's already found one it's divisible by.

    This book has been trying to drill me to put comments in but I figure for tests such as this which I am highly unlikely to use again then there's no real point to it.

    Thanks for the comments!

Similar Threads

  1. Leave thread yield or attempt join... or something else?
    By JavaCoder in forum Advanced Java
    Replies: 1
    Last Post: 08-11-2010, 07:35 PM
  2. First TicTacToe Attempt
    By Pyrexkidd in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 10:32 PM
  3. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 09:39 AM
  4. my Quicksort attempt has failed
    By Jeremy8 in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 03:56 AM
  5. Replies: 0
    Last Post: 02-18-2009, 06:20 AM

Posting Permissions

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