Design a class that tells whether a number is prime or not
My homework is to Design a class named MyInteger with the following conditions:
An int data field named value that stores the int value of an integer.
A constructor that creates a MyInteger object for the specified int value.
A get method that returns the int value.
A method, isPrime() that returns true if the value is a prime number. See -section 4.10 of the text for Java code that detects prime numbers (this may vary depending on the edition you have).
A static, isPrime(MyInteger) that returns true if the value is a prime number. Note that this method takes an object reference variable (rather than the value) as a parameter.
My problems come up in the static boolean isPrime method, stating that "/" and "%" are undefined for arguement type and in the main method in my if statement: isPrime() == true. It says to change it to static, but i already have a static boolean isPrime method and I'm supposed to have two isPrime methods according to my conditions. Thank you if you are able to help.
public class MyInteger {
public MyInteger(int value){
}
public static int getValue(){
int value = 997;
return value;
}
public boolean isPrime(){
int value = 997;
for (int i=2; i<=value/2; i++){
if(value % i == 0) {
return false;
}
}
return true;
}
public static boolean isPrime(MyInteger value){
for(int i=2; i<=value/2; i++){
if(value%i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
MyInteger value = new MyInteger(MyInteger.getValue());
if (isPrime()==true && isPrime(value)==true){
System.out.println("Testiwng Instance method, is Prime");
System.out.println("isPrime: " + value + " is prime");
System.out.println("--------------------------------");
System.out.println("Testing Class method (That takes a reference variable) is Prime");
System.out.println("isPrime: " + value + " is prime");
}
else{
System.out.println("Testiwng Instance method, is Prime");
System.out.println("isPrime: " + value + " is not prime");
System.out.println("--------------------------------");
System.out.println("Testing Class method (That takes a reference variable) is Prime");
System.out.println("isPrime: " + value + " is not prime");
}
}
}
You don't have to go till the half of the number to check whether it is prime. You can have a loop that checks only the numbers from 2 to the square root of your number. See this - StackOverflow question about checking prime numbers
I believe you need something like this:
public class Main {
public static void main(String[] args) throws IOException {
Scanner inp = new Scanner(System.in);
int someValue = inp.nextInt();
MyInteger myInt = new MyInteger(someValue);
System.out.println("Testing instance method:");
System.out.println(myInt.isPrime());
System.out.println("Testing static method:");
System.out.println(MyInteger.isPrime(myInt));
}
}
class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isPrime() {
int sqrt = (int) Math.sqrt((double)value);
for(int i = 2; i <= sqrt; i++) {
if (value % i == 0) return false;
}
return true;
}
public static boolean isPrime(MyInteger myInt) {
return myInt.isPrime();
}
}
Here's a good reference for testing prime numbers What would be the fastest method to test for primality in Java?
Primarily, change your isPrime() to
boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
Your value variable in the method you mentioned is of the type MyInteger, but you're trying to use it as an int. you probably want to use value.getValue() instead.
链接地址: http://www.djcxy.com/p/86628.html下一篇: 设计一个能够告诉数字是否为素数的类