Basically you need to define a non-static context if you want to use constructors.
Here is a modified version of the code.
Hope this will work properly
public class ClassWork {
public static void main(String[] args) {
MyInteger n1 = new MyInteger(5);
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("n1 is odd? " + n1.isOdd());
MyInteger n2 = new MyInteger(24);
System.out.println("n2 is even? " + n2.isEven());
System.out.println("n2 is prime " + n2.isPrime());
System.out.println("n1 is odd? " + n2.isOdd());
System.out.println("n1 is equal to n2?"+n1.equals(n2));
}
}
// Hint: define a non-static context
class MyInteger {
int number;
MyInteger(int value) {
number = value;
}
public boolean isPrime() {
for (int divisor = 2; divisor <= number / 2; divisor++)
if (number % divisor == 0)
return false;
return true;
}
public boolean isEven() {
if (number % 2 == 0)
return true;
return false;
}
public boolean isOdd() {
if (number % 2==1)
return true;
return false;
}
public boolean equals (MyInteger o) { //hint: define a "MyInteger" object as parameter to compare objects
if(o.number==number)
return true;
return false;
}
}