设计一个MyInteger的类。这个类包括:
一个名为value的int类型数据域,存储这个对象表示的int值。
一个为指定的int值创建MyInteger对象的构造方法。
一个为缺省值创建MyInteger对象的构造方法,value缺省值为0.
如果对象中的值分别是偶数、奇数或者素数,那么isEven()、isOdd()和isPrime()方法会返回true。
两个equals()方法,如果该对象的值与指定的值相等,那么equals(int)和equals(MyInteger)返回true。
函数接口定义:
请仔细阅读测试程序。
裁判测试程序样例:
测试程序如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
MyInteger A = new MyInteger(x);
MyInteger B = new MyInteger(y);
MyInteger C = new MyInteger();
System.out.println(A.isEven());
System.out.println(A.isOdd());
System.out.println(A.isPrime());
System.out.println(A.equals(z));
System.out.println(A.equals(B));
}
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
18 18 6
输出样例:
在这里给出相应的输出。例如:
true
false
false
false
true
class MyInteger{
int a;
MyInteger()
{
}
MyInteger(int x)
{
a=x;
}
public boolean isEven()
{
if(a%2==0)
return true;
else
return false;
}
public boolean isOdd()
{
if(a%2!=0)
return true;
else
return false;
}
public boolean isPrime()
{int i,k=0;
if(a==2)
return true;
for(i=2;i<a;i++)
{
if(a%i==0)
k=1;
}
if(k==0)
return true;
else
return false;
}
public boolean equals(MyInteger a)
{
if (this.a==a.a)
return true;
else
return false;
}
public boolean equals(int a)
{
if (this.a==a)
return true;
else
return false;
}
}