(Mylnteger.java class) Design a class named Mylnteger. The class contains: - An
ID: 3684812 • Letter: #
Question
(Mylnteger.java class) 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(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. - Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. - Static methods isEven(Mylnteger), isOdd(Mylnteger), and isPrime(Mylnteger) that return true if the specified value is even, odd, or prime, respectively. - Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value. - A static method parseint(String) that converts a string to an int value. Implement the method without using integer. parseint(x). Draw the UML diagram for the class. Implement the class. Write a client program that tests all methods in the class.Explanation / Answer
***** MyInteger.java **********
public class MyInteger {
private int data;
public MyInteger(int data) {
this.data = data;
}
public int getValue(){
return data;
}
public boolean isEven(){
return data%2 == 0;
}
public boolean isOdd(){
return data%2 == 1;
}
public boolean isPrime(){
for(int i=2; i<=data/2; i++){
if(data%i == 0)
return false;
}
return true;
}
public static boolean isEven(int n){
return n%2 == 0;
}
public static boolean isOdd(int n){
return n%2 == 1;
}
public static boolean isPrime(int n){
for(int i=2; i<=n/2; i++){
if(n%i == 0)
return false;
}
return true;
}
public boolean equals(int n){
return data == n;
}
public boolean equals(MyInteger n){
return data == n.getValue();
}
public static int parseInt(String s){
int n = 0;
for(int i=0; i<s.length(); i++){
n = n*10 + s.charAt(i)-'0';
}
return n;
}
}
********** Test Class ************
public class TestMyInteger {
public static void main(String[] args) {
MyInteger myInt1 = new MyInteger(20);
System.out.println("Value: "+ myInt1.getValue());
System.out.println("isEven ? "+myInt1.isEven());
System.out.println("isOdd ? "+myInt1.isOdd());
System.out.println("isPrime ? "+myInt1.isPrime());
// testing static methods
System.out.println("isEven(13): "+MyInteger.isEven(13));
System.out.println("isOdd(13): "+MyInteger.isOdd(13));
System.out.println("isPrime(13): "+MyInteger.isPrime(13));
System.out.println(MyInteger.parseInt("34"));
}
}
/*
Sample Output:
Value: 20
isEven ? true
isOdd ? false
isPrime ? false
isEven(13): false
isOdd(13): true
isPrime(13): true
34
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.