6-1 设计MyInteger类 (10分)

设计一个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;
	}
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值