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. The class contains: ? An i

ID: 3550793 • Letter: #

Question

(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 get method that returns the int value.

? The methods isEven(), isOdd(), and isPrime() that return true if the

value in this object is even, odd, or prime, respectively.

? The static methods isEven(int), isOdd(int), and isPrime(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 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 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.util.*;
//(The MyInteger class) Design a class named MyInteger. The class contains:
class MyInteger
{
// An int data field named value that stores the int value represented by this object.
private int value;
// A constructor that creates a MyInteger object for the specified int value.
public MyInteger(int value)
{
this.value = value;
}
// A get method that returns the int value.
public int getValue()
{
return value;
}
// The methods isEven(), isOdd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively.
public boolean isEven()
{
return (value%2==0);
}
public boolean isOdd()
{
return (value%2!=0);
}
public boolean isPrime()
{
if(value == 2) return true;
for(int i=2; i*i<=value; i++)
{
if(value%i==0) return false;
}
return true;
}
// The static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
public static boolean isEven(int val)
{
return (val%2==0);
}
public static boolean isOdd(int val)
{
return (val%2!=0);
}
public static boolean isPrime(int val)
{
if(val == 2) return true;
for(int i=2; i*i<=val; i++)
{
if(val%i==0) return false;
}
return true;
}
// The static methods isEven(MyInteger), isOdd(MyInteger), and
//isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
public static boolean isEven(MyInteger M)
{
return M.isEven();
}
public static boolean isOdd(MyInteger M)
{
return M.isOdd();
}
public static boolean isPrime(MyInteger M)
{
return M.isPrime();
}
// The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value.
public boolean equals(int val)
{
return value==val;
}
public boolean equals(MyInteger M)
{
return value==M.value;
}
// A static method parseInt(char[]) that converts an array of numeric characters to an int value.
public static int parseInt(char[] val)
{
int num = 0;
for(int i=0; i<val.length; i++)
num = 10*num + (int)(val[i]-'0');
return num;
}
// A static method parseInt(String) that converts a string into an int value.
public static int parseInt(String val)
{
int num = 0;
for(int i=0; i<val.length(); i++)
num = 10*num + (int)(val.charAt(i)-'0');
return num;
}
}

public class Tester
{
public static void main(String[] args)
{
// TESTING CONSTRUCTOR.
MyInteger new_M = new MyInteger(5);
//TESTING getValue() method
System.out.println("Value stored in My Integer is "+ new_M.getValue());
//TESTING isEven() method
System.out.println("My Integer " + new_M.getValue() + " is " + (new_M.isEven()?" Even.":" Odd."));
//TESTING isOdd() method
System.out.println("My Integer "+ new_M.getValue() + " is "+ (new_M.isOdd()?" Odd.":" Even."));
//TESTING isPrime() method
System.out.println("My Integer "+ new_M.getValue() + " is "+ (new_M.isPrime()?" Prime.":" Not Prime."));
//TESTING isEven(66) method
System.out.println("My Integer 66 is "+ (new_M.isEven(66)?" Even.":" Odd."));
//TESTING isOdd(57) method
System.out.println("My Integer 57 is "+ (new_M.isOdd(57)?" Odd.":" Even."));
//TESTING isPrime(9) method
System.out.println("My Integer 9 is "+ (new_M.isPrime(9)?" Prime.":" Not Prime."));
MyInteger another_M = new MyInteger(77);
//TESTING isEven(MyInteger) method
System.out.println("My Integer " + another_M.getValue() + " is " + (new_M.isEven(another_M)?" Even.":" Odd."));
//TESTING isOdd(MyInteger) method
System.out.println("My Integer "+ another_M.getValue() + " is "+ (new_M.isOdd(another_M)?" Odd.":" Even."));
//TESTING isPrime(MyInteger) method
System.out.println("My Integer "+ another_M.getValue() + " is "+ (new_M.isPrime(another_M)?" Prime.":" Not Prime."));
//TESTING equals(int) method
System.out.println("My Integer " + new_M.getValue() + " is " + (new_M.equals(77)?" equal to 77.":" not equal to 77."));
//TESTING equals(MyInteger) method
System.out.println("My Integer " + new_M.getValue() + " is " + (new_M.equals(another_M)?" equal to ":" not equal to ")+ another_M.getValue());
//TESTING parseInt(char[]) method
char[] array = {'1','2','3'};
System.out.println("Value stored in My Integer is "+ another_M.parseInt(array));
//TESTING eparseInt(String) method
System.out.println("Value stored in My Integer is "+ another_M.parseInt("4567"));
}
}