Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 of 1 Design a class named MyInteger. The class contains: An int data

ID: 3704149 • Letter: Q

Question

Question 1 of 1

Design a class named MyInteger. The class contains:

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.

Implement the class and write a program that tests all methods in the class. Your output should look like the following:

Testing Instance method, is Prime

isPrime: 997 is prime

-----------------------------------

Testing Class method (that takes a reference variable)is Prime

isPrime: 997 is prime

Testing Instance method, is Prime

isPrime: 998 is not prime

-----------------------------------

Testing Class method (that takes a reference variable)is Prime

isPrime: 998 is not prime

MY PROGRAM (My problem is the instance static method isPrime(MyInteger) ... it says The operator < is undefined for the argument type(s) int, MyInteger

MyInteger valuenew MyInteger); System.out.println (MyInteger.isPrime(value)); //tried to use scanner to in //System.out.print("Enter Number to Test for Instance method: ") //int double1-myInput.nextInt(O; System.out.println(value.isPrime()); class MyInteger int value997; MyInteger) this.value value; int getValue) return value; boolean isPrime() for(int i-2;2*ikvalue;i++) if(value^i--0) { System.out.println(value return false; "is not prime using Instance method"); System.out.println(value "is prime using Instance method"); return true; static boolean isPrime(MyInteger value) for(int i-valueti++)f if (value-0) [ System.out.println(value "is not prime"); return false; System.out.println(value "is prime"): return true;

Explanation / Answer

MyInteger.java

public class MyInteger{

int value;

MyInteger(int newValue) {

value = newValue;

}

public int getValue() {

return value;

}

public boolean isPrime(int num) {

for (int f = 2; f <= num / 2; f++) {

if (num % f == 0) {

return false;

}

  

}

return true;

}

public static boolean isPrime(MyInteger obj) {

int n = obj.getValue();

for (int f = 2; f < n / 2; f++) {

if (n % f == 0) {

return false;

}   

}

return true;

}   

public static void main(String[] args) {

// TODO Auto-generated method stub

java.util.Scanner in = new java.util.Scanner(System.in);

System.out.println("Please enter the value ..");

int value = in.nextInt();

MyInteger obj = new MyInteger(value);

boolean status = MyInteger.isPrime(obj);

if(status == true)

System.out.println("Given number is a prime number");

else

System.out.println("Given number is not a prime number");

}   

}

Output:

Please enter the value ..
997
Given number is a prime number

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote