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

(The MyInteger Class) Design a class named MyInteger: An int data field named va

ID: 3767611 • Letter: #

Question

(The MyInteger Class) Design a class named MyInteger:

An int data field named value that stores the int value represented by this object.
A constructor that creates a MyInteger object for the specified int value.
A get method that returns the int value.
Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively.
Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
A static method parseInt(char[]) that converts an array of numeric characters to an int value.
A static method parseInt(String)that converts a string into an int value.

Draw a UML diagram for the class and implement the class. Write a client program that tests all methods in the class. Given the definition of a prime number make sure to instruct the user to enter only positive integers.

Explanation / Answer

public class MyInteger{
   int num;
   MyInteger(int n){
       num = n;
   }
   int getNum(){
       return num;
   }
   boolean isEven(){
       return num % 2 == 0;
   }
   boolean isOdd(){
       return num % 2 == 1;
   }
   boolean isPrime(){
       int count = 0;
       for(int i = 1; i <= num; i++){
           if(count > 2) return false;
       }
       return true;
   }
   static boolean isEven(int n){
       return n % 2 == 0;
   }
   static boolean isOdd(int n){
       return n % 2 == 1;
   }
   static boolean isPrime(int n){
       int count = 0;
       for(int i = 1; i <= n; i++){
           if(count > 2) return false;
       }
       return true;
   }
  
   static boolean isEven(MyInteger n){
       return n.num % 2 == 0;
   }
   static boolean isOdd(MyInteger n){
       return n.num % 2 == 1;
   }
   static boolean isPrime(MyInteger n){
       int count = 0;
       for(int i = 1; i <= n.num; i++){
           if(count > 2) return false;
       }
       return true;
   }
   boolean equals(int x){
       if(num == x) return true;
       return false;
   }
   boolean equals(MyInteger x){
       if(num == x.num) return true;
       return false;
   }
   int parseInt(char[] c){
       int x = 0;
       for(int i = 0; i < c.length; i++){
           x *= 10;
           x = Integer.parseInt(c[i] + "");
       }

return x;
   }
   int parseInt(String s){
       return Integer.parseInt(s);
   }
}