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

10.3 (The MyInteger class) Design a class named MyInteger. The class contains: A

ID: 3585204 • Letter: 1

Question

10.3 (The MyInteger class) Design a class named MyInteger. The class contains: 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 getter method that returns the int value. .The methods isEven0, isOdd0, and isPrime0 that return true if the value in this object is even, odd, or prime, respectively. -The static methods isEven(int), isOdd(int), and is Prime(int) that return true if the specified value is even, odd, or prime, respectively The static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively. The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value. A static method parselnt(char[l) that converts an array of numeric characters to an int value. A static method parselnt(String) that converts a string into an int value. Draw the UML diagram for the class and then implement the class. Write a client program that tests all methods in the class

Explanation / Answer

import java.io.*;
import java.util.*;
import java.lang.*;

class MyInteger {
   private int value;
   public MyInteger(int a){
       value = a;
   }
   public int getValue(){
      return value;
   }
   int parseInt(char[] a){

     int sum = 0;
     int n = a.length - 1;
     for (int i = 0; i<a.length; i++){
          sum = sum + Character.getNumericValue(a[i])* (int)Math.pow(10,n-i);
     }
     return sum;
   }
  
   int parseInt(String a){
    
     char[] c = a.toCharArray();
     int sum = 0;
    
     int n = c.length - 1;
     for (int i = 0; i<c.length; i++){
          sum = sum + Character.getNumericValue(c[i])* (int)Math.pow(10,n-i);
     }
    
     return sum;
   }
  
   public boolean isOdd(){
      if (value % 2 != 0)
         return true;
      else
         return false;
   }
   public boolean isEven(){
      if (value % 2 == 0)
         return true;
      else
         return false;
   }
   public boolean isPrime(){
      for (int i = 2; i<value; i++){
         if (value % i == 0)
            return false;
      }
      return true;
   }
   public boolean isOdd(int a){
      if (a % 2 != 0)
         return true;
      else
         return false;
   }
   public boolean isEven(int a){
      if (a % 2 == 0)
         return true;
      else
         return false;
   }
   public boolean isPrime(int a){
      for (int i = 2; i<a; i++){
         if (a % i == 0)
            return false;
      }
      return true;
   }
   public boolean isOdd(MyInteger a){
      int b = a.getValue();
      if (b % 2 != 0)
         return true;
      else
         return false;
   }
   public boolean isEven(MyInteger a){
      int b = a.getValue();
      if (b % 2 == 0)
         return true;
      else
         return false;
   }
   public boolean isPrime(MyInteger a){
      int b = a.getValue();
      for (int i = 2; i<b; i++){
         if (b % i == 0)
            return false;
      }
      return true;
   }
   public boolean equals(int a){
       if (value == a)
          return true;
       else
          return false;
   }
   public boolean equals(MyInteger a){
       if (value == a.getValue())
          return true;
       else
          return false;
   }

}

public class DemoInteger{

   public static void main(String[] args){
      
        MyInteger a = new MyInteger(5);
        MyInteger e = new MyInteger(7);
        String str = "23";
        char [] b = str.toCharArray();
        int c = a.parseInt(str);
        int d = a.parseInt(b);
        System.out.println(a.isEven());
        System.out.println(a.isOdd());
        System.out.println(a.isPrime());
        System.out.println(a.isEven(2));
        System.out.println(a.isOdd(7));
        System.out.println(a.isPrime(13));
        System.out.println(a.isEven(e));
        System.out.println(a.isOdd(e));
        System.out.println(a.isPrime(e));
        System.out.println(c);
        System.out.println(b);


   }
}

UML Diagram

                 MyInteger
                value : int

                isEven() : boolean
                isOdd() : boolean
                isPrime() : boolean
                isEven(a:int) : boolean
                isOdd(a:int) : boolean
                isPrime(a:int) : boolean
                isEven(a:MyInteger) : boolean
                isOdd(a:MyInteger) : boolean
                isPrime(a:MyInteger) : boolean
                parseInt(a:char[]) :int
                parseInt(a:String) :int
                isEquals(a:int) : boolean
                isEquals(a:Myinteger): boolean
               

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote