Question: LongInteger Class Implement a class named LongInteger. The class contains: A private data field of type long A constructor that takes parameter of type long

LongInteger Class
Implement a class named LongInteger. The class contains:
A private data field of type long
A constructor that takes parameter of type long and creates LongInteger object for the specified long value.
toLong() method that returns the value of the field.
The instance methods isEven(), isOdd(), and isPrime() that return true if the value in the object is even, odd, or prime respectively.
The static methods isEven(long), isOdd(long), and isPrime(long) that return true if the specified value is even, odd, or prime respectively.
The instance method equals(long) and instance method equals(LongInteger) that return true if the value in this object is equal to the specified value.
A static method parseLong(String) that converts a string into an long value, positive or negative. Method throws IllegalArgumentException when
A string contains non-digit characters (other than '-' as the very first character of the sting).
A string has only '-' and no digits.
A string represents a number that is too large to be stored as long and produces overflow
A string represents a number that is too small to be stored as long and produces underflow
Test cases:
import java.io.PrintWriter;
public class TestLongInteger {
public static boolean tests(PrintWriter outputStream)
{
outputStream.println("\r
----LongInteger Class TEST SETS -------------------------------------------------------\r
");
boolean t1= test01LongIntegerClass(outputStream);
boolean t2= test02LongIntegerClass(outputStream);
boolean t3= test03LongIntegerClass(outputStream);
return t1&&t2&&t3;
}
public static boolean test01LongIntegerClass(PrintWriter outputStream)
{
int count =0;
int expectedCount =3;
int a =55;
int b =102;
int prime =97;
//Test #1
LongInteger num1= new LongInteger(a);
if(num1.toLong()==a)
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: constructor", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: constructor", "FAILED");
//Test #2
LongInteger num2= new LongInteger(b);
if(!num1.isEven()&& num1.isOdd()&& num2.isEven() && !num2.isOdd())
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: isEven() and isOdd()", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: isEven() and isOdd()", "FAILED");
//Test #3
LongInteger num3= new LongInteger(prime);
if(num3.isPrime() && !num2.isPrime() && !num1.isPrime())
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: isPrime()", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 01: isPrime()", "FAILED");
if (count==expectedCount) return true;
else return false;
}
public static boolean test02LongIntegerClass(PrintWriter outputStream)
{
int count =0;
int expectedCount =3;
int a =103;
int b =500;
int prime =919;
//Test #1
if(!LongInteger.isEven(a)&& LongInteger.isOdd(a)&& LongInteger.isEven(b) && !LongInteger.isOdd(b))
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: static isEven() and static isOdd()", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: static isEven() and static isOdd()", "FAILED");
//Test #2
if(LongInteger.isPrime(prime) && LongInteger.isPrime(a) && !LongInteger.isPrime(b))
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: static isPrime()", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: static isPrime()", "FAILED");
//Test #3
LongInteger num1= new LongInteger(a);
LongInteger num2= new LongInteger(a);
LongInteger num3= new LongInteger(b);
if(num1.equals(a) && !num1.equals(b) && num1.equals(num2) && !num3.equals(num1))
{
outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: both equals methods", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "LongInteger TEST SET 02: both equals methods", "FAILED");
if (count==expectedCount) return true;
else return false;
}
public static boolean test03LongIntegerClass(PrintWriter outputStream)
{
int count =0;
int expectedCount =4;
//Test #1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!