By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Print method

P: 17
I need modify first program.In second program I try print method findPrime in method main.I have there anywhere mistakes.Please obout help.


First program:


**
*PrimeNumber.java
*
*
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package TestPrime;

/**
*
* @author lm
*/
public class PrimeNumber{



private int numberOf = 1;
private int numberTo = 100;


public PrimeNumber() {

}


public void setOf(int number) {
numberOf = number;
}


public void setTo(int number) {
numberTo = number;
}


private boolean testIsPrime(int testprime) {
boolean prime = true;

for(int k = 3; k < testprime; k+=2) {
if (prime){

for(int j=2;j<=Math.sqrt(testprime);j++) {
if(testprime%k==0){

prime=false;
break;

}
}
}
}

return prime;
}



public String isPrime(int testprime) {
if ( testIsPrime(testprime)) {
return testprime+" is prime";
}else{
return testprime+" isn't prime";
}
}


public void findPrime(){
for(int j=numberOf;j<= numberTo;j++) {
if ( testIsPrime(j)) {
System.out.println(j+"is prime");

}

}
}


public static void main(String[] args) {



PrimeNumber firstprime = new PrimeNumber();


firstprime.numberOf = 10;
firstprime.numberTo = 40;

firstprime.findPrime();

System.out.println(firstprime.isPrime(4272));
System.out.println(firstprime.isPrime(3517));

}
}




Second program:


**
*PrimeNumber.java
*
*
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package TestPrime;

/**
*
* @author lm
*/
public class PrimeNumber{



private int numberOf = 1;
private int numberTo = 100;


public PrimeNumber() {

}


public void setOf(int number) {
numberOf = number;
}


public void setTo(int number) {
numberTo = number;
}


private boolean testIsPrime(int testprime) {
boolean prime = true;

for(int k = 3; k < testprime; k+=2) {
if (prime){

for(int j=2;j<=Math.sqrt(testprime);j++) {
if(testprime%k==0){

prime=false;
break;

}
}
}
}

return prime;
}



public String isPrime(int testprime) {
if ( testIsPrime(testprime)) {
return testprime+" is prime";
}else{
return testprime+" isn't prime";
}
}


public int findPrime(){
for(int j=numberOf;j<= numberTo;j++) {
if ( testIsPrime(j)) {
return j;

}

}
}


public static void main(String[] args) {



PrimeNumber firstprime = new PrimeNumber();


firstprime.numberOf = 10;
firstprime.numberTo = 40;

System.out.println(firstprime.findPrime (j));
System.out.println(firstprime.isPrime(4272));
System.out.println(firstprime.isPrime(3517));

}
}
Nov 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: 17
Please help me somebody.I want all method print in main.I don't know haw to do it.
Nov 27 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.