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

Pr og r am 1 (50 points): Following the instructions in the problem statement, d

ID: 3714587 • Letter: P

Question

Program 1 (50 points): Following the instructions in the problem statement, design and implement a Java program for programming exercise 10.3, page 400 (name it MyInteger.java). Next, develop a test program in a separate file (call it TestMyInteger.java) to test all methods of the class. Document your code and organize the output using appropriate formatting techniques. To implement the parseInt() methods, do not use Java standard library methods (such as Integer.parseInt() or Character.getNumericValue()) other than charAt()from the class String – you must implement the approach yourself.

10.3 (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 isEvenO, is0dd(), 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 isPrimeCint) that return true if the specified value is even, odd, or prime, respectively. The static Textbook exercise 10.3 lyInteger), 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 cli- ent program that tests all methods in the class.

Explanation / Answer

MyInteger.java

public class MyInteger {
//Declaring instance variables
private int value;

//Parameterized constructor
public MyInteger(int value) {
this.value = value;
}
//Getter method
public int getValue() {
return value;
}

//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;

}


//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;
}

//This method will check whether the number is even or not
public static boolean isEven(MyInteger mi) {
if (mi.getValue() % 2 == 0)
return true;
else
return false;
}

//This method will check whether the number is odd or not
public static boolean isOdd(MyInteger mi) {
if (mi.getValue() % 2 != 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.getValue() == 2)
return true;
for (int i = 2; i * i <= mi.getValue(); i++) {
if (mi.getValue() % 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.getValue())
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);
}
}

_________________

Test.java

public class Test {

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(mi2)) {
System.out.println(num + " is Even.");
} else {
System.out.println(num + " is not Even.");
}
if (MyInteger.isOdd(mi2)) {
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.getValue() + " is equal to " + num);
} else {
System.out.println(mi1.getValue() + " is not equal to " + num);
}
if (mi1.equals(mi2)) {
System.out.println(mi1.getValue() + " is equal to " + mi2.getValue());
} else {
System.out.println(mi1.getValue() + " is not equal to " + mi2.getValue());
}

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


_____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote