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

Please help

P: 17
I have error in System.out.println(firstprime.findPrime ()); I don't know how
it repair.Please hepl me with it.


**
*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 String findPrime(){
for(int j=numberOf;j<= numberTo;j++) {
if ( testIsPrime(j)) {
return j+"is primenumber";

}

}
}


public static void main(String[] args) {



PrimeNumber firstprime = new PrimeNumber();


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

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

}
}
Nov 28 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
I have error in System.out.println(firstprime.findPrime ()); I don't know how
it repair.Please hepl me with it.


**
*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 String findPrime(){
for(int j=numberOf;j<= numberTo;j++) {
if ( testIsPrime(j)) {
return j+"is primenumber";

}

}
}


public static void main(String[] args) {



PrimeNumber firstprime = new PrimeNumber();


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

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

}
}


Expand|Select|Wrap|Line Numbers
  1.  
  2. /**
  3. *PrimeNumber.java
  4. *
  5. *
  6. *
  7. * To change this template, choose Tools | Template Manager
  8. * and open the template in the editor.
  9. */
  10. //package TestPrime;
  11. /**
  12. *
  13. * @author lm
  14. */
  15. public class PrimeNumber{
  16.  
  17. private int numberOf = 1;
  18. private int numberTo = 100;
  19.  
  20. public PrimeNumber() {
  21. }
  22.  
  23. public void setOf(int number) {
  24. numberOf = number;
  25. }
  26.  
  27. public void setTo(int number) {
  28. numberTo = number;
  29. }
  30.  
  31. private boolean testIsPrime(int testprime) {
  32. boolean prime = true;
  33. for(int k = 3; k < testprime; k++) {//changed here
  34. if (prime){
  35. for(int j=2;j<=Math.sqrt(testprime);j++) {
  36. if(testprime%k==0){
  37. prime=false;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. return prime;
  44. }
  45.  
  46. public String isPrime(int testprime) {
  47. if ( testIsPrime(testprime)) {
  48. return testprime+" is prime";
  49. }else{
  50. return testprime+" isn't prime";
  51. }
  52. }
  53.  
  54. public String findPrime(){
  55.  String s = "";
  56.  for(int j=numberOf;j<= numberTo;j++) {
  57.   if ( testIsPrime(j)) {
  58.    s = s + j+"is primenumber\n";
  59.   }
  60.  }
  61.  return s;
  62.  
  63. }
  64.  
  65. public static void main(String[] args) {
  66.  
  67. PrimeNumber firstprime = new PrimeNumber();
  68.  
  69. firstprime.numberOf = 10;
  70. firstprime.numberTo = 40;
  71. System.out.println(firstprime.findPrime ());
  72. System.out.println(firstprime.isPrime(4272));
  73. System.out.println(firstprime.isPrime(3517));
  74. }
  75. }
  76.  
Like this?
Nov 28 '06 #2

P: 17
Thank for your help
Nov 28 '06 #3

Post your reply

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