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

Java Programming: Design a class named Mylnteger. The class contains: An int dat

ID: 3672648 • Letter: J

Question

Java Programming:

Design a class named Mylnteger. The class contains: An int data field named value that stores the int value represented by this object A constructor that creates a Mylnteger object for the specified int value. A get method that returns the int value. Methods isEven is Odd(), and isPrime that return true if the value is even, odd, or prime, respectively. Static methods isEven(Mylnteger), isOdd(Mylnteger), isPrime(Mylnteger) that return true if the specified value is even, odd, or prime, respectively. Methods equals(int) and equals(Mylnteger) that return true if the value in the object is equal to the specified value. A static method parselntfint) that converts a string to an int value. The following Two classes: Mylnteger.java MylntegerDriver.java (use this to test all the methods in Mylnteger class)

Explanation / Answer

MyInteger.java
import java.lang.Integer;

public class MyInteger
{      
       private int value=0;
       public MyInteger(int value)
       {
           this.value=value;
       }
       public int getValue()
       {
           return value;
       }
       public boolean isEven()
       {
           if(value%2==0)
               return true;
           else
               return false;
       }
       public boolean isOdd()
       {
           if(value%2==1)
               return true;
           else
               return false;
       }
       public boolean isPrime()
       {
           for(int i = 2; i < value/2; i++)
           {
               if(value%i==0)
                   return false;
           }
           return true;
       }
       public static boolean isEven(int value)
       {
           if(value%2==0)
               return true;
           else
               return false;
       }
       public static boolean isOdd(int value)
       {
           if(value%2 == 1)
               return true;
           else
               return false;
       }
      
       public static boolean isPrime(int value)
       {
            for (int i = 2; i < value/2; i++)
            {
              if (value % i == 0)
                return false;
            }
            return true;
       }
       public static boolean isEven(MyInteger a)
       {
           if(a.getValue()%2==0)
               return true;
           else
               return false;
       }
       public static boolean isOdd(MyInteger a)
       {
           if(a.getValue()%2 == 1)
               return true;
           else
               return false;
       }
       public static boolean isPrime(MyInteger a)
       {
            for (int i = 2; i < a.getValue()/2; i++)
            {
              if (a.getValue() % i == 0)
                return false;
            }
            return true;
       }
       public static boolean equals(int a, MyInteger b)
       {
           if(a == b.getValue())
               return true;
           else
               return false;
       }
       public static int parseInt(String a)
       {
           return Integer.parseInt(a);
       }
      
}

MyIntegerDriver.java
import java.util.Scanner;

public class MyIntegerDriver {
   public static void main(String[] args)
   {
       Scanner scan = new Scanner(System.in);
       System.out.println("Please enter an integer:");
       int y = scan.nextInt();
       System.out.println("Enter a string of numbers:");
       String s = scan.next();
       MyInteger x = new MyInteger(43);
       System.out.println(x.isEven()+" "+x.isOdd()+" "+x.isPrime());
       System.out.println(MyInteger.equals(y,x));
       System.out.println(MyInteger.isEven(x)+" "+MyInteger.isOdd(x)+" "+MyInteger.isPrime(x));
       System.out.println(MyInteger.parseInt(s));
       System.out.println("isEven " + MyInteger.isEven(y));
       System.out.println("isOdd " + MyInteger.isOdd(y));
       System.out.println("isPrime " + MyInteger.isPrime(y));
   }
}

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