Java Program - Design a class named MyInteger. The class contains: ? An int data
ID: 3568303 • Letter: J
Question
Java Program -
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 isEven(), isOdd(), 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 isPrime(int) that
return true if the specified value is even, odd, or prime, respectively.
? The static methods isEven(MyInteger), 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 client
program that tests all methods in the class.
Next, develop a test program in a separate file (call it TestMyInteger.java) to create all methods of the class.
Explanation / Answer
import java.util.Scanner;
class MyInteger
{
int value;
MyInteger()
{
System.out.println("Enter a new number");
Scanner sc= new Scanner(Syatem.in);
int value= sc.nextInt();
}
public static int getter()
{
System.out.println("Enter a new number");
Scanner sc= new Scanner(Syatem.in);
int i= sc.nextInt();
return i;
}
public static boolean isEven(int a)
{
if(a%2==0)
return true;
}
public static boolean isOdd(int a)
{
if (a%2 !=0)
return true;
}
public static boolean isPrime(int a)
{
int count=0;
for(int i=2; i<a; i++)
{
if(a%i==0) count ++;
}
if(count==0) return true;
}
public static boolean isEven(MyInteger a)
{
if(a.value%2==0)
return true;
}
public static boolean isOdd(MyInteger a)
{
if (a.value%2 !=0)
return true;
}
public static boolean isPrime(MyInteger a)
{
int count=0;
for(int i=2; i<a; i++)
{
if(a.value%i==0) count ++;
}
if(count==0) return true;
}
public static boolean equals(int a)
{
if (a==value) return true;
}
public static boolean equals(MyInteger a)
{
if (a.value==value) return true;
}
public static int parseInt()// conversion of char to int
{
char[] digits = { '1', '2', '3' };
int number = Integer.parseInt(new String(digits));
return number;
}
public static int parseInt(String s)
{
int y = Integer.parseInt(s);
return y;
}
}
class TestMyInteger
{
public static void main(String args[])
{
MyInteger m = new MyInteger();
int i= getter();
System.out.println(isEven(i));
System.out.println(isOdd(i));
System.out.println(isPrime(i));
System.out.println(isEven(m));
System.out.println(isOdd(m));
System.out.println(isPrime(m));
System.out.println(equals(i));
System.out.println(equals(m));
System.out.println(parseInt());
System.out.println(parseInt("1768"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.