Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program is already done the question is why is : \"class MyInteger {\" BEING

ID: 3916637 • Letter: T

Question

The program is already done the question is why is : "class MyInteger {" BEING USED ?

public class Exercise10_03 {

public static void main(String[] args) {

MyInteger n1 = new MyInteger(5);

System.out.println("n1 is even? " + n1.isEven());

System.out.println("n1 is prime? " + n1.isPrime());

System.out.println("15 is prime? " + MyInteger.isPrime(15));

char[] chars = {'3', '5', '3', '9'};

System.out.println(MyInteger.parseInt(chars));

String s = "3539";

System.out.println(MyInteger.parseInt(s));

MyInteger n2 = new MyInteger(24);

System.out.println("n2 is odd? " + n2.isOdd());

System.out.println("45 is odd? " + MyInteger.isOdd(45));

System.out.println("n1 is equal to n2? " + n1.equals(n2));

System.out.println("n1 is equal to 5? " + n1.equals(5));

}

}

class MyInteger {

int value;

MyInteger(int newValue) {

value = newValue;

}

public int getValue() {

return value;

}

public static boolean isEven(int n) {

return (n % 2 == 0);

}

public static boolean isOdd(int n) {

return !isEven(n);

}

public static boolean isPrime(int n) {

for (int f = 2; f < n / 2; f++) {

if (n % f == 0) {

return false;

}

}

return true;

}

public static boolean isEven(MyInteger n) {

return n.isEven();

}

public static boolean isOdd(MyInteger n) {

return n.isOdd();

}

public static boolean isPrime(MyInteger n) {

return n.isPrime();

}

public boolean isEven() {

return isEven(value);

}

public boolean isOdd() {

return isOdd(value);

}

public boolean isPrime() {

return isPrime(value);

}

public boolean equals(int n) {

return (value == n);

}

public boolean equals(MyInteger n) {

return equals(n.getValue());

}

public static int parseInt(String s) {

return Integer.parseInt(s);

}

public static int parseInt(char[] s) {

return parseInt(new String(s));

}

}

Explanation / Answer

The purposes of defining and using MyInteger class are as follows:

1) To define a class of integers with several predefined variables and methods. So that whenever we create an object of this class, it comes predefined with all these variables and methods like value, isEven(), etc. and we do not need to create them separately.

e.g. MyInteger n1=new MyInteger(5);

System.out.println(n1.isPrime());

2) This also is useful in code hiding or abstraction. If the MyInteger class is located at a different location than the main class, anyone using the methods of MyInteger class can just access the methods but cannot the method body. So the body of the methods is hidden from one who uses these methods.

3) This also facilitates code reuse i.e. we can use the methods of MyInteger class in any other class by just class name and variable name or method name instead of defining them separately in other classes.

e.g. MyInteger.value, MyInteger.isPrime(5), MyInteger.isEven(4), etc.