求解java题
(TheMyIntegerclass)DesignaclassnamedMyInteger.Theclasscontains:■Anintdatafieldnamedva...
(The MyInteger class ) 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 get method that returns the int value.
■ Methods isEven() , isOdd() , and isPrime() that return true if the value is
even, odd, or prime, respectively.
■ Static methods isEven(int) , isOdd(int) , and isPrime(int) that return
true if the specified value is even, odd, or prime, respectively.
■ Static methods isEven(MyInteger) , isOdd(MyInteger) , and
isPrime(MyInteger) that return true if the specified value is even, odd, or
prime, respectively.
■ Methods equals(int) and equals(MyInteger) that return true if the
value in the 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. 展开
■ 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 get method that returns the int value.
■ Methods isEven() , isOdd() , and isPrime() that return true if the value is
even, odd, or prime, respectively.
■ Static methods isEven(int) , isOdd(int) , and isPrime(int) that return
true if the specified value is even, odd, or prime, respectively.
■ Static methods isEven(MyInteger) , isOdd(MyInteger) , and
isPrime(MyInteger) that return true if the specified value is even, odd, or
prime, respectively.
■ Methods equals(int) and equals(MyInteger) that return true if the
value in the 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. 展开
3个回答
展开全部
public class myinteger {
private int number;
/**构造函数**/
public myinteger(int number){
this.number = number;
}
/**get方法获取对象内的值**/
public int get(){
return this.number;
}
/**判断对象内的值是否为偶数
* @return 为偶数返回true,否则返回false
* **/
public boolean isEven(){
if(this.number%2 == 0)return true;
else return false;
}
/**判断对象内的值是否为奇数
* @return 为奇数返回true,否则返回false
* **/
public boolean isOdd(){
if(this.number%2 != 0)return true;
else return false;
}
/**判断对象内的值是否为素数
* @return 素数返回true,不是素数返回false
* **/
public boolean isPrime(){
for(int i = 2; i < this.number; i++){
if(this.number%i == 0)return false;
}
return true;
}
/**静态方法<p> 判断对象内的值是否为偶数
* @param num 被判断的值
* @return 为偶数返回true,否则返回false
* **/
public static boolean isEven(int num){
if(num%2 == 0)return true;
else return false;
}
/**静态方法<p> 判断对象内的值是否为奇数
* @param num 被判断的值
* @return 为奇数返回true,否则返回false
* **/
public static boolean isOdd(int num){
if(num%2 != 0)return true;
else return false;
}
/**静态方法<p> 判断对象内的值是否为素数
* @param num 被判断的值
* @return 素数返回true,不是素数返回false
* **/
public static boolean isPrime(int num){
for(int i = 2; i < num; i++){
if(num%i == 0)return false;
}
return true;
}
/**
* 判断传入值是否与对象内值相等
* @param 被判断值,类型为int
* @return 相等返回true,不等返回false
* */
public boolean equals(int num){
if(this.number == num)return true;
else return false;
}
/**
* 判断传入的myinteger对象中的值与本对象内的值是否相等
* @param 被判断值 类型为int
* @return 相等返回true,不等返回false
* **/
public boolean equals(myinteger num){
if(this.number == num.get())return true;
else return false;
}
/**
* 静态方法<p>将char数组中的文字数字转化成int型
* @param 传入一个char型的组,请保持数组内的字符仅由数字组成,否侧抛出异常
* @return 返回转化后的值
* **/
public static int parseInt(char [] ch){
String st = "";
for(char c:ch)st += c;
return parseInt(st);
}
/**
* 静态方法<p>将String中的文字数字转化成int型
* @param 传入一个String型字符串,请保持串内的字符仅由数字组成,否侧抛出异常
* @return 返回转化后的值
* **/
public static int parseInt(String str){
return Integer.parseInt(str);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |