
JAVA MyInteger类问题,高手帮帮忙吧
设计名为MyInteger的类,它包括:一个名为value的int型数据域,存储这个对象表示的int值。一个为指定的int值创建的MyInteger对象的构造方法。一个返...
设计名为MyInteger的类,它包括:
一个名为value的int型数据域,存储这个对象表示的int值。
一个为指定的int值创建的MyInteger对象的构造方法。
一个返回int的get方法。
如果值分别为偶数,奇数或素数,那么isEven()、isOdd()、isPrime()方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(int)、isOdd(int)、isPrime(int)方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(MyInteger)、isOdd(MyInteger)、isPrime(MyInteger)方法都会返回true。
如果该对象的值与指定的值相等,那么equals(int)和equals(MyInteger)方法返回return。
静态方法parseInt(char[])将数字字符构成的数组转换为一个int值。
静态方法parseInt(String)将一个字符串转换为一个int值。
编写用户程序测试该类中的所有方法。
我自己只编写了:
public class MyInteger {
int value;
public MyInteger(int value){
this.value = value;
}
public int getInteger(){
return value;
}
public boolean isEven(){
return isEven(value);
}
public boolean isOdd(){
return isOdd(value);
}
public boolean isPrime(){
return isPrime(value);
}
public static boolean isEven(int value){
if(value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int value){
if(value % 2 == 1)
return true;
else
return false;
}
public static boolean isPrime(int value){
for(int i = 2;i<value;i++)
if(value%i!=0)
return false;
return true;
}
} 展开
一个名为value的int型数据域,存储这个对象表示的int值。
一个为指定的int值创建的MyInteger对象的构造方法。
一个返回int的get方法。
如果值分别为偶数,奇数或素数,那么isEven()、isOdd()、isPrime()方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(int)、isOdd(int)、isPrime(int)方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(MyInteger)、isOdd(MyInteger)、isPrime(MyInteger)方法都会返回true。
如果该对象的值与指定的值相等,那么equals(int)和equals(MyInteger)方法返回return。
静态方法parseInt(char[])将数字字符构成的数组转换为一个int值。
静态方法parseInt(String)将一个字符串转换为一个int值。
编写用户程序测试该类中的所有方法。
我自己只编写了:
public class MyInteger {
int value;
public MyInteger(int value){
this.value = value;
}
public int getInteger(){
return value;
}
public boolean isEven(){
return isEven(value);
}
public boolean isOdd(){
return isOdd(value);
}
public boolean isPrime(){
return isPrime(value);
}
public static boolean isEven(int value){
if(value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int value){
if(value % 2 == 1)
return true;
else
return false;
}
public static boolean isPrime(int value){
for(int i = 2;i<value;i++)
if(value%i!=0)
return false;
return true;
}
} 展开
2个回答
2012-10-06
展开全部
为了提高性能可以将 value % 2 == 1 改为 value >> 2 == 1
public static boolean isPrime(int value)中循环for(int i = 2;i<value;i++) 的value可以改为value/2+1 就可以了
public static boolean isPrime(int value)中循环for(int i = 2;i<value;i++) 的value可以改为value/2+1 就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询