So the code I am working with is supposed to check if a number is prime and then I need to print out the result of, whether it is prime? From my instance and class method. I am having trouble because I feel like I set up everything correctly, but I am getting no results when I run the program. I will take any advice. Be easy on me this is my first year programming.
import java.io.IOException;
import java.util.Scanner;
public class Assignment4 {
public static void main(String[] args) throws IOException {
Scanner myInput = new Scanner(System.in);
int someValue = myInput.nextInt();
MyInteger myInt = new MyInteger(someValue);
System.out.println("Testing instance method:");
System.out.println(myInt.isPrime());
System.out.println("Testing class 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();
}
}