设计一个名为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-03 09:49:13 浏览: 14
好的,以下是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 == 1;
}
public boolean isPrime() {
if (value <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(value); i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
public boolean equals(int num) {
return value == num;
}
public boolean equals(MyInteger myInt) {
return value == myInt.getValue();
}
public static int parseInt(char[] chars) {
int result = 0;
for (int i = 0; i < chars.length; i++) {
result = result * 10 + (chars[i] - '0');
}
return result;
}
public static int parseInt(String str) {
return Integer.parseInt(str);
}
}
```
其中,isPrime方法使用了基本的判断质数的算法,即从2到该数的平方根逐个判断是否能整除。
另外,parseInt方法通过将字符数组或字符串中的字符逐个转换成数字,然后累加起来得到对应的int值。
希望这个MyInteger类能够满足你的需求,有问题可以随时问我。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)