Design a class named MyInteger The class contains An int in

Design a class named MyInteger. The class contains: • An int instance variable named value that stores the int value represented by the object. • A constructor that creates a MyInteger object for the specified int value. • A get method that returns the int value. • Static methods isEven(int), isOdd(int) and isPrime(int) that return true if the specified value is odd, even, or prime, respectively, and false otherwise. • Methods isEven(), isOdd(), and isPrime() that return true if the value in this object is even, odd or prime, respectively, and false otherwise. • Static methods isEven(MyInteger), isOdd(MyInteger) and isPrime(MyInteger) that return true if the value of the specified object is even, odd, or prime, respectively, and false otherwise. • Method equals(int) that returns true if the value in this object is equal to the specified integer. • Method equals(MyInteger) that return true if the value in this object is equal to the value in another object. • Static method parseInt(char[]) that converts an array of numeric characters to an int value. • Static method parseInt(String) that converts a string into an int value.

Exercise 3: Design a class named MyInteger. The class contains An int instance variable named value that stores the int value represented by the object. A constructor that creates a MyInteger object for the specified int value. A get method that returns the int value prime, respectively, and false otherwise. Methods isEven isodd0, and isPrime0 that return true if the value in this object is even, odd or prime respectively, and false otherwise Static methods isEven(MyInteger), isOdd(MyInteger and isPrime(MyInteger) that return true if the value of the specified object is even, odd, or prime, respectively, and false otherwise. Method equals (int) that returns true if the value in this object is equal to the specified integer. Method equals(MyInteger that return true if the value in this object is equal to the value in another object. Static method parselnt(charl] that converts an array ofnumeric characters to an int value Static method parselnt(String) that converts a string into an int value Note: if c is a character from 0 to 90, then (int)c-48 converts c to an integer value. You may assume that the user enters only digits Here\'s a demo class that tests the above methods and the output. Change the values and run the code at least three times for different input sets public class MyIntegerDemo public static void main (String args) My Integer n1 new My Integer (5) System out. println (\"nl is nl. getValue out. println (\"nl is even nl. isEven System System out. println (\"nl is odd? n isodd System.out.println (\"nl is prime 1 is Prime My Integer. is Prime (15) out.println (\"15 is prime System char chars 3 \'9\' out. println (My Integer parse Int (chars) System String s 9786 System.out.println (My Integer parselnt (s)) My Integer n2 new My Integer (24) System out. println (\"n2 is n 2. getvalue System out. println (\"n2 is odd? n2. isodd System out. println (\"45 is odd? My Integer s Odd (45) out. println (\"nl is equal to n2? n equals (n2) System System.out.println (\"nl is equal to 5? n equals (5) GRASP exec ea My Integer Demo Java n1 is 5 n 1 is even fals n 1 is odd true nl is prime true 15 is prime false 3539

Solution

PROGRAM CODE:

MyInteger.java


public class MyInteger {
   private int value;
  
   public MyInteger(int value)
   {
       this.value = value;
   }
  
   public int getValue()
   {
       return this.value;
   }
  
   public static boolean isEven(int num)
   {
       if(num%2 == 0)
           return true;
       else return false;
   }
  
   public static boolean isOdd(int num)
   {
       if(num%2 == 0)
           return false;
       else return true;
   }
  
   public static boolean isPrime(int num)
   {
       for(int i=2; i<=num/2; i++)
       {
           if(num%i==0)
               return false;
       }
       return true;
   }
  
   public boolean isEven()
   {
       return isEven(getValue());
   }
  
   public boolean isOdd()
   {
       return isOdd(getValue());
   }
  
   public boolean isPrime()
   {
       return isPrime(getValue());
   }
  
   public static boolean isEven(MyInteger integer)
   {
       return isEven(integer.getValue());
   }
  
   public static boolean isOdd(MyInteger integer)
   {
       return isOdd(integer.getValue());
   }
  
   public static boolean isPrime(MyInteger integer)
   {
       return isPrime(integer.getValue());
   }
  
   public boolean equals(MyInteger obj) {
      
       if(this.getValue() == obj.getValue())
           return true;
       else return false;
   }
  
   public boolean equals(int integer) {
      
       if(this.getValue() == integer)
           return true;
       else return false;
   }
  
   public static int parseInt(String values)
   {
       return Integer.valueOf(values);
   }
  
   public static int parseInt(char[] values)
   {
       String stringValue = \"\";
       for(int i=0; i<values.length; i++)
           stringValue += values[i];
       return parseInt(stringValue);
   }
}

MyIntegerDemo.java


public class MyIntegerDemo {

   /**
   * @param args
   */
   public static void main(String[] args) {
       MyInteger n1 = new MyInteger(5);
       System.out.println(\"n1 is \" + n1.getValue());
       System.out.println(\"n1 is even? \" + n1.isEven());
       System.out.println(\"n1 is odd? \" + n1.isOdd());
       System.out.println(\"n1 is prime? \" + n1.isPrime());
       System.out.println(\"15 is prime? \" + MyInteger.isPrime(15));
       char chars[] = {\'3\', \'5\', \'3\', \'9\'};
       System.out.println(MyInteger.parseInt(chars));
       String s = \"9786\";
       System.out.println(MyInteger.parseInt(s));
       MyInteger n2 = new MyInteger(24);
       System.out.println(\"n2 is \" + n2.getValue());
       System.out.println(\"n2 is odd? \" + n2.isOdd());
       System.out.println(\"45 is odd? \" + MyInteger.isOdd(45));
       System.out.println(\"n1 is equal to n2? \" + n1.equals(n2));
       System.out.println(\"n1 is equal to 5? \" + n1.equals(5));
   }

}

OUTPUT:

n1 is 5
n1 is even? false
n1 is odd? true
n1 is prime? true
15 is prime? false
3539
9786
n2 is 24
n2 is odd? false
45 is odd? true
n1 is equal to n2? false
n1 is equal to 5? true

Design a class named MyInteger. The class contains: • An int instance variable named value that stores the int value represented by the object. • A constructor
Design a class named MyInteger. The class contains: • An int instance variable named value that stores the int value represented by the object. • A constructor
Design a class named MyInteger. The class contains: • An int instance variable named value that stores the int value represented by the object. • A constructor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site