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

Design a class named MyInteger. The class contains: An int data field named valu

ID: 3685980 • Letter: D

Question

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.

Explanation / Answer

MyInteger.java


public class MyInteger{
   int value;
   MyInteger(int newValue) {
   value = newValue;
   }
   public int getValue() {
   return value;
   }  
   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 ..
37
Given number is a prime number

Please enter the value ..
48
Given number is not a prime number

Please enter the value ..
59
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