(The MyInteger class) Design a class named MyInteger. The class contains: An int
ID: 3827476 • 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 getter 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.
"Please use Eclipse to write program" and give me attached file.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
###############
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 static boolean isEven(MyInteger n){
return n.data%2 == 0;
}
public static boolean isOdd(MyInteger n){
return n.data%2 == 1;
}
public static boolean isPrime(MyInteger n){
for(int i=2; i<=n.data/2; i++){
if(n.data%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;
}
public static int parseInt(char[] s){
int n = 0;
for(int i=0; i<s.length; i++){
n = n*10 + s[i]-'0';
}
return n;
}
}
#################
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 run:
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.