设计一个名为MyInteger的类。这个类包括 1.一个名为value的int型数据域,存储这个对象表示的int值 2.一个指定的int值创建MyInteger对象构造方法 3.一个返回int值得get方法 4.如果值分别为偶数,奇数,素数,那么isEven() isOdd() isprime()方法都会返回ture 5.如果该对象的值与指定的值相等,那么equals (int) 和 equals(MyInteger)方法返回值ture 6.静态方法parseInt(Char[])将数字字符构成的数组转换成一个int值 7.静态方法parseInt(String)将一个字符串转换成一个int值并且运行
时间: 2024-03-01 11:52:09 浏览: 52
好的,我已经了解了你的问题。下面是对你的问题的解答:
以下是关于MyInteger类的实现:
```java
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isEven() {
return (value % 2 == 0);
}
public boolean isOdd() {
return (value % 2 != 0);
}
public boolean isPrime() {
if (value == 1 || value == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(value); i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
public boolean equals(int intValue) {
return (value == intValue);
}
public boolean equals(MyInteger myInteger) {
return (value == myInteger.getValue());
}
public static int parseInt(char[] charArray) {
int value = 0;
for (int i = 0; i < charArray.length; i++) {
value = value * 10 + (charArray[i] - '0');
}
return value;
}
public static int parseInt(String str) {
return Integer.parseInt(str);
}
}
```
这个类包含了一个int型数据域value,它存储了这个对象所代表的int值。此外,它还包含了一个指定的int值创建MyInteger对象构造方法、一个返回int值的get方法,以及isEven()、isOdd()和isPrime()方法,它们分别返回该对象的值是否为偶数、奇数、素数。
equals(int)和equals(MyInteger)方法分别用于比较该对象的值是否与指定的int值相等,以及是否与另一个MyInteger对象的值相等。
静态方法parseInt(char[])将数字字符构成的数组转换成一个int值。它首先将每个字符转换为数字,并将它们组合成一个int值。静态方法parseInt(String)将一个字符串转换成一个int值,并使用Java内置的Integer.parseInt()方法来完成转换。
希望这个解答对你有所帮助!
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)