In: Computer Science
Java
Implement a class MyInteger that contains:
• An int data field/instance variable named value that stores the int value represented by this object.
• A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice?
• A constructor that creates a MyInteger object for a specified int value.
• A getter method, valueOf(), that returns value.
• A setter method, setValue(int), that replaces the current value with the specified one and returns the original value.
• The method equals(int) that return true if the value in this object is equal to the specified value. This method will not override Object’s equals() method.
• The method toString() that returns a text (String) version of value. toString() must include thousands grouping separators when value is greater than 999. This method will override Object’s toString() method. You will need to use String.format() to get the desired result.
• The static methods isZero(int), isEven(int) and isOdd(int) that return true if the specified value is zero, even or odd, respectively.
• The instance methods isZero(), isEven() and isOdd() that return true if the value in this object is zero, even or odd, respectively. Do not duplicate the code in the static methods – use them to do the work in the instance methods.
• The methods increment() and decrement() that increase or decrease value by 1, respectively. These methods should both modify value and return its new value.
• The instance methods add(int), subtract(int), multiplyBy(int), divideBy(int), and remainder(int), corresponding to the five arithmetic operators (+, -, *, /, %). These methods should both modify value and return its new value.
Continue by adding the following:
• The instance method equals(Object) that return true if the value in this object is equal to the value of the specified instance (the parameter – an instance of MyInteger). This method will override Object’s equals() method. You must check the parameter to make sure it’s not null and that it’s a MyInteger before you can cast it to a MyInteger and interrogate its value.
• The instance methods add(MyInteger), subtract(MyInteger), multiplyBy(MyInteger), divideBy(MyInteger), and remainder(MyInteger), corresponding to the five arithmetic operators (+, -, *, /, %). These methods should both modify value in this instance and return its new value. The specified instance (parameter) is unchanged.
• The methods isPrime() and isPrime(MyInteger) that return true if the value in this object or the specified instance (parameter), respectively, is a prime number. isPrime() is an instance method and isPrime(MyInteger) is a static method.
• The static methods parseInt(char[]) that converts an array of numeric characters to an int value and parseInt(String) that converts a String of numeric characters to an int value. You must do the conversions by hand (hint: subtract '0' from each digit character to get its numeric value).
• Javadoc comments for all methods. Make sure you generate the documentation for all public elements.
Create a MyIntegerStudentTests to test all methods. Your tests should include positive numbers, negative numbers, and zero as arguments and as the results of the operations. Make sure you test the static and instance methods.
Your program should not do any input/prompting. The output is mostly up to you, but it should inform the user of the operations under test and the expected and actual results.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// MyInteger.java
public class MyInteger {
// Declaring instance variables
private int value;
// Zero Argumented constructor
public MyInteger() {
this.value = 1;
}
// Parameterized constructor
public MyInteger(int value) {
this.value = value;
}
// Getter method
public int valueOf() {
return value;
}
public void setValue(int val) {
this.value = val;
}
// This method will check whether the number is
even or not
public boolean isEven() {
if (value % 2 == 0)
return
true;
else
return
false;
}
// This method will check whether the number is odd
or not
public boolean isOdd() {
if (value % 2 != 0)
return
true;
else
return
false;
}
// This method will check whether the number is
prime or not
public boolean isPrime() {
// If the user entered number is
'2' return true
if (value == 2)
return
true;
for (int i = 2; i * i <= value;
i++) {
if (value % i ==
0)
return false;
}
return true;
}
public void increment() {
this.value = value + 1;
}
public void decrement() {
this.value = value - 1;
}
public int add(int val) {
this.value += val;
return value;
}
public int subtract(int val) {
this.value -= val;
return value;
}
public int multiplyBy(int val) {
this.value *= val;
return value;
}
public int divideBy(int val) {
this.value /= val;
return value;
}
public int remainder(int val) {
this.value %= val;
return value;
}
@Override
public boolean equals(Object o) {
if (o != null) {
MyInteger mi =
(MyInteger) o;
if (value ==
mi.valueOf()) {
return true;
} else {
return false;
}
}
return false;
}
// This method will check whether the number is
even or not
public static boolean isEven(int val) {
if (val % 2 == 0)
return
true;
else
return
false;
}
// This method will check whether the number is odd
or not
public static boolean isOdd(int val) {
if (val % 2 != 0)
return
true;
else
return
false;
}
// This method will check whether the number is
prime or not
public static boolean isPrime(int val) {
// If the user entered number is
'2' return true
if (val == 2)
return
true;
for (int i = 2; i * i <= val;
i++) {
if (val % i ==
0)
return false;
}
return true;
}
public boolean isZero() {
if (value == 0)
return
true;
else
return
false;
}
public static boolean isZero(int val) {
if (val == 0)
return
true;
else
return
false;
}
// This method will check whether the number is
prime or not
public static boolean isPrime(MyInteger mi) {
// If the user entered number is
'2' return true
if (mi.valueOf() == 2)
return
true;
for (int i = 2; i * i <=
mi.valueOf(); i++) {
if (mi.valueOf()
% i == 0)
return false;
}
return true;
}
// This method will whether the two numbers are
equal are not
public boolean equals(int val) {
if (this.value == val)
return
true;
else
return
false;
}
// This method will whether the two numbers are
equal are not
public boolean equals(MyInteger mi) {
if (this.value ==
mi.valueOf())
return
true;
else
return
false;
}
public static int parseInt(char[] ch) {
return
Integer.parseInt(String.valueOf(ch));
}
public static int parseInt(String str) {
return Integer.parseInt(str);
}
public String toString() {
return "MyInteger " + value;
}
public int add(MyInteger mi) {
this.value += mi.valueOf();
return value;
}
public int subtract(MyInteger mi) {
this.value -= mi.valueOf();
return value;
}
public int multiplyBy(MyInteger mi) {
this.value *= mi.valueOf();
return value;
}
public int divideBy(MyInteger mi) {
this.value /= mi.valueOf();
return value;
}
public int remainder(MyInteger mi) {
this.value %= mi.valueOf();
return value;
}
}
___________________________
// MyIntegerStudentTests.java
public class MyIntegerStudentTests {
public static void main(String[] args) {
int num = 567;
MyInteger mi1 = new
MyInteger(num);
if (mi1.isEven()) {
System.out.println(num + " is
Even.");
} else {
System.out.println(num + " is not
Even.");
}
if (mi1.isOdd()) {
System.out.println(num + " is
Odd.");
} else {
System.out.println(num + " is not
Odd.");
}
if (mi1.isPrime()) {
System.out.println(num + " is
Prime.");
} else {
System.out.println(num + " is not
Prime.");
}
num = 555;
if (MyInteger.isEven(num)) {
System.out.println(num + " is
Even.");
} else {
System.out.println(num + " is not
Even.");
}
if (MyInteger.isOdd(num)) {
System.out.println(num + " is
Odd.");
} else {
System.out.println(num + " is not
Odd.");
}
if (MyInteger.isPrime(num)) {
System.out.println(num + " is
Prime.");
} else {
System.out.println(num + " is not
Prime.");
}
num = 987;
MyInteger mi2 = new
MyInteger(num);
if (MyInteger.isEven(num)) {
System.out.println(num + " is
Even.");
} else {
System.out.println(num + " is not
Even.");
}
if (MyInteger.isOdd(num)) {
System.out.println(num + " is
Odd.");
} else {
System.out.println(num + " is not
Odd.");
}
if (MyInteger.isPrime(mi2)) {
System.out.println(num + " is
Prime.");
} else {
System.out.println(num + " is not
Prime.");
}
num = 567;
if (mi1.equals(num)) {
System.out.println(mi1.valueOf() +
" is equal to " + num);
} else {
System.out.println(mi1.valueOf() +
" is not equal to " + num);
}
if (mi1.equals(mi2)) {
System.out.println(mi1.valueOf() +
" is equal to " + mi2.valueOf());
} else {
System.out.println(mi1.valueOf() +
" is not equal to " + mi2.valueOf());
}
char arr[] = {
'9',
'9',
'9'
};
int number1 =
MyInteger.parseInt(arr);
System.out.println("After
converting char array to number : " + number1);
int number2 =
MyInteger.parseInt("786");
System.out.println("After
converting String to number : " + number2);
}
}
_____________________________
Output:
567 is not Even.
567 is Odd.
567 is not Prime.
555 is not Even.
555 is Odd.
555 is not Prime.
987 is not Even.
987 is Odd.
987 is not Prime.
567 is equal to 567
567 is not equal to 987
After converting char array to number : 999
After converting String to number : 786
_______________Could you plz rate me well.Thank
You