Beginner Projects
- From: Pseudo Silk Kimono <Pseudo_Silk_Kimono@xxxxxxxxxxxxx>
- Date: Wed, 29 Aug 2007 21:23:06 +0000 (UTC)
Greetings,
As per the instructions I found here
http://mindprod.com/project/projects.html
I have decided to start working my way through some of the Student Projects I have found on this site.
The creator has stated that "...I would appreciate hearing from you if you decide to implement any of these ideas. "
so I am posting my code for the "Sieve of Eratosthenes" Project.
I would be eager to hear of any suggested improvments.
First, my "tester"
public class IsPrime {
public static void main(String [] args) {
MyNumber m1 = new MyNumber(123);
System.out.println(m1.intValue + " " + m1.isPrime + " " + m1.isEven);
}
}
Now for the "MyNumber" class
public class MyNumber {
int intValue;
boolean isPrime;
boolean isEven;
public MyNumber(int intValue) {
this.intValue = intValue;
this.isPrime = primeTester(intValue);
if ( (intValue % 2) == 0 )
this.isEven = true;
else
this.isEven = false;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public boolean isPrime() {
return isPrime;
}
public static boolean primeTester(int candidate) {
if ( candidate == 2) {
return true;
}
if ( candidate == 1 | candidate == 0) {
return false;
}
int y = 2;
while ( y < candidate ) {
if ( (candidate % y) == 0 )
return false;
y++;
}
return true;
}
}
One should be able to copy and paste these classes into an IDE or a text editor.
I use Eclipse as my IDE, but it should work to simply compile.
--
What were you? A jock or a brain?" http://blinkynet.net/comp/uip5.html
"I was a ghost." PSK - RLU 452647
Warrick Brown and Gil Grissom (Bully for You)
.
- Follow-Ups:
- Re: Beginner Projects
- From: Roedy Green
- Re: Beginner Projects
- From: Roedy Green
- Re: Beginner Projects
- From: Lew
- Re: Beginner Projects
- Prev by Date: Re: Adding SCROLLBARS to a JTextArea
- Next by Date: Re: Adding SCROLLBARS to a JTextArea
- Previous by thread: Re: OT. Amusing Variant on Nigeria Scam Spam
- Next by thread: Re: Beginner Projects
- Index(es):
Relevant Pages
|