In: Computer Science
In Java, design a class named MyInteger. The class contains:
Testing: (Describe how you test this program, you should use your own input data to test not just the test cases given in sample runs.) (4 points)
Test your program using the class and the main method given below. There are two test objects, n1 and n2 in the following test program, you should add a third test object, n3 with your own integer, and invoke all methods in your MyInteger program using object n3.
public class ProgrammingAssignmentEight {
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));
// Write your third test code for object n3 here!
}
}
Code to copy along with screenshots of code and
output are provided.
Please refer to screenshots to understand the indentation of
code.
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of "ProgrammingAssignmentEight.java":
Screenshots of "MyInteger.java":
Screenshots of Output :
---------------------------------------------------------------------------------------------------------
Code to copy(" ProgrammingAssignmentEight.java " ):
---------------------------------------------------------------------------------------------------------
public class ProgrammingAssignmentEight {
public static void main(String[] args) {
// initializing MyInteger object with value = 5
MyInteger n1 = new MyInteger(5);
// checking isEven() function
System.out.println("n1 is even? " + n1.isEven());
// checking isPrime() function
System.out.println("n1 is prime? " + n1.isPrime());
// Checking isPrime(int) function
System.out.println("15 is prime? " + MyInteger.isPrime(15));
// declaring character array
char[] chars = {'3', '5', '3', '9'};
// checking parseInt(char[]) function
System.out.println(MyInteger.parseInt(chars));
// initializing string
String s = "3539";
// checking parseInt(String) function
System.out.println(MyInteger.parseInt(s));
// initializing MyInteger object with value = 24
MyInteger n2 = new MyInteger(24);
// checking isOdd() function
System.out.println("n2 is odd? " + n2.isOdd());
// checking us Odd(int) function
System.out.println("45 is odd? " + MyInteger.isOdd(45));
// checking equals(int) function
System.out.println("n1 is equal to n2? " + n1.equals(n2));
// checking equals(MyInteger) function
System.out.println("n1 is equal to 5? " + n1.equals(5));
// Write your third test code for object n3 here!
// declaring MyInteger object named n3
MyInteger n3 = new MyInteger(5);
// print statement
System.out.println("<<<<<-------Third
Test-------->>>>>>.");
// checking isEven() function
System.out.println("n3 is even? " + n3.isEven());
// checking isPrime() function
System.out.println("n3 is prime? " + n3.isPrime());
// checking static isPrime(int) function
System.out.println("29 is prime? " + MyInteger.isPrime(29));
// declaring array of characters
char[] chars2 = {'4', '3', '3', '2'};
// checking parseInt(char[]) function
System.out.println("chars2 = "+ MyInteger.parseInt(chars2));
// declaring string
String s2 = "4332";
// checking parseInst(String) function
System.out.println("s2 = "+ MyInteger.parseInt(s2));
// adding integers returned by parseInt function for checking
System.out.println("chars2 + s2 = " + (MyInteger.parseInt(chars2) +
MyInteger.parseInt(s2)));
// checking equals(int) function
System.out.println("n3 is equal to n1? " + n3.equals(n1));
// checking equals(MyInteger) function
System.out.println("n3 is equal to 9? " + n1.equals(9));
}
}
------------------------------------------------------------------------------
Code to copy(" MyInteger.java " ):
------------------------------------------------------------------------------
public class MyInteger {
// integer variable to store value
int value;
// constructor
MyInteger(int val)
{
this.value = val;
}
// boolean function to return true if value is
even
boolean isEven()
{
if(this.value % 2 == 0)
{
// return
true
return
true;
}
else
{
// otherwise
return false
return
false;
}
}
// boolean function to return false if value is
Odd
boolean isOdd()
{
if(this.value % 2 == 0)
{
// return false
if remainder is 0
return
false;
}
else
{
// otherwise
return true
return
true;
}
}
// function to return true if value is prime
boolean isPrime()
{
for(int i = 2; i <=
this.value/2; ++i)
{
if(this.value % i == 0)
{
// return false if remainder is 0
return false;
}
}
// otherwise return true if
divisible by none
return true;
}
// static method to return true if n is even
static boolean isEven(int n)
{
if(n%2 == 0)
{
// return true
if divisible by 2
return
true;
}
else
{
// otherwise
return false
return
false;
}
}
// static method to return true if n is Odd
static boolean isOdd(int n)
{
if(n%2 == 0)
{
// return false
if divisible by 2
return
false;
}
else
{
// otherwise
return false
return
true;
}
}
// static method to return true if n is prime
static boolean isPrime(int n)
{
for(int i = 2; i <= n/2;
++i)
{
if(n % i == 0)
{
// return false if n is divisible by i
return false;
}
}
// return true if divisible by
none
return true;
}
// method to return true if value is equal to n
boolean equals(int n)
{
if(this.value == n)
{
// return true
if value is equal to n
return
true;
}
else
{
// otherwise
return false
return
false;
}
}
// method to return true if MyInteger object's value
is equalt to this value
boolean equals(MyInteger n)
{
if(this.value == n.value)
{
// return true
if equal
return
true;
}
else
{
// otherwise
return false
return
false;
}
}
// method to return the integer value of array of
characters
static int parseInt(char[] chars)
{
// store the value into integer
num
int num =
Integer.parseInt(String.valueOf(chars));
// return num
return num;
}
// method to return the integer value of string
str
static int parseInt(String str)
{
// store integer value into
num
int num =
Integer.parseInt(str);
// return num
return num;
}
}
------------------------------------------------XXXXXXXXXX--------------------