Beginner Projects



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)
.



Relevant Pages

  • Re: Beginner Projects
    ... public static void main{ ... public MyNumber(int intValue) { ... public boolean isPrime() { ...
    (comp.lang.java.help)
  • Re: Integer / Long comparison
    ... In JUnit I want to compare an Integer with a Long, ... Assuming you mean't intValue() above. ... but seems a bit verbose with two "casts". ... If possible, use int and long to start with, rather than Integer and Long. ...
    (comp.lang.java.programmer)