The My Integer class Design a class named My Integer The cla

(The My Integer class) Design a class named My Integer. 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 isEven(), is odd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively. The static methods isEven (int), is0dd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. The static methods isEven (MyInteger), isOdd(My Integer), 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[]) 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.

Solution


public class myinteger {
   int value;
   myinteger(){
       value=0;
      
   }
   myinteger(int value){
       setvalue(value);
      
   }
   public void setvalue(int value){
       this.value=value;
   }
   public int getvalue(){
       return value;
   }
   public String tostring(){
       return value+\"\";
   }
   public boolean iseven(){
       if(value%2==0)
           return true;
       else
           return false;
   }
   public boolean isodd(){
       if(value%2!=0)
           return true;
       else
           return false;
   }
   public boolean isprime(){
       int count=0;
       for(int i=1;i<=value;i++){
           if(value%i==0)
               count++;
       }
       if(count==2)
           return true;
       else
           return false;
   }
   public boolean iseven(int val){
       value=val;
       if(value%2==0)
           return true;
       else
           return false;
   }
   public boolean isodd(int val){
       value=val;
       if(value%2!=0)
           return true;
       else
           return false;
   }
   public boolean isprime(int val){
       value=val;
       int count=0;
       for(int i=1;i<=value;i++){
           if(value%i==0)
               count++;
       }
       if(count==2)
           return true;
       else
           return false;
   }
   public boolean iseven(myinteger val){
       int z=val.getvalue();
       if(z%2==0)
           return true;
       else
           return false;
   }
   public boolean isodd(myinteger val){
       int z=val.getvalue();
       if(z%2!=0)
           return true;
       else
           return false;
   }
   public boolean isprime(myinteger val){
       int z=val.getvalue();
       int count=0;
       for(int i=1;i<=z;i++){
           if(z%i==0)
               count++;
       }
       if(count==2)
           return true;
       else
           return false;
   }
   public boolean isequal(int data){
       if(data==value)
           return true;
       else
           return false;
      
   }
   public boolean isequal(myinteger data){
       if(data.value==value)
           return true;
       else
           return false;
      
   }
   public void parseInt(String da){
       int convertotitn=Integer.parseInt(da);
       System.out.println(\"converted to int is:\"+convertotitn);
      
   }
   public void parseInt(char c[]){
       int cd;
       for(int i=0;i<c.length;i++){
           cd=(int)c[i];
       System.out.println(\"char to int is\"+cd);
       }
      
   }

}
main class
public class testmaininteger {
   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       char c[]={\'s\',\'w\',\'a\'};
       myinteger m=new myinteger();
       System.out.println(\"value of object m is\"+m.tostring());
       myinteger m1=new myinteger(7);
       System.out.println(\"value of object m1 is:\"+m1.tostring());
       System.out.println(\"is even :\"+m1.iseven());
       System.out.println(\"is odd:\"+m1.isodd());
       System.out.println(\"is prime:\"+m1.isprime());
       System.out.println(\"is even :\"+m1.iseven(5));
       myinteger m2=new myinteger(11);
       System.out.println(\"is odd:\"+m1.isodd(m2));
       System.out.println(\"is equal:\"+m1.isequal(22));
   m1.parseInt(\"101\");
   m1.parseInt(c);
   }
}

output:

value of object m is0
value of object m1 is:7
is even :false
is odd:true
is prime:true
is even :false
is odd:true
is equal:false
converted to int is:101
char to int is115
char to int is119
char to int is97

 (The My Integer class) Design a class named My Integer. The class contains An int data field named value that stores the int value represented by this object.
 (The My Integer class) Design a class named My Integer. The class contains An int data field named value that stores the int value represented by this object.
 (The My Integer class) Design a class named My Integer. The class contains An int data field named value that stores the int value represented by this object.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site