Results 1 to 7 of 7
Thread: First attempt at Java.
- 11-26-2010, 02:04 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
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!"); } } } } }
- 11-26-2010, 02:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-26-2010, 02:30 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
- 11-27-2010, 01:37 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
could set currentNumber as final too, as its only declared once anyway.
- 11-27-2010, 02:20 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
wonders how the original code actually compiled
- 11-27-2010, 08:55 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 10
- Rep Power
- 0
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 08:57 AM.
- 11-27-2010, 09:04 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
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
-
Leave thread yield or attempt join... or something else?
By JavaCoder in forum Advanced JavaReplies: 1Last Post: 08-11-2010, 06:35 PM -
First TicTacToe Attempt
By Pyrexkidd in forum New To JavaReplies: 2Last Post: 07-25-2010, 09:32 PM -
Switching JPanels inside JFrame attempt
By frenk_castle in forum AWT / SwingReplies: 7Last Post: 03-31-2010, 08:39 AM -
my Quicksort attempt has failed
By Jeremy8 in forum New To JavaReplies: 4Last Post: 11-16-2009, 02:56 AM -
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted
By R O C K Y in forum XMLReplies: 0Last Post: 02-18-2009, 05:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks